Minimal, dependency-free ANSI color helper for terminal CLIs. A single
drop-in .py file, meant to be copied wherever it's needed rather than
installed as a package.
from pyansi import Ansi
ansi = Ansi() # defaults to stream=sys.stdout
print(ansi.red("error"))
print(ansi.red.bold("error")) # order doesn't matter
print(ansi.bold.red("error")) # same output as aboveColors: black, red, green, yellow, blue, magenta, cyan,
white, orange. Every color chains with .bold in either order. Color
is automatically disabled when output isn't a tty, or when NO_COLOR is
set (no-color.org).
There's no reset() -- every call returns a complete, self-contained
string (codes already opened and closed), so it composes with normal
print()/f-strings instead of imposing its own output model.
Run the file directly for a demo of every color:
./pyansi.py
Copy pyansi.py wherever your script can import it. If it's alongside
your script, a plain from pyansi import Ansi works as-is.
To install it somewhere shared (e.g. /usr/local/include, for other
scripts to import from), run:
./install.sh
This copies pyansi.py to /usr/local/include/pyansi.py (needs sudo).
Scripts importing it from there need to add that directory to sys.path
first -- see the import example in pyansi.py's own docstring.
pip install pytest
pytest
MIT -- see LICENSE.