ANSI API Reference¶
Auto-generated API documentation for the ansi module.
ansi
¶
ANSI escape code primitives for terminal styling.
Zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Provides named colors, text attributes, 256-color and 24-bit true-color support, terminal capability detection, and utility helpers for stripping escape sequences and measuring visible text width.
Quick styling::
from ansi import style
print(style("Error!", fg="red", bold=True))
print(style("Success", fg="green", bg="black", italic=True))
Programmatic color construction::
from ansi import fg, bg, BOLD, RESET
print(f"{BOLD}{fg('blue')}hello{RESET}")
print(f"{fg('#ff8800')}orange text{RESET}")
print(f"{fg(214)}256-color{RESET}")
print(f"{bg(55)}{fg('white')}white on purple{RESET}")
Terminal detection::
from ansi import supports_color, color_depth
if supports_color():
depth = color_depth() # 16, 256, or 16777216
fg(color)
¶
Return an ANSI escape for the given foreground color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str | int | tuple[int, int, int]
|
A color name ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
An ANSI escape string. |
Example::
print(fg("red") + "error" + RESET)
print(fg("#00ff00") + "green" + RESET)
print(fg(214) + "orange" + RESET)
print(fg((100, 200, 50)) + "custom" + RESET)
Source code in ansi/ansi.py
bg(color)
¶
Return an ANSI escape for the given background color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str | int | tuple[int, int, int]
|
Same formats as :func: |
required |
Returns:
| Type | Description |
|---|---|
str
|
An ANSI escape string. |
style(text, *, fg=None, bg=None, bold=False, dim=False, italic=False, underline=False, strikethrough=False, reverse=False, reset=True)
¶
Wrap text with ANSI escape codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The string to style. |
required |
fg
|
str | int | tuple[int, int, int] | None
|
Foreground color (name, hex, 256-index, or RGB tuple). |
None
|
bg
|
str | int | tuple[int, int, int] | None
|
Background color (same formats as fg). |
None
|
bold
|
bool
|
Apply bold weight. |
False
|
dim
|
bool
|
Apply dim/faint rendering. |
False
|
italic
|
bool
|
Apply italic style. |
False
|
underline
|
bool
|
Apply underline decoration. |
False
|
strikethrough
|
bool
|
Apply strikethrough decoration. |
False
|
reverse
|
bool
|
Swap foreground and background. |
False
|
reset
|
bool
|
Append a |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The styled string. |
Example::
style("Error!", fg="red", bold=True)
style("note", fg="cyan", italic=True)
style("warn", fg="yellow", bg="black", underline=True)
Source code in ansi/ansi.py
supports_color(stream=None)
¶
Check if the output stream supports ANSI color codes.
Respects the NO_COLOR environment variable
(see <https://no-color.org/>_) and the FORCE_COLOR variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Any
|
The output stream to check. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the stream is a color-capable terminal. |
Source code in ansi/ansi.py
color_depth(stream=None)
¶
Detect the color depth of the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream
|
Any
|
The output stream to check. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
int
|
|
int
|
|
int
|
|
int
|
|
Source code in ansi/ansi.py
strip_ansi(text)
¶
Remove all ANSI escape sequences from text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string potentially containing ANSI escapes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The plain text without escape sequences. |
Example::
strip_ansi("\033[1m\033[31mhello\033[0m") # "hello"
Source code in ansi/ansi.py
visible_len(text)
¶
Return the visible character count of text, ignoring ANSI escapes.
Useful for aligning columns when text contains color codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A string potentially containing ANSI escapes. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The number of visible characters. |
Source code in ansi/ansi.py
cursor_move(n=1, direction='up')
¶
Return an ANSI escape to move the cursor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of positions to move. |
1
|
direction
|
str
|
One of |
'up'
|
Returns:
| Type | Description |
|---|---|
str
|
An ANSI escape string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If direction is not recognised. |