Skip to main content

Terminal color styling: compose ANSI escapes for foreground/background and text attributes.

Project description

colortty

A tiny Python helper to build ANSI escape sequences for terminal styling. It composes colors and text attributes and returns the styled string. Output/printing is up to you.

Requirements

  • Python 3.11+

Install

  • From PyPI (once published):
pip install colortty
  • From source (in this repository):
pip install .

Usage

Two common ways to use it:

  • Build and immediately style a string:
from src.colortty import colortty, ColorTTY

print(
    colortty("Hello")
    .set(ColorTTY.Color.red())
    .set(ColorTTY.BackgroundColor.blue())
    .make()
)
# -> "\033[31;44mHello\033[0m"
  • Build a reusable style and apply it later:
from src.colortty import colortty, ColorTTY

red_text = colortty().set(ColorTTY.Color.red())
print(red_text.make("Error:"))
print(red_text.set(ColorTTY.BackgroundColor.white(lighter=True)).make("Critical"))

More examples

  • Bright 8‑color variant:
colortty("Bright red").set(ColorTTY.Color.red(lighter=True)).make()
  • 256‑color index and truecolor (RGB):
colortty("Indexed").set(ColorTTY.Color.color_256(208)).make()
colortty("RGB").set(ColorTTY.Color.color(255, 128, 64)).make()
  • Text attributes and background:
from src.colortty import bold, underline, sparking, inverse, invisible

colortty("Styled").set(bold()).set(underline()).set(sparking()).set(inverse()).set(invisible(False)).make()
colortty("Warn").set(ColorTTY.BackgroundColor.yellow(lighter=True)).set(bold()).make()

API (brief)

  • colortty(s: str | None = None) -> Statement
  • Statement.set(step) -> Statement
  • Statement.make(s: str | None = None) -> str
  • ColorTTY.Color: .black/.red/.green/.yellow/.blue/.magenta/.cyan/.white(lighter=False), .color_256(index), .color(r,g,b)
  • ColorTTY.BackgroundColor: same as ColorTTY.Color
  • Attributes: bold(enable=True), underline(enable=True), sparking(enable=True), inverse(enable=True), invisible(enable=True)

License

MIT. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colortty-0.1.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

colortty-0.1.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file colortty-0.1.1.tar.gz.

File metadata

  • Download URL: colortty-0.1.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for colortty-0.1.1.tar.gz
Algorithm Hash digest
SHA256 289ebc22ff1e78a98814bf7935b9939d41e5286db4053025531b949a8ba62fed
MD5 fef1b46479d48b6a7e2938efb459b125
BLAKE2b-256 58312685338b0f8d382073e1c9d5841467018662f004d9f40c4b4d58cd11fb3b

See more details on using hashes here.

File details

Details for the file colortty-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: colortty-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for colortty-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c505dc6d3cdfa838ab6b533894a91ac6d6af63530ac2d167565fdb3fbc7c250
MD5 b0203ba362a61ee298279d19f3a39c5b
BLAKE2b-256 7ef6811286d77dd9bd88986fa7e8dd0925ac50f79b5f4083e5ec626034748115

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page