click_extra.capture.terminal module¶
Rebuild the screen a terminal shows from the stream a command writes at it.
A command prints text and control sequences, never a picture. The terminal makes
the picture: a carriage return sends the cursor back over its row, a cursor
position sends it anywhere on the screen, an erase blanks cells in the
background color in force, and a full-screen program draws on a second screen
that hides the first. TerminalScreen is that terminal, held in memory,
so a recording can read every screen off it.
Note
The vocabulary is xterm’s, as ncurses drives it for TERM=xterm-256color: the
terminal RECORDING_TERM names to every
recorded command. A spinner or a progress bar uses a handful of it: a return, an
erase, a newline. A full-screen program uses much more: cursor addressing,
scroll regions, insertion and deletion of cells and rows, the line-drawing
character set and the alternate screen. Sequences that change nothing on screen
(keypad and mouse modes, window titles, hyperlinks, the queries a program sends
its terminal) are read whole and dropped, so their bytes never reach a cell.
Todo
Answer the cursor position report (CSI 6 n) and the device attributes query
(CSI c) once a recorded program blocks on either. No reply reaches the program
today, so one waiting for it waits until it gives up.
- click_extra.capture.terminal.DEC_SPECIAL_GRAPHICS: Final[dict[str, str]] = {'_': ' ', '`': '◆', 'a': '▒', 'b': '␉', 'c': '␌', 'd': '␍', 'e': '␊', 'f': '°', 'g': '±', 'h': '', 'i': '␋', 'j': '┘', 'k': '┐', 'l': '┌', 'm': '└', 'n': '┼', 'o': '⎺', 'p': '⎻', 'q': '─', 'r': '⎼', 's': '⎽', 't': '├', 'u': '┤', 'v': '┴', 'w': '┬', 'x': '│', 'y': '≤', 'z': '≥', '{': 'π', '|': '≠', '}': '£', '~': '·'}¶
Glyphs the DEC Special Graphics character set draws in place of ASCII.
A program selects the set with
ESC ( 0and then writesqfor a horizontal rule,xfor a vertical one andlfor a corner.ncursesdoes so for its line-drawing routines, likebox()and theACS_*characters. The screen keeps the Unicode glyph, which the renderers draw as the shape it names.
- click_extra.capture.terminal.PENDING_LIMIT: Final = 4096¶
Characters an unfinished escape sequence may hold while its tail is awaited.
A sequence split across two reads waits for its tail, and each read re-reads what waits. A string longer than this is skipped as it arrives instead, up to the terminator closing it.
- click_extra.capture.terminal.TAB_WIDTH: Final = 8¶
Columns between two tab stops, until a program sets its own.
- class click_extra.capture.terminal.TerminalScreen(columns=None, rows=None, *, onlcr=True)[source]¶
Bases:
objectThe screen a terminal shows, rebuilt from what was written at it.
The screen is a grid of cells, each holding one grapheme and the style it was written in. A write lands on the cells under the cursor and leaves the others alone, so a redraw shorter than the row it covers keeps the row’s tail, the way a terminal does. An erase blanks cells in the background color in force, which is how a full-screen program paints a bar across the screen.
Rows the cursor leaves through the top of a full screen stay above it, as a terminal’s history does, and stay in the picture:
displayholds every row a command printed, not only the last screenful. A full-screen program draws on the alternate screen instead, which keeps no history and holds exactly the screen’s height. Leaving it brings the first screen back as it was.Note
A screen of unknown size is boundless: a row never wraps, and a newline on the last row adds a row below it. That is the screen an in-process recording has, which nothing tells how big its terminal is. The renderers fold a long row at the capture’s own width later.
Start on an empty screen, with the cursor in its top-left corner.
- Parameters:
columns (
int|None) – width of the terminal, in cells.Nonenever wraps.rows (
int|None) – height of the terminal, in cells.Nonenever scrolls.onlcr (
bool) – return the cursor to the start of its row on every newline, as theONLCRflag of a terminal driver does on the way to the screen. A pseudo-terminal applies the flag itself, so a stream read from one already carries a return before each newline that wants one, and a full-screen program clears the flag to move straight down. A stream written in-process went through no driver at all.
- Raises:
ValueError – when a stated size is not positive.
- width¶
Width of the terminal, in cells, or
Nonewhen unbounded.
- height¶
Height of the terminal, in cells, or
Nonewhen unbounded.
- property cursor: tuple[int, int]¶
Row and column the cursor stands on, both counted from zero.
The row counts from the top of
rows, history included.
- property rows: list[str]¶
Every row of the picture, each as styled text.
On the first screen, the history and every row down to the last one holding anything, or the cursor’s when it stands lower: output closing on a newline leaves an empty last row, where the cursor waits. On the alternate screen, exactly its height.
- property synchronized: bool¶
Whether the program is in the middle of a synchronized update.
A program opens one with
CSI ? 2026 hand closes it withCSI ? 2026 l, asking its terminal to show nothing it drew in between until the close.
- feed(text)[source]¶
Replay what a command wrote, moving the screen to what it now shows.
Safe to call with the stream cut anywhere: an escape sequence straddling two calls is held until the rest of it arrives. A sequence still unfinished when the stream ends never reaches the screen, having named nothing to draw.