Skip to main content

clitint

Very fast terminal text formatting for Python 3.13+, with no dependencies.

Python Dependencies Typed Licence

Text is coloured only when someone is reading it on a terminal: redirect the output to a file, a pipe or a CI log and it comes out plain.

This library is ideal for simple and very fast formatting of text on libraries and command-line tools.

from clitint import formatter

text_1 = formatter("error:", "bold_red")
text_2 = formatter("warning", "underline_black_highlight_yellow")
print(text_1, "file not found")
print(text_2)
clitint printing a bold red 'error:' and an underlined black-on-yellow 'warning' in a terminal, and plain text when redirected to a file

It is made for libraries and tools that build messages: one call with a short spec, an answer that follows the environment as it is now, and colours only when both stdout and stderr are terminals, so an error message that ends up in a log has no escape codes in it.

📦 Install

pip install clitint

🎨 Specs

A spec is a set of styles joined by _, in any order: "red", "bold_red", "red_bold", "italic_white_highlight_blue".

Kind Names
Attributes bold dim italic underline
Colours black red green yellow blue magenta cyan white grey orange pink purple brown teal gold maroon navy olive coral lime
Backgrounds highlight_ followed by a colour, e.g. highlight_blue

Combine as many attributes as you like with at most one colour and one background: bold_highlight_blue, italic_red, underline_green.

  • An unknown name, two colours or two backgrounds raise a ValueError that names the problem.
  • formatter takes any object and writes its str.
  • Each spec is worked out once and remembered.

🖥️ When text is coloured

formatter decides at every call, in this order:

# Condition Result
1 turn_colours_off() / turn_colours_on() was called as chosen
2 NO_COLOR, ANSI_COLORS_DISABLED or CLITINT_COLOURS_OFF=1 plain
3 FORCE_COLOR coloured
4 TERM=dumb plain
5 otherwise coloured only when both stdout and stderr are terminals

let_the_environment_decide() undoes rule 1, and colours_on() tells you the answer now.

NO_COLOR and FORCE_COLOR count when they are set to anything but an empty string, as no-color.org and force-color.org say (FORCE_COLOR=0 forces too). On Windows 10 and 11 the console is asked to read escape sequences; where it can't, text is plain unless forced.

🧰 Other tools

Switching colours from the program

from clitint import turn_colours_off, turn_colours_on, let_the_environment_decide, colours_on

turn_colours_off()              # plain from now on, whatever the environment says (a --no-color flag)
turn_colours_on()               # coloured from now on, even into a file or a pipe (a --color flag)
let_the_environment_decide()    # back to the rules above
colours_on()                    # the answer right now: True or False

Building one message: decide once

Every formatter call checks the environment and the streams. That is the only performance bottleneck this library has. It is insignificant for most use cases. However, when a message is built from many pieces (hunderds or thousands of calls to formatter), that can slowdown the process noticeably. Using one_decision avoids repeated checks and improves performance in such cases many times over.

from clitint import formatter, one_decision

with one_decision():
    line = formatter("a", "red") + " " + formatter("b", "blue") + " " + formatter("c", "green") + ...

Blocks can be nested, and an inner one keeps the outer answer. Something that changes the answer inside the block, such as turn_colours_off(), takes effect only after the block ends. The answer lives in a module global, so don't build messages in several threads at once.

decide(), decision() and release() do the same without a with block, for a library that wraps its own message building:

from clitint import decide, decision, release

decide()        # check now and keep the answer (returns it)
decision()      # the answer kept, or None if nothing is decided
release()       # forget it: each formatter call checks again

Raw escape sequences, for hot loops

escapes gives the sequences that go before and after the text, two per spec, in one flat tuple. They are the same whether colours are on or not, so check coloured() yourself. It returns the answer kept by one_decision / decide if there is one, and colours_on() otherwise:

from clitint import escapes, coloured

red, reset, bold, bold_reset = escapes("red", "bold")
text = f"{red}x{reset}" if coloured() else "x"

ATTRIBUTES, COLOURS and BACKGROUNDS are the tables of names and their sequences, and ESC is the reset sequence ("\x1b[0m").

Removing colours

from clitint import strip_colours

strip_colours(formatter("x", "red"))   # 'x': every ANSI style sequence removed (for tests and logs)

For a library built on clitint

Give your users an off switch of their own, next to CLITINT_COLOURS_OFF:

from clitint import add_off_switch

add_off_switch("MYLIB_COLOURS_OFF")    # MYLIB_COLOURS_OFF=1 now turns colours off too

Only the value 1 counts, and it is checked together with the other off switches, before FORCE_COLOR.

📄 Licence

MIT

Metadata

Release files for clitint 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clitint 0.1.4
File Size Uploaded Provenance Actions
clitint-0.1.4.tar.gz 18.5 kB Without provenance Details for clitint-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for clitint 0.1.4
File Interpreter ABI Platform Actions
clitint-0.1.4-py3-none-any.whl Python 3 none any Details for clitint-0.1.4-py3-none-any.whl

Total release size: 29.7 kB

Release files / clitint-0.1.4.tar.gz

Download URL clitint-0.1.4.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a9c106f60c886351348d75ff3ca84465179ecbfc7415be52167ff7e5d408efae
BLAKE2b-256 checksum
How to use checksums
2899d9f031efdaf92d0ec0b2e7f6595d3c45f9c1a9675d7d247d615dc07ff59d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / clitint-0.1.4-py3-none-any.whl

Download URL clitint-0.1.4-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de073bd16c5c9263a9cde28f59c32f36a45e4b92a2dd7fd11f5fe038a1a6b4fa
BLAKE2b-256 checksum
How to use checksums
93c18e8396174683c3b5a44d99f5548ea73f29fd446dd3dd59fcd57d4946090b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for clitint 0.1.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page