ansi_stdio
Tells Python that this is a module dir
ansi_stdio.loader
load
def load(path: Path)
Loads a buffer-like object from a file.
ansi_stdio.loader.ansi
load
def load(path)
Loads ANSI data from a file
ansi_stdio.loader.asciinema
load
def load(path)
Loads asciinema data from a file
ansi_stdio.utils
ansi_stdio.utils.fonts
ansi_stdio.cli.quantize
Terminal output quantizer - captures terminal output at specified frame rate.
Usage: ./quantize.py [options] script.py
Examples:
./quantize.py –fps 2 my_script.py ./quantize.py –width 120 –height 40 –fps 5 my_script.py
parse_arguments
def parse_arguments()
Parse command line arguments.
quantize_output
def quantize_output(script: str,
width: Optional[int] = None,
height: Optional[int] = None,
fps: float = 1.0)
Capture and quantize terminal output from a Python script.
Arguments:
script
str - Path to the Python script to runwidth
int, optional - Terminal widthheight
int, optional - Terminal heightfps
float, optional - Output frames per second
main
def main()
Main entry point for the terminal quantizer.
ansi_stdio.cli
ansi_stdio.cli.fonts
List available monospace fonts.
Usage: monospace-fonts [–print=FIELD] [–format=FORMAT]
Options: –print FIELD What to print. Can be ‘name’, ‘path’, or ‘all’ [default: name] –format FORMAT Output format. Can be ‘text’, ‘json’, or ‘csv’ [default: text] -h, –help Show this help message
main
def main()
Entry point for the CLI script.
print_text_output
def print_text_output(fonts, print_field)
Print fonts in text format.
print_json_output
def print_json_output(fonts, print_field)
Print fonts in JSON format.
print_csv_output
def print_csv_output(fonts, print_field)
Print fonts in CSV format.
ansi_stdio.core
ansi_stdio.core.clock
Clock Objects
class Clock()
Base class for clocks.
A clock represents a time transformation that can be applied to animations. Each clock can have a parent clock, creating a hierarchy of time transformations.
__init__
def __init__(parent=None)
Initialize the clock.
Arguments:
parent
- Optional parent clock that feeds time to this clock
time
@property
def time()
Get the current time, considering parent’s time and local adjustments.
Returns:
Current transformed time in seconds
time
@time.setter
def time(value)
Set the current time by adjusting the skew from parent.
Arguments:
value
- The time to set
parent_time
@property
def parent_time()
Get the time of the parent clock, or system time if no parent.
Returns:
Parent’s time or system time in seconds
pause
def pause()
Pause the clock, freezing its time.
resume
def resume()
Resume the clock, continuing from where it was paused. Adjusts the skew to maintain the exact time where it was paused.
ansi_stdio.core.saved
Param Objects
@dataclass(slots=True)
class Param()
Represents a parameter.
Params Objects
@dataclass(slots=True)
class Params()
Holds kwargs and constructor function for a class.
Saved Objects
class Saved()
A class that can be serialized and deserialized.
arguments
def arguments() -> dict[str, Any]
Get the arguments needed to construct this object. Filters out any arguments that are the same as the default value.
save
def save() -> dict
Convert the instance to a dictionary.
ansi_stdio.core.versioned
Versioned Objects
class Versioned()
Inherit this class to store a version number on each change.
change
def change()
Call this if you changed something and need to blow caches
version
@property
def version()
Return the version number of this object.
__hash__
def __hash__() -> int
Versionable objects are cacheable ones
changes
def changes(method)
Decorate methods with this if they make changes to the object
waits
def waits(method)
Decorate methods with this if they need to wait for changes
ansi_stdio.core.box
Box Objects
class Box()
Represents a rectangular area on the screen.
__init__
def __init__(min_x=0, min_y=0, max_x=0, max_y=0)
Initialize the box with the given coordinates.
reset
def reset()
Reset the box to the origin.
__add__
def __add__(other)
Combine two boxes into a new box that encompasses both.
__and__
def __and__(other: "Box") -> "Box"
Intersect two box to find the overlapping area.
__iand__
def __iand__(other: "Box") -> "Box"
Crop the box to the overlapping area.
__bool__
def __bool__()
True if the box has a non-zero area.
__len__
def __len__()
Return the area of the box.
__contains__
def __contains__(item)
Check if the given item is contained within the box.
update
def update(x, y)
Update the box to include the given coordinates.
contains
def contains(x=None, y=None)
Check if the given coordinates are within the box. If x or y is None, it will not be checked.
width
@property
def width()
Width of the bounding box.
height
@property
def height()
Height of the bounding box.
ansi_stdio.terminal.info
get_terminal_size
def get_terminal_size(default_width: int = 80,
default_height: int = 24) -> Tuple[int, int]
Detect terminal size using multiple fallback methods.
Arguments:
default_width
int, optional - Default terminal width. Defaults to 80.default_height
int, optional - Default terminal height. Defaults to 24.
Returns:
Tuple[int, int]: A tuple of (width, height) representing terminal dimensions.
ansi_stdio.terminal
ansi_stdio.terminal.capture
capture_terminal
def capture_terminal(
program: str,
width: Optional[int] = None,
height: Optional[int] = None,
buffer_size: int = 4096,
display_callback: Optional[Callable[[pyte.Screen], None]] = None
) -> pyte.Screen
Capture terminal output for a given program with flexible processing.
Arguments:
program
str - Command to run in the terminalwidth
int, optional - Terminal width. Defaults to detected width.height
int, optional - Terminal height. Defaults to detected height.buffer_size
int, optional - Size of read buffer. Defaults to 4096.display_callback
Callable, optional - Function to process screen state. Receives the pyte Screen object for custom handling.
Returns:
pyte.Screen
- The final screen state after program execution
ansi_stdio.terminal.render
Pyte screen renderer for ANSI stdio. Converts pyte screen state to formatted strings with ANSI escape sequences.
format_char
def format_char(char)
Format a single character with its attributes.
Arguments:
char
- A pyte character with attributes
Returns:
str
- The formatted character with ANSI codes
format_line
def format_line(row, width)
Format a single line of the screen.
Arguments:
row
- A dictionary of column -> character mappingswidth
- The width of the screen
Returns:
str
- The formatted line
render_screen
def render_screen(screen, dirty_only=False, clear_dirty=True)
Convert a pyte screen to a dictionary of formatted strings.
Arguments:
screen
- A pyte.Screen instancedirty_only
- If True, only render dirty linesclear_dirty
- Whether to clear the dirty set after processing
Returns:
dict
- A dictionary mapping line numbers to formatted strings
display_screen
def display_screen(screen, dirty_only=False, clear_dirty=True)
Display a pyte screen using ANSI escape sequences.
Arguments:
screen
- A pyte.Screen instancedirty_only
- If True, only display dirty linesclear_dirty
- Whether to clear the dirty set after processing
ansi_stdio.__main__
Entry point for the package
ansi_stdio.buffer.animation
Animation Objects
class Animation(Versioned)
An animation that we can render somewhere.
ansi_stdio.buffer
ansi_stdio.buffer.buffer
Buffer Objects
class Buffer(Versioned)
A 2D sparse grid of rich.Segment objects.
__init__
def __init__()
Initialize the buffer as a sparse structure.
__getitem__
def __getitem__(coords)
Get the item at the given coordinates.
Arguments:
coords
- A tuple of (x, y) coordinates
Returns:
The Segment at those coordinates or None if empty
__setitem__
@changes
def __setitem__(coords, segment)
Set a segment at the given coordinates.
Arguments:
coords
- A tuple of (x, y) coordinatessegment
- The Segment to place at these coordinates
__iadd__
@changes
def __iadd__(other) -> "Buffer"
Merge another buffer into this one.
__add__
def __add__(other) -> "Buffer"
Create a new buffer by merging this buffer with another.
__and__
def __and__(box: Box) -> "Buffer"
Crop the buffer to the given box. Returns a newly allocated buffer.
__iand__
@changes
def __iand__(box: Box) -> "Buffer"
Crop the buffer to the given box. This modifies the buffer in place.
__sub__
def __sub__(other: "Buffer") -> "Buffer"
Create a new buffer representing the difference: self - other. Only includes cells in self that differ from other.
__isub__
@changes
def __isub__(other: "Buffer") -> "Buffer"
Remove from self any cells that are identical in other. Modifies the buffer in-place.
__len__
def __len__()
Get the number of cells set in the buffer.
set
@changes
def set(x, y, segment)
Set cell(s) starting at given coordinates with a Segment. Handles multi-character segments by writing each character in sequence.
Arguments:
x
- Starting X coordinatey
- Y coordinatesegment
- Rich Segment object to place at this position
copy
def copy()
Create a deep copy of this buffer.
Returns:
A new Buffer instance with the same content
recalculate
@changes
def recalculate(size: bool = True, box: bool = True)
Recalculate the size and box
ansi_stdio.buffer.frame
Frame Objects
class Frame(Versioned)
A frame of animation, containing a buffer.