click_extra.layout module¶
Measure a line of terminal text, and compose one of a stated width.
A terminal is a grid of cells, and everything a CLI draws on it has to agree on
how many cells a string occupies. That number is not the string’s length: an
ideograph takes two cells and a combining mark none. cell_width() answers
it once, and every line this package composes is built on that answer.
The module holds the composition helpers built on it, starting with
center_in_rule(). They are terminal-text primitives, not capture
machinery: click_extra.capture.screenshot consumes them to lay a picture
out on the same grid, and so can any CLI drawing a divider of its own.
- click_extra.layout.PADDING = ' \xa0'¶
Characters that separate one column of terminal text from the next.
Both are one cell wide and draw nothing.
render_svg()emits every space as a non-breaking one, so the padding survives an XML round-trip and no renderer collapses a run of them.
- click_extra.layout.RULE_GLYPH = '─'¶
Character
center_in_rule()draws a rule with, absent a better one.An unbroken line, which is what a divider between two whole things is.
- click_extra.layout.RULE_COLOR = 'bright_black'¶
Color
center_in_rule()paints a rule and its brackets.A rule is the one line of a screen nothing printed, so it is drawn to recede: dimmer than the text it separates, in both the gallery of
click-extra themesand the marker standing in for what a capture cut. A rule a caller spells out in full is written as given, color included, since a caller naming one has already decided how it should look.
- click_extra.layout.cell_width(text, term_program=None)[source]¶
Columns
textoccupies on a terminal’s character grid.Not its length. A CJK ideograph is drawn two cells wide, a combining mark none, an ANSI escape or an OSC 8 hyperlink none at all while adding several characters, and a tab as many as it takes to reach the next stop eight columns along.
wcwidth.widthanswers for all of them, which is what lets a caller measure text as it arrived rather than stripping it first.Note
wcwidth.wcswidth, the older call, refuses any string carrying a control character and answers-1for it, which is every styled string. Reach for it only where a consumer reads that value back, astabulatedoes.- Parameters:
text (
str) – the text to measure.term_program (
str|None) – name of the terminal drawing the text, as$TERM_PROGRAMholds it. A few of them advance an emoji-presentation sequence by one column where the Unicode tables say two, and naming the terminal applieswcwidth’s correction for it.Nonemeasures what the tables say.
- Return type:
- Returns:
the number of cells it occupies, never negative.
- click_extra.layout.pad_to(text, width)[source]¶
Pad
textwith blanks until it occupieswidthcells.str.ljust()counts characters, so on a styled line it counts the escapes it cannot see and pads too little, or nothing at all. Text already at leastwidthcells wide comes back untouched.Any style the text left open is closed before the blanks, so a background cannot bleed across the gap into whatever sits beside it.
- click_extra.layout.center_in_rule(label, width, rule='─', opening='[ ', closing=' ]', color='bright_black')[source]¶
One line of
widthcells:labelcentered in a rule drawn withrule.labelmay arrive already styled:cell_width()discounts its escapes, so a caller paints the label its own way and hands the whole thing over.colorpaints the rule and the two brackets, which is the half a caller cannot pre-style without knowing where they fall.A
Noneor empty label draws no brackets and one contiguous rule: a divider naming nothing should not look like a frame around nothing. A width too narrow for the brackets drops them the same way, and one too narrow for the label leaves the label alone rather than drawing a rule that cannot close.Measured in cells, not characters: a label carrying a wide glyph shifts a rule built on
lenby one column per glyph.- Parameters:
label (
str|None) – the text the rule is drawn around, styled or not, orNonefor an unbroken rule.width (
int) – columns the line occupies.rule (
str) – character the rule is drawn with.opening (
str) – bracket written between the rule andlabel.closing (
str) – bracket written betweenlabeland the rule.color (
str|None) – color the rule and brackets are painted, orNoneto leave them as they are.labelis never repainted: a caller styles it itself, or leaves it in the terminal’s own ink.
- Return type:
- Returns:
the whole line.
- click_extra.layout.LINE_NUMBER_SEPARATOR = ' │ '¶
Rule drawn between a line’s number and the line itself.
A vertical bar rather than a bare space, so the gutter reads as a column of its own even where the output is itself indented.
- click_extra.layout.RTL_BIDI_CLASSES = frozenset({'AL', 'AN', 'R'})¶
Unicode bidirectional classes written right to left.
Right-to-left letters, Arabic letters and Arabic-Indic numbers, as
unicodedata.bidirectional()names them. Seeis_bidirectional().
- click_extra.layout.number_lines(text, start=1, digits=0)[source]¶
Prefix each line of
textwith its number, in a dim gutter.The numbers are drawn into the terminal text rather than into a column beside it, which is the same trade Pygments makes with its inline line numbers: every renderer places them for free, and every reader copying the text copies them too.
Right-aligned on the widest number, so the gutter is one column whatever the output’s length, and separated by
LINE_NUMBER_SEPARATOR.- Parameters:
text (
str) – the text to number, ANSI escape sequences included.start (
int) – number given to the first line.digits (
int) – fewest digits the numbers are aligned on, for a text whose gutter has to match the gutter of a longer one, seenumber_frames(). The widest number oftextstill fits when it has more.
- Return type:
- Returns:
the numbered text.
- click_extra.layout.number_frames(frames, start=1)[source]¶
Number the lines of each frame of an animation, in one gutter for all.
A frame is a screen, so each one counts its own rows from
start. The gutter is as wide on every frame as on the tallest: numbered on its own, a frame reaching ten rows takes one digit more than the frame before it, and every one of its rows moves a cell to the right.
- click_extra.layout.is_bidirectional(text)[source]¶
Whether
textcarries a character written right to left.Arabic, Hebrew and their neighbours are reordered by whoever draws them, and the cursive ones are shaped: a letter’s form depends on what it joins. A terminal grid describes neither, which is why
render_svg()stops pinning such a run to an exact width.
- click_extra.layout.fit_columns(text, floor=0)[source]¶
Width, in characters, of the longest line in
text.ANSI escapes style the glyphs around them and occupy no cell of their own, so they are discounted. Measured in terminal cells, so a line of CJK asks for the two columns per glyph it is drawn with.
- Parameters:
- Return type:
- Returns:
the width laying every line out without folding any.
- click_extra.layout.grid(text, columns, *, wrap_marker='')[source]¶
Lay ANSI text out on a terminal’s character grid.
Where a stream of styled text stops being a stream and becomes a picture. Each styled run of
split_ansi()is split at newlines into rows, then placed on the column it starts at, measured in cells rather than characters so a wide glyph takes the two it is drawn with.A line reaching past
columnssoft-wraps onto the next row, the way it would on a terminal that narrow, rather than being cropped: a command is free to print a line it never wraps itself (a long URL, a wide table, a machine-readable dump), and a layout that silently swallowed the overflow would be lying about what ran. A glyph straddling the edge moves down whole.Returning the column with each run is what lets a renderer place a run without measuring anything back out of its own output.
Note
wrap_markercloses each row a fold cut short, which is what tells a reader that the row continues below instead of the command having printed a line that stops mid-word. It is the renderer’s own mark, so it is painted withRULE_COLORrather than left in the terminal’s ink, and it takes a cell of the row it closes: a marked row carries one cell less of text, and a line that would have fit exactly folds once the marker is asked for. A terminal folds silently, so the default draws nothing.- Parameters:
- Return type:
- Returns:
one list of
(style, text, column)runs per row.
- click_extra.layout.wrap_ansi(text, width)[source]¶
Wrap
texttowidthterminal cells, preserving its ANSI styling.textwrap.wrap()counts every byte of an ANSI escape toward the line length, so a styled string wraps far earlier than its visible width warrants.wcwidth.wrapmeasures an escape at no cells and every character between escapes at the width a terminal advances by. It reopens on each line the styling still in effect, so no escape sequence crosses a line boundary: each returned line carries the styling it needs, opened and closed within the line.Note
Breaks land where
textwrap.wrap()puts them on plain ASCII, so long-word breaking and whitespace handling match it exactly. The two measures part on a double-width character, which counts for the two cells it takes, and on an OSC 8 hyperlink, which counts for none.