click_extra.capture.svg module

Draw captured terminal text as an SVG picture of a terminal window.

click_extra.capture.screenshot decides what a capture shows, then hands the text, its palette and the resolved Chrome to render_svg(), which lays the text out on a character grid and paints the window around it, one layer at a time: backdrop, shadow, frame, title bar, emphasis bands, text, cursor, credit line and animation.

The window’s geometry lives here too, since every pixel size below is the picture’s: the title bar, the shadow’s blur and offset, the credit line’s size and place, and the cell metrics the grid is measured in. click_extra.capture.html draws the same window and borrows them, so the two documents look like the same terminal. The paints and sizes a Chrome defaults to, like NO_PAINT and OPAQUE, live beside it in click_extra.capture.presets.

click_extra.capture.svg.THold: TypeAlias

Pause an animation takes on its last frame, see AUTO_HOLD.

click_extra.capture.svg.TITLEBAR_HEIGHT = 40

Height, in pixels, of the strip a title and its buttons sit in.

The padding a renderer leaves above the text, which is what a window’s chrome occupies. Restated here because a capture wearing neither decoration nor caption drops the strip, and one drawn as a real terminal paints it.

click_extra.capture.svg.SHADOW_BLUR = 6

Standard deviation, in pixels, of the drop shadow’s blur.

click_extra.capture.svg.SHADOW_OFFSET = 3

Downward offset, in pixels, of the drop shadow.

click_extra.capture.svg.WINDOW_PADDING = 8

Pixels every window keeps between its frame and its text, on three sides.

The fourth is the top, where TITLEBAR_HEIGHT answers instead. This is the window’s own breathing room, before the padding a capture may ask for on top.

click_extra.capture.svg.WINDOW_INSET = 1

Pixels between the image’s edge and the window’s frame.

A stroke straddles the shape it outlines, so a frame drawn flush with the viewBox loses its outer half to the crop. Inset by more than that half and the whole line shows.

click_extra.capture.svg.CSS_SIDE_ANGLES = {'to bottom': 180.0, 'to bottom left': 225.0, 'to bottom right': 135.0, 'to left': 270.0, 'to left bottom': 225.0, 'to left top': 315.0, 'to right': 90.0, 'to right bottom': 135.0, 'to right top': 45.0, 'to top': 0.0, 'to top left': 315.0, 'to top right': 45.0}

Angle each CSS side keyword names, in degrees clockwise from to top.

to bottom is what a gradient opening with no direction at all means, which is why it doubles as the default. See gradient_svg().

click_extra.capture.svg.TITLE_SIZE = 18

Height, in pixels, of the caption drawn in a window’s title bar.

click_extra.capture.svg.WATERMARK_SIZE = 13

Height, in pixels, of the credit line’s glyphs.

Below the terminal’s own text, since a mark competing with the screen it credits is a mark in the way.

click_extra.capture.svg.WATERMARK_INSET = 12

Pixels between the credit line and the image’s bottom-right corner.

It is drawn in the margin, the one band of a capture that carries nothing else. A capture shot with margin=0 has no such band, and the line lands on the window’s own corner instead of beside it.

click_extra.capture.svg.WATERMARK_URL = 'https://kdeldycke.github.io/click-extra/screenshots.html'

Page the credit line points at, for a reader holding only the image.

A capture travels away from whatever explained it, so the mark carries the way back. Only PACKAGE_NAME is linked, and only where the line still names it: a project crediting itself instead has no click-extra left to point at, and gets no link rather than one pointing somewhere it did not ask for.

Note

The link answers where the file is interactive: opened on its own, inlined into a page, or embedded through <object>. An <img> draws an SVG as a picture and no click reaches inside one, which is how this documentation embeds its own captures.

click_extra.capture.svg.CELL_HEIGHT = 20.0

Height of one glyph cell, in pixels, which is also the text’s font size.

click_extra.capture.svg.FONT_ASPECT_RATIO = 0.61

Width-to-height ratio of the font a capture is laid out for.

Fira Code’s, the first family CAPTURE_FONT_STACK asks for. Every monospaced fallback behind it is close enough that the grid holds, and textLength pins each run to its columns for the ones that are not.

click_extra.capture.svg.CELL_WIDTH = 12.2

Width of one glyph cell, in pixels. One character of a monospaced terminal.

click_extra.capture.svg.LINE_HEIGHT = 24.4

Vertical distance between two consecutive text baselines, in pixels.

click_extra.capture.svg.CELL_BLEED = 0.25

Pixels a cell’s background is grown by, past the line it belongs to.

Two rectangles meeting on an exact boundary leave a hairline of page showing through when a renderer rounds their edges to different pixels. Overlapping them slightly is what closes that seam, and is invisible because the color painted twice is the same color.

click_extra.capture.svg.CELL_TOP_INSET = 1.5

Pixels between a line’s top edge and the cell backgrounds drawn on it.

A glyph does not fill its line box: the leading sits above the tallest letter. Starting the paint just under that keeps a highlighted run reading as one block of color rather than as a band taller than the text it marks.

click_extra.capture.svg.CURSOR_THICKNESS = 2.0

Pixels a cursor is drawn thick, when it is a line rather than a cell.

Both BAR and UNDERLINE are the same line turned a quarter, so one number states both. Thin enough to read as a cursor beside a glyph, thick enough to survive a renderer rounding it to whole pixels.

click_extra.capture.svg.DEFAULT_CURSOR_SHAPE = CursorShape.BLOCK

Shape a cursor takes when nothing names one.

What every terminal here but Windows draws, and the shape a reader recognizes as a terminal cursor rather than as a text caret. A capture given a preset takes that terminal’s own shape instead, see cursor.

click_extra.capture.svg.BLOCK_ELEMENTS = {'▀': ((0, 0, 8, 4),), '▁': ((0, 7, 8, 8),), '▂': ((0, 6, 8, 8),), '▃': ((0, 5, 8, 8),), '▄': ((0, 4, 8, 8),), '▅': ((0, 3, 8, 8),), '▆': ((0, 2, 8, 8),), '▇': ((0, 1, 8, 8),), '█': ((0, 0, 8, 8),), '▉': ((0, 0, 7, 8),), '▊': ((0, 0, 6, 8),), '▋': ((0, 0, 5, 8),), '▌': ((0, 0, 4, 8),), '▍': ((0, 0, 3, 8),), '▎': ((0, 0, 2, 8),), '▏': ((0, 0, 1, 8),), '▐': ((4, 0, 8, 8),), '░': ((0, 0, 8, 8),), '▒': ((0, 0, 8, 8),), '▓': ((0, 0, 8, 8),), '▔': ((0, 0, 8, 1),), '▕': ((7, 0, 8, 8),), '▖': ((0, 4, 4, 8),), '▗': ((4, 4, 8, 8),), '▘': ((0, 0, 4, 4),), '▙': ((0, 0, 4, 8), (4, 4, 8, 8)), '▚': ((0, 0, 4, 4), (4, 4, 8, 8)), '▛': ((0, 0, 8, 4), (0, 4, 4, 8)), '▜': ((0, 0, 8, 4), (4, 4, 8, 8)), '▝': ((4, 0, 8, 4),), '▞': ((4, 0, 8, 4), (0, 4, 4, 8)), '▟': ((4, 0, 8, 4), (0, 4, 8, 8))}

Rectangles each Block Elements character fills on its cell.

Each rectangle is (left, top, right, bottom), counted in eighths of the cell from its top-left corner: ▁, the lower one eighth, is the finest step these characters take. block_svg() draws the rectangles in place of the glyphs, and block_css() does in an HTML capture.

A font draws these characters to its own height, not to the row’s. SF Mono 15.0d5e1 draws █ exactly one em tall and Menlo 22.0d2e1 1.02 em, while a capture’s rows are LINE_HEIGHT apart, 1.22 em. Set as glyphs, two stacked rows of block art leave a band of 4.0 to 4.4 pixels between them, and stretching the glyphs would take a factor and an offset per font, which a capture cannot know. A rectangle fills its share of the cell in every renderer, whatever font the reader has.

Terminals draw these characters themselves rather than take them from the font, for the same result: WezTerm with custom_block_glyphs and Alacritty with builtin_box_drawing, both on by default, and VS Code with terminal.integrated.customGlyphs under GPU acceleration.

The characters stay in the text, unpainted, see HIDDEN_GLYPH_ATTRIBUTES. A decoration on one, like an underline, is hidden with it.

click_extra.capture.svg.SHADE_DENSITY = {'░': 0.25, '▒': 0.5, '▓': 0.75}

How much of a shade character’s cell is ink.

BLOCK_ELEMENTS gives each shade the full cell. Its ink is mixed with the color behind it up front rather than thinned with opacity, for the reason blend() gives, so a shade draws as one flat color.

click_extra.capture.svg.BRAILLE_DOTS = ((0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (0, 3), (1, 3))

Where each Braille dot sits on its cell, as (column, row) on a 2 by 4 grid.

Listed in bit order: bit n of a pattern’s offset from U+2800 raises dot n + 1. The dots number down the left column, then down the right, and the bottom row, dots 7 and 8, comes last: 8-dot Braille extends the 6-dot cell with that row. braille_svg() draws them.

The rows split the line into four and the columns split the cell into two, so the dots keep one pitch across cells, and stacked rows of Braille plot one even grid.

Menlo, where a stock macOS falls through CAPTURE_FONT_STACK, has no Braille. Its stand-in, Apple Symbols 22.0d3e1, draws every pattern 0.684 em wide, 1.5 pixels wider than its column at a capture’s size. Drawn as dots, a pattern fits its cell whatever font the reader has. Terminals draw Braille themselves too: WezTerm with custom_block_glyphs and VS Code with terminal.integrated.customGlyphs.

click_extra.capture.svg.BRAILLE_DOT_RADIUS = 0.25

Radius of a Braille dot, as a share of the pitch between two dots.

A quarter, so a dot is half as wide as the pitch: the proportion Apple Braille 22.0d3e1 draws, a dot 248 units across on a pitch of 488.

click_extra.capture.svg.BOX_ARMS = {'─': (0, 1, 0, 1), '━': (0, 2, 0, 2), '│': (1, 0, 1, 0), '┃': (2, 0, 2, 0), '┌': (0, 1, 1, 0), '┍': (0, 2, 1, 0), '┎': (0, 1, 2, 0), '┏': (0, 2, 2, 0), '┐': (0, 0, 1, 1), '┑': (0, 0, 1, 2), '┒': (0, 0, 2, 1), '┓': (0, 0, 2, 2), '└': (1, 1, 0, 0), '┕': (1, 2, 0, 0), '┖': (2, 1, 0, 0), '┗': (2, 2, 0, 0), '┘': (1, 0, 0, 1), '┙': (1, 0, 0, 2), '┚': (2, 0, 0, 1), '┛': (2, 0, 0, 2), '├': (1, 1, 1, 0), '┝': (1, 2, 1, 0), '┞': (2, 1, 1, 0), '┟': (1, 1, 2, 0), '┠': (2, 1, 2, 0), '┡': (2, 2, 1, 0), '┢': (1, 2, 2, 0), '┣': (2, 2, 2, 0), '┤': (1, 0, 1, 1), '┥': (1, 0, 1, 2), '┦': (2, 0, 1, 1), '┧': (1, 0, 2, 1), '┨': (2, 0, 2, 1), '┩': (2, 0, 1, 2), '┪': (1, 0, 2, 2), '┫': (2, 0, 2, 2), '┬': (0, 1, 1, 1), '┭': (0, 1, 1, 2), '┮': (0, 2, 1, 1), '┯': (0, 2, 1, 2), '┰': (0, 1, 2, 1), '┱': (0, 1, 2, 2), '┲': (0, 2, 2, 1), '┳': (0, 2, 2, 2), '┴': (1, 1, 0, 1), '┵': (1, 1, 0, 2), '┶': (1, 2, 0, 1), '┷': (1, 2, 0, 2), '┸': (2, 1, 0, 1), '┹': (2, 1, 0, 2), '┺': (2, 2, 0, 1), '┻': (2, 2, 0, 2), '┼': (1, 1, 1, 1), '┽': (1, 1, 1, 2), '┾': (1, 2, 1, 1), '┿': (1, 2, 1, 2), '╀': (2, 1, 1, 1), '╁': (1, 1, 2, 1), '╂': (2, 1, 2, 1), '╃': (2, 1, 1, 2), '╄': (2, 2, 1, 1), '╅': (1, 1, 2, 2), '╆': (1, 2, 2, 1), '╇': (2, 2, 1, 2), '╈': (1, 2, 2, 2), '╉': (2, 1, 2, 2), '╊': (2, 2, 2, 1), '╋': (2, 2, 2, 2), '═': (0, 3, 0, 3), '║': (3, 0, 3, 0), '╒': (0, 3, 1, 0), '╓': (0, 1, 3, 0), '╔': (0, 3, 3, 0), '╕': (0, 0, 1, 3), '╖': (0, 0, 3, 1), '╗': (0, 0, 3, 3), '╘': (1, 3, 0, 0), '╙': (3, 1, 0, 0), '╚': (3, 3, 0, 0), '╛': (1, 0, 0, 3), '╜': (3, 0, 0, 1), '╝': (3, 0, 0, 3), '╞': (1, 3, 1, 0), '╟': (3, 1, 3, 0), '╠': (3, 3, 3, 0), '╡': (1, 0, 1, 3), '╢': (3, 0, 3, 1), '╣': (3, 0, 3, 3), '╤': (0, 3, 1, 3), '╥': (0, 1, 3, 1), '╦': (0, 3, 3, 3), '╧': (1, 3, 0, 3), '╨': (3, 1, 0, 1), '╩': (3, 3, 0, 3), '╪': (1, 3, 1, 3), '╫': (3, 1, 3, 1), '╬': (3, 3, 3, 3), '╴': (0, 0, 0, 1), '╵': (1, 0, 0, 0), '╶': (0, 1, 0, 0), '╷': (0, 0, 1, 0), '╸': (0, 0, 0, 2), '╹': (2, 0, 0, 0), '╺': (0, 2, 0, 0), '╻': (0, 0, 2, 0), '╼': (0, 2, 0, 1), '╽': (1, 0, 2, 0), '╾': (0, 1, 0, 2), '╿': (2, 0, 1, 0)}

Arms of each Box Drawing line, as their (up, right, down, left) weights.

0 draws no arm, 1 a light one, 2 a heavy one and 3 a double one: the weights the character’s Unicode name lists. box_svg() draws each arm from its cell’s edge toward the middle, and works out where two arms’ strokes meet.

As glyphs, two of these meet wherever the font and the renderer place them, a fraction of a pixel off the grid, and a table’s rule shows a seam or a kink at every join: rich#2536, which its maintainer answers takes drawn shapes. Terminals draw these characters themselves: WezTerm with custom_block_glyphs, Alacritty with builtin_box_drawing and VS Code with terminal.integrated.customGlyphs.

click_extra.capture.svg.BOX_STROKES = {1: ((-1, 1),), 2: ((-2, 2),), 3: ((-3, -1), (1, 3))}

Where the strokes of a light, heavy and double arm sit across it.

Counted from the cell’s middle line, in sixteenths of its width or thirty-seconds of its height: one unit on both axes, a row being twice as tall as a column is wide. A light stroke is two units thick, 0.076 em, close to Menlo 22.0d2e1’s 0.078 em. A heavy one is twice that, and a double one is two light strokes a light stroke apart, as Menlo draws them.

click_extra.capture.svg.BOX_DASHES = {'┄': (False, 1, 3), '┅': (False, 2, 3), '┆': (True, 1, 3), '┇': (True, 2, 3), '┈': (False, 1, 4), '┉': (False, 2, 4), '┊': (True, 1, 4), '┋': (True, 2, 4), '╌': (False, 1, 2), '╍': (False, 2, 2), '╎': (True, 1, 2), '╏': (True, 2, 2)}

Dashed Box Drawing lines, as whether each runs down, its weight and its dashes.

A dash covers two thirds of its share of the cell and leaves the rest as a gap split between its two ends, so dashes keep one pitch across cells.

click_extra.capture.svg.BOX_CURVES = {'╭': 'M16,16 A8,8 0 0,0 8,24 L8,32', '╮': 'M0,16 A8,8 0 0,1 8,24 L8,32', '╯': 'M8,0 L8,8 A8,8 0 0,1 0,16', '╰': 'M8,0 L8,8 A8,8 0 0,0 16,16', '╱': 'M16,0 L0,32', '╲': 'M0,0 L16,32', '╳': 'M16,0 L0,32 M0,0 L16,32'}

Arcs and diagonals of Box Drawing, as SVG path data on a 16 by 32 cell.

Drawn as a light stroke, see BOX_STROKES. An arc turns on a radius of half the cell’s width, so it meets the straight arms of its neighbors.

click_extra.capture.svg.DIM_RATIO = 0.4

How far a dim run’s ink is mixed toward the background, see blend().

click_extra.capture.svg.EMPHASIS_RATIO = 0.16

How far an emphasized line’s band is mixed from the background toward the ink.

Mixed rather than stated outright, so one number answers for every chrome: a band a shade lighter than a dark terminal is a shade darker than a light one, and both read as the same emphasis. Far enough to find the line at a glance, near enough to leave its text the thing being read.

click_extra.capture.svg.HIDDEN_FRAME_ATTRIBUTES = ' visibility="hidden" opacity="0"'

How an animated capture hides the frames its still is not made of.

Presentation attributes rather than a stylesheet rule, so a renderer reading no CSS still shows one picture instead of every frame at once. A CSS animation outranks a presentation attribute, so frame_animation_css() restores both together and the two mechanisms never disagree.

Caution

Neither property is redundant. visibility alone satisfies a browser, librsvg and macOS Quick Look’s thumbnailer, but two other readers ignore it and draw every frame on top of the last: a git client’s SVG diff view, and macOS Finder’s preview pane, a different code path from the thumbnailer. Both honor opacity, so each property covers a reader the other misses, and dropping either takes a class of reader with it.

click_extra.capture.svg.HIDDEN_GLYPH_ATTRIBUTES = ' opacity="0"'

How a run drawn as shapes keeps its characters without painting them.

Kept as text, the characters still reach a reader copying the capture’s text, and whatever rebuilds the terminal’s lines from it.

opacity, because every reader honors it, see HIDDEN_FRAME_ATTRIBUTES. fill would lose to the run’s class, which sets it in the stylesheet, and AppKit’s NSImage ignores fill-opacity on text: macOS Finder’s preview pane and a git client’s SVG diff view then draw each glyph over its own shapes.

click_extra.capture.svg.ANIMATION_METADATA_RE = re.compile('<!-- @recording (?P<fields>[^>]*?) -->')

The line an animated capture states its own identity on.

Written beside the @generated line, and read back by whatever has to decide whether a freshly drawn animation is the one already on disk. Holding the answer in the file is what spares that decision from parsing an SVG back into frames.

click_extra.capture.svg.FRAME_TIMING_FUNCTION = 'step-end'

How an animated capture moves between two frames: it does not.

A terminal repaints a whole cell at once, so a capture of one has nothing to interpolate. step-end holds each keyframe’s value until the next one is reached, which turns a percentage ladder into discrete frames and lets every frame state its own window. That is what carries a recording, whose frames each last as long as the terminal held them, on the same machinery as a spinner, whose frames are all the same length.

click_extra.capture.svg.REDUCED_MOTION_QUERY = '@media (prefers-reduced-motion: no-preference)'

Guard every animation rule an animated capture emits sits behind.

click_extra.accessibility counts an endlessly repeating spinner among the things --accessible exists to lower, and an image looping forever on a documentation page is the same imposition on a reader who asked their system for less motion. Outside the guard the first frame stays visible and the rest stay hidden, which is the picture a renderer that reads no CSS animation already gets, so honoring the preference costs a media query and no second code path.

click_extra.capture.svg.AUTO_HOLD: Literal['auto'] = 'auto'

Hold asking for the pause the final screen’s own density decides.

Resolved by auto_hold(): the busier the closing screen, the longer the loop pauses on it before starting over. A fixed number serves an animation whose ending the author has seen; this serves one whose ending changes with every retake, like a recorded command whose closing report grows with what there is to report.

click_extra.capture.svg.AUTO_HOLD_SECONDS_PER_LINE = 0.25

Seconds auto_hold() grants per populated line of the final screen.

A scanning rate rather than a reading one: terminal output is tables and listings, which a reader sweeps at a few lines a second rather than reads word by word.

click_extra.capture.svg.AUTO_HOLD_MIN = 2.0

Shortest pause auto_hold() answers.

Even a one-line outcome deserves a beat before the loop swallows it.

click_extra.capture.svg.AUTO_HOLD_MAX = 30.0

Longest pause auto_hold() answers.

Past this, a looping animation reads as a still that never moves: whoever needs longer than half a minute on one screen wants the still, not the loop.

click_extra.capture.svg.auto_hold(text)[source]

Seconds a reader needs on a closing screen, from how much it shows.

Counts the populated lines of text (ANSI escapes stripped, blank rows ignored) and grants AUTO_HOLD_SECONDS_PER_LINE for each, clamped between AUTO_HOLD_MIN and AUTO_HOLD_MAX. Lines rather than words, because terminal output is mostly tables whose box-drawing rows would drown a word count without adding anything to read.

Parameters:

text (str) – the final frame’s captured text, ANSI escapes included.

Return type:

float

Returns:

the pause, in seconds.

click_extra.capture.svg.parse_pause(value)[source]

Read a pause written as text into seconds, zero included.

Parameters:

value (str) – the text.

Return type:

float

Returns:

the pause, in seconds.

Raises:

ValueError – when the text is not a number, or is negative.

click_extra.capture.svg.parse_hold(value)[source]

Read a hold written as text into seconds, or into auto.

AUTO_HOLD scales the pause to the final frame’s own line count, see auto_hold().

Parameters:

value (str) – the text, a number of seconds or the word auto.

Return type:

float | Literal['auto']

Returns:

the hold.

Raises:

ValueError – when the text is neither, or names a negative pause.

click_extra.capture.svg.blend(color, into, ratio)[source]

Mix color toward into, the way a terminal fades dim text.

SVG has no dim, and thinning the glyphs with opacity would let whatever sits behind the capture show through them. Mixing the two colors up front keeps the text opaque and lands the same shade.

Parameters:
  • color (str) – the color to fade, as #rrggbb.

  • into (str) – the color to fade it toward, usually the background.

  • ratio (float) – how far to go, from 0.0 (unchanged) to 1.0 (into).

Return type:

str

Returns:

the blended color, as #rrggbb.

click_extra.capture.svg.emphasis_paint(palette)[source]

The color an emphasized line’s band is painted, on palette.

EMPHASIS_RATIO of the way from the terminal’s background toward its ink, see blend().

Parameters:

palette (TerminalPalette) – the terminal colors the capture is drawn with.

Return type:

str

Returns:

the band’s color, as #rrggbb.

click_extra.capture.svg.palette_color(color, palette)[source]

Resolve any color a Style carries to a hex string.

The 16 named and indexed ANSI slots are not colors, they are names: a terminal decides what its red looks like, and a capture has no terminal, so they answer to palette. Every other form a style can carry (a 24-bit triplet, a 256-cube index, a hex string) already states its own color and passes through.

Parameters:
  • color (object) – the value to resolve, as Style holds it.

  • palette (TerminalPalette) – the terminal colors to resolve names against.

Return type:

str

Returns:

the color, as #rrggbb.

Raises:

ValueError – when the value names no color.

click_extra.capture.svg.style_rules(style, palette)[source]

Compile a style to the CSS an SVG text run is drawn with.

Parameters:
Return type:

str

Returns:

the CSS declarations, semicolon-separated.

click_extra.capture.svg.cursor_cell(picture, columns)[source]

Where a terminal left its cursor on the screen a capture pictures.

A command printing line by line leaves its cursor after the last thing it wrote, so the cursor stands at the end of the last row a TerminalScreen joins with newlines. A picture closing on a newline therefore carries an empty last row, and the cursor lands on it at column zero, which is where a terminal puts it.

Note

Derived rather than recorded, and exactly rather than nearly: the position is a property of the text a screen holds, so it survives a frame travelling as a bare string and costs the recorder no second channel to carry it on.

Todo

Carry each frame’s cursor position and visibility beside its text once a recording of a full-screen program asks for a cursor. Such a program moves and hides its cursor anywhere on the screen, which the end of the text does not say, so the cursor drawn today stands after its last row.

Parameters:
  • picture (str) – the captured text, ANSI escape sequences included.

  • columns (int) – width of the terminal, in characters.

Return type:

tuple[int, int] | None

Returns:

the cursor’s row and column, both counted from zero, or None for a screen showing nothing. The blank beat closing an animation’s cycle is the one such screen, and an empty terminal shows no cursor.

click_extra.capture.svg.gradient_svg(value, unique_id, width, height)[source]

Translate a CSS gradient into the paint server SVG draws it with.

An SVG fill takes a paint: a color, or a reference to a gradient declared as an element of its own. The syntax a page’s CSS carries, linear-gradient(135deg, #ff9a9e, #fad0c4), means nothing to it, and a capture handed one would come out unpainted. So the CSS is read here and re-emitted as the element SVG does understand, which is what lets the same --backdrop value serve both formats.

Understood: linear-gradient opening with an optional angle (135deg) or side keyword (to bottom right, see CSS_SIDE_ANGLES), and radial-gradient, both followed by two or more color stops, each pinnable at a percentage. Anything else returns None and is left alone, being a plain color as far as this is concerned.

The gradient is placed in user space, which is what makes it exact rather than approximated: the CSS line runs through the image’s center at the given angle, and is as long as the box measures along it (|W·sinθ| + |H·cosθ|), while a radial one reaches the farthest corner.

Parameters:
  • value (str) – the --backdrop value, gradient or not.

  • unique_id (str) – identifier the paint server is declared under.

  • width (float) – width of the image the gradient fills, in pixels.

  • height (float) – its height.

Return type:

tuple[str, str] | None

Returns:

the <defs> markup and the fill value referencing it, or None when the value is not a gradient this understands.

click_extra.capture.svg.titlebar_strip(left, top, width, *, paint, radius)[source]

Paint the strip a terminal seats its title and buttons in.

A capture leaves that strip the color of the terminal itself, where a real window carries a chrome of its own: the strip is what a reader’s eye reads as the top of a window rather than as the first line of output.

Drawn as a path rather than a rectangle because only its top corners follow the window’s own rounding; the bottom two meet the text and stay square.

Parameters:
  • left (float) – where the window starts, in pixels.

  • top (float) – where the window starts vertically, in pixels.

  • width (float) – how wide the window is, in pixels.

  • paint (str) – color to fill the strip with.

  • radius (int) – the window’s corner radius, in pixels.

Return type:

str

Returns:

the SVG markup.

click_extra.capture.svg.credit_markup(text, url, link, *, escape)[source]

Write a credit line as markup, the package name it credits linked.

The one part of the line worth pointing anywhere is the name, so both documents link that and leave the rest as prose. A line naming no package, or given no URL, is the escaped text and nothing more: a custom credit has nothing to link.

Parameters:
  • text (str) – the credit line.

  • url (str) – where the package name points. Empty links nothing.

  • link (Callable[[str, str], str]) – draws the anchor around the name, from the URL and the raw name. It escapes both for the document it writes.

  • escape (Callable[[str], str]) – how the rest of the line is escaped for that document.

Return type:

str

Returns:

the markup.

click_extra.capture.svg.watermark_svg(text, *, width, height, paint, font_stack="'Fira Code', 'Cascadia Code', Menlo, Consolas, monospace", url='https://kdeldycke.github.io/click-extra/screenshots.html')[source]

Draw the credit line in the image’s bottom-right corner.

Placed in the margin rather than over the terminal, which is what keeps it from covering a line of output: a capture is a picture of text, and a mark crossing that text costs the reader the thing being shown.

Carries a watermark class, so a reader taking a capture apart can tell the one run the renderer never captured from the ones it did.

The package name is wrapped in a link, see WATERMARK_URL and credit_markup().

Parameters:
  • text (str) – the credit to draw. Empty draws nothing.

  • width (float) – width of the whole image, in pixels.

  • height (float) – its height, in pixels.

  • paint (str) – color to draw the text in, alpha included.

  • font_stack (str) – fonts it is set in, the capture’s own.

  • url (str) – where the package name points. Empty links nothing.

Return type:

str

Returns:

the SVG markup, empty when there is nothing to draw.

click_extra.capture.svg.window_buttons(buttons, *, width, color, font_stack="'Fira Code', 'Cascadia Code', Menlo, Consolas, monospace")[source]

Draw a title bar’s decorations, as the terminal being mimicked draws them.

Two conventions, and a window carries one or the other: macOS fills round buttons on the left, Windows and GNOME set glyphs against the right edge. Both are placed in the window’s own coordinates, so they follow it wherever the frame moves it.

Parameters:
  • buttons (WindowButtons) – which decorations to draw.

  • width (float) – width of the window they are drawn in, in pixels.

  • color (str) – paint for the glyphs. Circles carry their own.

  • font_stack (str) – fonts the glyphs are set in, the window’s own.

Return type:

str

Returns:

the SVG markup, empty when the window wears none.

click_extra.capture.svg.cursor_svg(cursor, row, column, paint, unique_id)[source]

Draw a terminal’s cursor on the cell it stands on.

Emitted as part of that row’s cell backgrounds rather than as a layer of its own, which is what puts it under the glyphs and hands it to the machinery already deciding which rows move between frames. A row whose cursor never stirs is then drawn once for the whole animation, and a row that moves carries its cursor along at no charge. It is also why an animation typing a command line needs no caret of its own: the cursor follows the text.

Note

The cursor is never drawn over a glyph, so a block shape covering one is not a case to answer. cursor_cell() puts it at the end of a row, which is past everything written on it.

Parameters:
  • cursor (Cursor) – what the cursor looks like, see Cursor.

  • row (int) – the row it stands on, counted from zero.

  • column (int) – the column it stands on, counted from zero.

  • paint (str) – color it is drawn in.

  • unique_id (str) – prefix namespacing this document’s classes.

Return type:

str

Returns:

the SVG source for the cursor.

click_extra.capture.svg.column_segments(text, column)[source]

Cut a run of text into the columns it actually occupies.

A run carries its own padding: a help screen’s –count INTEGER Number of greetings. is one styled run holding two columns and the gutter between them. Drawn as a single element, the second column only lands where it belongs if the renderer honors textLength and resolves the font, because the gutter’s width is being paid for in glyphs. librsvg does neither, and the columns collapse onto each other.

Cutting the run at its gutters and giving each piece its own offset asks nothing of the renderer but to draw glyphs at coordinates.

Parameters:
  • text (str) – the run’s text, padding included.

  • column (int) – the terminal column the run starts on.

Return type:

Iterator[tuple[str, int]]

Returns:

each column’s text, with the column it starts on.

click_extra.capture.svg.glyph_offsets(text, column)[source]

Place a piece of text on the grid, as the attributes SVG reads.

A right-to-left piece is pinned by its offset alone: it is reordered and shaped by whoever draws it, and holding it to a width fights that.

Parameters:
  • text (str) – the piece’s glyphs.

  • column (int) – the terminal column it starts on.

Return type:

str

Returns:

the x attribute, and a textLength where one applies.

click_extra.capture.svg.shape_runs(text, column)[source]

Cut a column’s text into its runs of shapes and the text between.

A run of Box Drawing, Block Elements and Braille patterns is drawn as shapes by shape_svg(), and the text between as glyphs. That text is trimmed of the blanks at either end, which its offset pays for instead, as column_segments() does for a column.

Parameters:
  • text (str) – the column’s text.

  • column (int) – the terminal column it starts on.

Return type:

Iterator[tuple[str, int, bool]]

Returns:

each piece, with the column it starts on and whether it is drawn as shapes.

click_extra.capture.svg.block_svg(text, column, top, ink, paper)[source]

Draw a run of Block Elements as the rectangles they name.

Each character fills its share of its cell, see BLOCK_ELEMENTS. Neighbors sharing a band and a paint merge into one rectangle, so a bar of █ is one shape rather than one per cell. A rectangle reaching the bottom of its cell takes the CELL_BLEED a background takes, which closes the seam with the row below.

Parameters:
  • text (str) – the run, Block Elements only.

  • column (int) – the terminal column it starts on.

  • top (float) – where the row’s cells start, see CELL_TOP_INSET.

  • ink (str) – color the shapes are drawn in, as #rrggbb.

  • paper (str) – color behind them, as #rrggbb, which a shade mixes its ink with, see SHADE_DENSITY.

Return type:

str

Returns:

the SVG markup.

click_extra.capture.svg.braille_dots(char)[source]

Where the dots a Braille pattern raises sit on its cell.

Parameters:

char (str) – a Braille pattern, from U+2800 to U+28FF.

Return type:

tuple[tuple[int, int], ...]

Returns:

each raised dot’s (column, row), see BRAILLE_DOTS.

click_extra.capture.svg.braille_svg(text, column, top, ink)[source]

Draw a run of Braille patterns as the dots they raise.

Parameters:
  • text (str) – the run, Braille patterns only.

  • column (int) – the terminal column it starts on.

  • top (float) – where the row’s cells start, see CELL_TOP_INSET.

  • ink (str) – color the dots are drawn in, as #rrggbb.

Return type:

str

Returns:

the SVG markup, empty when no pattern raises a dot.

click_extra.capture.svg.box_strokes(char)[source]

The rectangles a Box Drawing line or dash fills on its cell.

Each is (vertical, low, high, start, end): whether the stroke runs down, then its span across and its span along, in the units of BOX_STROKES, on a cell 16 wide and 32 tall. Strokes on one line are merged, so a straight line is one rectangle.

A stroke runs from its cell’s edge toward the middle, and stops where its arm meets the arms across it, the way Menlo 22.0d2e1 draws each join:

  • A line passing straight through runs on into its other half, across the gap of a double line if it meets one, as in ╪.

  • A single line ending on a double line that passes through stops at its nearer stroke, as in ╤.

  • A stroke of a double line stops at the nearer stroke of the arm on its side, as the inner corner of ╔ does.

  • Any other stroke runs to the far edge of the arms across it, which fills the corner it turns, as the outer corner of ╔ does.

Parameters:

char (str) – a Box Drawing line or dash, see BOX_ARMS and BOX_DASHES.

Return type:

tuple[tuple[bool, float, float, float, float], ...]

Returns:

the rectangles.

click_extra.capture.svg.box_svg(text, column, top, ink)[source]

Draw a run of Box Drawing as the lines it names.

Lines and dashes are rectangles, see box_strokes(), and a stroke running on into the next cell merges with it, so a table’s rule is one shape. Arcs and diagonals are paths, see BOX_CURVES. A vertical stroke reaching the bottom of its cell takes the CELL_BLEED a background takes, which closes the seam with the row below.

Parameters:
  • text (str) – the run, Box Drawing only.

  • column (int) – the terminal column it starts on.

  • top (float) – where the row’s cells start, see CELL_TOP_INSET.

  • ink (str) – color the lines are drawn in.

Return type:

str

Returns:

the SVG markup.

click_extra.capture.svg.shape_svg(text, column, top, ink, paper)[source]

Draw a run of shapes: lines, rectangles and dots.

Box Drawing is drawn by box_svg(), Block Elements by block_svg() and Braille patterns by braille_svg().

Parameters:
  • text (str) – the run, see shape_runs().

  • column (int) – the terminal column it starts on.

  • top (float) – where the row’s cells start, see CELL_TOP_INSET.

  • ink (str) – color the shapes are drawn in, as #rrggbb.

  • paper (str) – color behind them, as #rrggbb, see block_svg().

Return type:

str

Returns:

the SVG markup.

click_extra.capture.svg.animation_digest(frames, durations)[source]

Fingerprint what an animation is, rather than how it happened to run.

Two recordings of one unchanged command are never byte-identical: the durations are wall-clock, so they answer to how busy the machine was. Quantizing settles the ordinary jitter and cannot settle a frame the scheduler dropped, which leaves a shorter sequence of the same frames.

So the fingerprint covers the frames a cycle holds and the beat it holds them on, not the order or the count. A dropped frame changes neither, and a command whose output actually changed changes the first.

Note

This is what lets a rebuild leave an unchanged asset alone instead of rewriting it, see ANIMATION_METADATA_RE. It is a freshness check and never a security one: a digest saying two animations match is a statement about the pixels, made by the same process that drew them.

Parameters:
  • frames (Sequence[str]) – each frame’s captured text.

  • durations (Sequence[float]) – how long each is shown, in the same order.

Return type:

str

Returns:

the fingerprint, as hexadecimal.

click_extra.capture.svg.animation_metadata(svg)[source]

Read back what an animated capture states about itself.

Parameters:

svg (str) – source of a rendered capture.

Return type:

dict[str, str]

Returns:

the @recording line’s fields, empty for a capture carrying none.

click_extra.capture.svg.frame_animation_css(unique_id, durations, shared=())[source]

Time an animated capture’s frames into CSS animation rules.

Every frame is shown for its own slice of one cycle, so a recording whose frames each lasted as long as the terminal held them rides the same machinery as a spinner whose frames are all one interval. A group several frames share is shown over each of their slices.

Note

Each boundary is computed once and handed to both the frame that ends on it and the frame that starts there. Rounding the two sides of one instant separately is what opens a gap, which the animation shows as a blank flash, or an overlap, which it shows as two frames drawn at once.

Every rule sits behind REDUCED_MOTION_QUERY, and nothing else here hides anything: the frames a still is not made of carry HIDDEN_FRAME_ATTRIBUTES as presentation attributes instead. A CSS animation outranks a presentation attribute, so the two never argue, and putting the hiding outside CSS altogether is what keeps a renderer reading no stylesheet from drawing every frame on top of the last. The keyframes restore both properties those attributes set, for the same reason.

Parameters:
  • unique_id (str) – prefix namespacing this document’s classes and keyframes.

  • durations (Sequence[float]) – seconds each frame is shown, in order.

  • shared (Sequence[Sequence[int]]) – the frames each shared group shows over, in order: the first is timed as the -s0 class, the next as -s1, and so on.

Return type:

str

Returns:

the stylesheet fragment, indented to sit in a <style> block.

Raises:

ValueError – when a frame is given a duration that is not positive.

Time a cursor’s blink into a CSS animation rule.

One keyframe set and one rule for the whole document, however many frames it holds: a terminal has one cursor, and every frame’s copy of it therefore lights and darkens together. It steps like a frame does, being lit or dark with nothing in between, see FRAME_TIMING_FUNCTION.

Note

The blink dims opacity and never touches visibility. A cursor sits inside the group of the frame it belongs to, and that group is hidden by both properties at once, see HIDDEN_FRAME_ATTRIBUTES. Opacity multiplies down into the group, so a hidden frame’s cursor stays hidden; a rule restoring visibility would instead override the inherited value and show every frame’s cursor at once.

The rule sits behind REDUCED_MOTION_QUERY, which leaves the cursor lit and still for a reader who asked their system for less motion.

Parameters:
  • unique_id (str) – prefix namespacing this document’s classes and keyframes.

  • period (float) – seconds one blink takes, half of it lit.

Return type:

str

Returns:

the stylesheet fragment, indented to sit in a <style> block.

Raises:

ValueError – when the period is not positive.

click_extra.capture.svg.render_svg(text='', *, columns, title='', unique_id=None, frames=None, interval=None, hold=0.0, blank=0.0, speed=1.0, emphasize=(), cursor=None, wrap_marker='', palette=('#292929', '#c5c8c6', ('#2e3436', '#cc0000', '#4e9a06', '#c4a000', '#3465a4', '#75507b', '#06989a', '#d3d7cf', '#555753', '#ef2929', '#8ae234', '#fce94f', '#729fcf', '#ad7fa8', '#34e2e2', '#eeeeec'), '#292929'), font_stack="'Fira Code', 'Cascadia Code', Menlo, Consolas, monospace", chrome=Chrome(border='none', border_width=1, radius=8, backdrop='none', shadow='none', margin=0, padding=0, opacity=1.0, watermark='', watermark_color='rgba(128,128,128,0.85)'), buttons=(('#ff5f57', '#febc2e', '#28c840'), ''), buttons_color=None, titlebar='none', collapse_titlebar=False)[source]

Draw captured terminal text as a picture of a terminal window.

A terminal is a fixed grid of identically-sized cells, which is what makes this arithmetic rather than typesetting: grid() says which cell each run of same-styled characters starts on, and every coordinate below is that column times CELL_WIDTH.

Two primitives draw everything. A <rect> fills the cells behind a run that carries a background, and a <text> draws its glyphs, pinned to its columns with textLength so the layout survives a reader who does not have the font. Box Drawing, Block Elements and Braille patterns are the exception: drawn as shapes by shape_svg(), and kept, unpainted, as text.

Note

A run’s padding is left out of its <text> and paid for in the x offset instead. Written the other way, a column only lands where it belongs if the glyphs are exactly the width assumed here, which asks the renderer to both honor textLength and resolve the font. A web browser does both. librsvg (and through it rsvg-convert and ImageMagick) ignores textLength, and a file manager, a git client or a thumbnailer commonly falls back to a proportional font. Starting each run on its own column asks neither.

Passing frames draws an animation instead of a still. The window, its caption and its clip path are drawn once and every frame is stacked inside them, so the frames differ in nothing but their text, and one stylesheet covers the lot: a color two frames share is one rule, and a frame cannot name a class the document never defines. Frames are hidden by frame_animation_css() in turn, leaving the last one that draws anything visible wherever the animation does not run.

Note

Everything a frame carries is namespaced by unique_id, keyframes included, so two animations inlined into one HTML page keep their own timing. Sharing a selector between them is what makes the shorter one run on the longer one’s clock and blank out for the frames it does not have.

Parameters:
  • text (str) – captured output, ANSI escape sequences included. The whole picture when frames is left out, and unused when it is given.

  • columns (int) – width of the terminal, in characters.

  • title (str) – caption drawn in the window’s title bar. Empty draws none.

  • unique_id (str | None) – prefix namespacing this document’s CSS classes and element IDs, see render(). Derived from the content when not given.

  • frames (Sequence[str] | None) – the animation’s frames, each captured text, in order. A single frame draws the same still text would.

  • interval (float | Sequence[float] | None) – seconds each frame is shown. One number times every frame alike, which is what a spinner asks for; a sequence gives each frame its own, which is what a recording asks for. Required alongside frames.

  • hold (float | Literal['auto']) – extra seconds the last frame stays up before the animation starts over. An animation that ends somewhere (a trail filled in, a bar run out, an outcome landed) is worth reading, and a loop that restarts the instant it arrives never lets anyone. A spinner turning in place ends nowhere, so it wants none of this and defaults to none. AUTO_HOLD scales the pause to the final frame’s own line count, see auto_hold().

  • blank (float) – seconds of empty screen closing the cycle, after hold. A loop that jumps from its last frame back to its first reads as one long animation doing something odd; an empty beat says plainly that this is where it starts over. Never the frame a still falls back to.

  • speed (float) – how much faster to play than it was recorded, so 2 halves every frame’s time and 0.5 doubles it. hold and blank are stated in real seconds and are not scaled: they are how long a reader is given, not part of what is being replayed.

  • emphasize (Sequence[int]) – lines to draw a band behind, counted from 1 the way :emphasize-lines: counts them. A band runs the full width of the window rather than of the text, the row being what is emphasized. In an animation it appears with the frame that first draws the row it marks, which is also when a gutter would first number that row, and it is gone again wherever the row is.

  • cursor (Cursor | None) – the terminal cursor to draw, see Cursor. None draws none. Where it stands is read off each frame’s own text, see cursor_cell(), so an animation carries it from screen to screen on its own. A cursor landing under the last line of output grows the window by that line, the way a terminal’s does.

  • wrap_marker (str) – characters closing each row a fold cut short, see grid(). Empty folds silently, the way a terminal does.

  • palette (TerminalPalette) – terminal colors the capture’s ANSI codes resolve against.

  • font_stack (str) – fonts the text is set in, best first.

  • chrome (Chrome) – the window around the text, resolved, see resolve(): its frame, corners, backdrop, shadow, margin, padding, opacity and credit line. Its backdrop takes a CSS gradient too, see gradient_svg(). Its opacity thins the window’s body alone: the frame, the title bar and the text keep their own paint. Defaults to BARE_CHROME.

  • buttons (WindowButtons) – decorations drawn in the title bar.

  • buttons_color (str | None) – paint for the glyph decorations. Circles carry their own colors. None takes the palette’s foreground.

  • titlebar (str) – paint for the strip the title and buttons sit in. NO_PAINT leaves it the terminal’s own color.

  • collapse_titlebar (bool) – drop that strip, closing the window over the first line of text. For a capture wearing neither decoration nor caption.

Return type:

str

Returns:

the SVG source.

Raises:

ValueError – when chrome is not resolved, when frames is given without an interval, when the two disagree on how many frames there are, when no frame is given, when hold or blank is negative, when emphasize names a line the capture does not have, or when a cursor is given a negative blink.