colorfulstring
colorfulstring is a lightweight Python utility for building ANSI-colored terminal strings with a fluent, chainable API.
Installation
pip install colorfulstring
Quick Start
from colorfulstring import c
print(c.r << "Error:" << " something went wrong")
print(c.g("OK"))
Usage
1) Color Shortcuts
Available shortcut color properties are d/r/g/y/b/p/c/w (dark/red/green/yellow/blue/purple/cyan/white).
You can chain them for background presets, e.g. c.b.g means blue foreground + green background.
Chaining is limited to two colors; c.b.g.r is not allowed.
print(c.y << "Warning")
print(c.b.g << "Blue on green via chaining")
2) Pipe-Style Chaining
Use << (or @) to append fragments in sequence:
print(c.b << "[INFO]" << " service started")
Use >> to convert a finished builder:
builder >> str: finalize as a regular Python string (same asstr(builder)).builder >> c.plaintext: finalize and strip ANSI escape codes.
ansi = c.r << "Error" >> str
plain = c.r << "Error" >> c.plaintext
3) c() Shortcut
c(text) immediately converts one fragment using the current style context and returns a string.
print(c.r("error"))
print(c.underline.g("underlined green"))
4) Conditional Output
iftrue(condition): include the next fragment only whenconditionisTrue.ifnot(condition): include the next fragment only whenconditionisFalse.ifelse(condition): choose between the next two fragments.ifcases(cond1, cond2, ...): chain multipleifelsebranches and then provide one fallback fragment.
ok = True
line = c << "status: " << c.ifelse(ok) << c.g("success") << c.r("failed")
print(line)
score = 82
level = c.ifcases(score >= 90, score >= 75) << c.g("A") << c.y("B") << c.r("C")
print(c << "level: " << level)
5) Immediate Printing
c.print outputs each generated fragment immediately (without an automatic newline). It can be combined with c.endl.
line = c.print << "hello" << c.endl
6) Underline
Use .underline to add underline style:
print(c.underline << "plain underline")
print(c.underline.g << "green underline")
print(c.underline.g.b << "green on blue underline")
7) Faint Foreground
Use .faint to switch the foreground to a faint ANSI variant.
print(c.faint.r << "faint red")
print(c.faint.underline.g.b << "faint green on blue underline")
Note:
c.faintis no different fromc, if you need a faint dark color, tryc.faint.d.
8) Inline Token Grammar
Besides fluent chaining, colorfulstring can also parse inline token fragments from plain strings:
print(c << "$R:error$")
print(c << "$G-.B:faint green on blue$")
print(c << "$_Y:underlined yellow$")
Grammar (inside $...$):
TOKEN:textTOKEN:FG(foreground), e.g.R,G,BFG-(faint foreground)FG.BG(foreground + background)FG-.BG(faint foreground + background)- optional underline prefix:
_{TOKEN}
Escaping:
- Only
$$is treated as an escaped dollar sign ($). - Any
$...$fragment that is not a valid token expression raisesValueError. - A single unmatched
$is treated as an error and raisesValueError.
See Also
Github repository
PyPI project
License
This project falls under the BSD 3-Clause License.
History
v0.0.7
- Added
matchcases(value, *cases, fallback=True)helper for value-based multi-branch conditional rendering. - Improved conditional chaining behavior and docs for
ifcases(..., fallback=...)fallback handling.
v0.0.6
- Added
builder >> c.plaintext/c.plaintext(...)support to strip ANSI escape sequences from finalized output. - Added new method
c.ifcases(cond1, cond2, ...)for multi-branch conditional usage.
v0.0.5
- Improved inline token parsing diagnostics with clearer
ValueErrormessages for malformed token expressions. - Added strict handling for unmatched single
$markers and now raises explicit errors instead of silently accepting invalid input. - Fixed token rendering order so inline token fragments are interpreted before default style wrapping in chained color contexts.
v0.0.4
- Removed unnecessary imports.
v0.0.3
- Now correctly handles escaped dollar sequences like
$$in inline token parsing. - Improved README structure and polished API usage descriptions.
v0.0.2
- Added inline token grammar support (
$TOKEN:text$) to render ANSI styles directly from plain strings. - Added
.underlineand.faintstyle modifiers, including combinations with foreground/background colors. - Improved chaining/conditional flow documentation and examples (
iftrue,ifnot,ifelse,<<,@).
v0.0.1
- Renamed
ColorfulStringtoColorfulStringBuilderto avoid conflicts.
v0.0.0
- Initial release.
Metadata
Release files for colorfulstring 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| colorfulstring-0.0.7.tar.gz | 11.1 kB | Without provenance | Details for colorfulstring-0.0.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| colorfulstring-0.0.7-py3-none-any.whl | Python 3 | none | any | Details for colorfulstring-0.0.7-py3-none-any.whl |
Total release size: 21.8 kB
Release files / colorfulstring-0.0.7.tar.gz
| Download URL | colorfulstring-0.0.7.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
733ab0d8f69ecfe2b2cae8f09cbbe38e504d3abb4df400ade367b71dbf0c2b8e
|
|
BLAKE2b-256 checksum How to use checksums |
b8b03e4b2a2f89b2063c9a3c09c4f2443acf3c10333a9ab67fe7a9862510c5c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Release files / colorfulstring-0.0.7-py3-none-any.whl
| Download URL | colorfulstring-0.0.7-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
55e862c87d2dfebe9da1a936baa70cc9d3bc9011887c07103c878ddff1357383
|
|
BLAKE2b-256 checksum How to use checksums |
8bdeac451426610ece1beba4192cfbc1e036d9ac33a104a84bf002eff499a693
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.7
|
Security for colorfulstring 0.0.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.