Sty
Sty is a Python library that provides a simple, customizable, and performant string styling markup for terminal output, decoupled from specific color palettes and terminal implementations. It supports 3/4bit, 8bit, and 24bit (truecolor/RGB) colors, as well as effects like bold, italic, and underline. Currently at version 1.0.6, Sty follows semantic versioning and aims to maintain backward compatibility for 1.x.x releases after 1.0.0. It has no external dependencies and is actively maintained.
Common errors
-
AttributeError: 'Register' object has no attribute 'set_style'
cause Attempting to use the `set_style()` method, which was removed in `sty` v1.0.0-rc.1.fixInstead of `register.set_style(...)`, directly assign `Rule` objects to define custom styles, e.g., `fg.custom_color = Rule(Render.rgb_fg, 100, 200, 50)`. -
AttributeError: 'Reset' object has no attribute 'rs'
cause Attempting to use the `sty.rs.rs` attribute after it was renamed to `sty.rs.ef` in `sty` v1.0.0-rc.2.fixChange `sty.rs.rs` to `sty.rs.ef` or use `rs.all` to reset all styles. For example, `print(fg.red + 'text' + rs.ef)` or `print(fg.red + 'text' + rs.all)`. -
ModuleNotFoundError: No module named 'sty.lib'
cause Trying to import internal modules like `sty.lib` directly. These are not part of the public API and their structure can change.fixAlways import public symbols directly from the `sty` package, e.g., `from sty import fg, bg, ef, rs`. The `sty.lib` path is for internal use.
Warnings
- breaking Methods `.set_style()` and `.get_style()` were removed in v1.0.0-rc.1. They were deprecated in v1.0.0-beta.12. Using these methods in v1.0.0 or later will result in an AttributeError.
- breaking The reset register attribute `sty.rs.rs` was renamed to `sty.rs.ef` in v1.0.0-rc.2 due to a typo fix. Code using the old `sty.rs.rs` will fail with an AttributeError in subsequent versions.
- breaking The grey color palette definitions were changed in v1.0.0-rc.0. If your application relies on specific grey values from pre-release versions, the visual output might differ.
- gotcha While `import sty` and then referencing `sty.fg`, `sty.bg`, etc., technically works, the idiomatic and recommended approach for convenience and better performance is to directly import the register objects: `from sty import fg, bg, ef, rs`.
Install
-
pip install sty
Imports
- fg, bg, ef, rs
from sty import fg, bg, ef, rs
Quickstart
from sty import fg, bg, ef, rs # Basic colors print(fg.red + 'This is red text!' + fg.rs) print(bg.blue + 'This has a blue background!' + bg.rs) # Effects print(ef.bold + 'This is bold text' + ef.rs) print(ef.italic + 'This is italic text' + ef.rs) # 8-bit colors print(fg(201) + 'This is pink text using 8bit colors' + fg.rs) # 24-bit (RGB) colors print(fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs) # Combine styles print(fg.green + bg.yellow + ef.bold + 'Green text on yellow background, bold!' + rs.all)