@promptctl/rich-js 0.18.0 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/anchor.d.ts +45 -0
- package/dist/core/anchor.js +39 -0
- package/dist/core/ansi.d.ts +65 -0
- package/dist/core/ansi.js +231 -0
- package/dist/core/box.d.ts +143 -0
- package/dist/core/box.js +379 -0
- package/dist/core/cells.d.ts +159 -0
- package/dist/core/cells.js +287 -0
- package/dist/core/color.d.ts +246 -0
- package/dist/core/color.js +1038 -0
- package/dist/core/console.d.ts +165 -0
- package/dist/core/console.js +527 -0
- package/dist/core/emoji.d.ts +24 -0
- package/dist/core/emoji.js +391 -0
- package/dist/core/export-html.d.ts +59 -0
- package/dist/core/export-html.js +200 -0
- package/dist/core/export-lines.d.ts +121 -0
- package/dist/core/export-lines.js +141 -0
- package/dist/core/highlighter.d.ts +33 -0
- package/dist/core/highlighter.js +124 -0
- package/dist/core/json.d.ts +27 -0
- package/dist/core/json.js +53 -0
- package/dist/core/markup.d.ts +119 -0
- package/dist/core/markup.js +731 -0
- package/dist/core/measure.d.ts +27 -0
- package/dist/core/measure.js +62 -0
- package/dist/core/oklch.d.ts +161 -0
- package/dist/core/oklch.js +312 -0
- package/dist/core/osc8.d.ts +68 -0
- package/dist/core/osc8.js +90 -0
- package/dist/core/place.d.ts +30 -0
- package/dist/core/place.js +51 -0
- package/dist/core/pretty.d.ts +144 -0
- package/dist/core/pretty.js +476 -0
- package/dist/core/protocol.d.ts +267 -0
- package/dist/core/protocol.js +163 -0
- package/dist/core/render.d.ts +84 -0
- package/dist/core/render.js +145 -0
- package/dist/core/segment.d.ts +161 -0
- package/dist/core/segment.js +492 -0
- package/dist/core/spinnerData.d.ts +12 -0
- package/dist/core/spinnerData.js +117 -0
- package/dist/core/strip.d.ts +141 -0
- package/dist/core/strip.js +305 -0
- package/dist/core/style.d.ts +169 -0
- package/dist/core/style.js +695 -0
- package/dist/core/subscription.d.ts +12 -0
- package/dist/core/subscription.js +12 -0
- package/dist/core/text.d.ts +256 -0
- package/dist/core/text.js +976 -0
- package/dist/core/wrap.d.ts +41 -0
- package/dist/core/wrap.js +127 -0
- package/dist/host/app.d.ts +121 -0
- package/dist/host/app.js +255 -0
- package/dist/host/host-environment.d.ts +26 -0
- package/dist/host/host-environment.js +44 -0
- package/dist/host/index.d.ts +5 -0
- package/dist/host/index.js +3 -0
- package/dist/host/terminal-host.d.ts +179 -0
- package/dist/host/terminal-host.js +186 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +107 -0
- package/dist/node/prompt.d.ts +21 -0
- package/dist/node/prompt.js +30 -0
- package/dist/node/save.d.ts +17 -0
- package/dist/node/save.js +21 -0
- package/dist/node/terminal-host.d.ts +113 -0
- package/dist/node/terminal-host.js +227 -0
- package/dist/node/traceback.d.ts +37 -0
- package/dist/node/traceback.js +134 -0
- package/dist/renderables/align.d.ts +16 -0
- package/dist/renderables/align.js +33 -0
- package/dist/renderables/columns.d.ts +61 -0
- package/dist/renderables/columns.js +152 -0
- package/dist/renderables/constrain.d.ts +15 -0
- package/dist/renderables/constrain.js +37 -0
- package/dist/renderables/embed.d.ts +50 -0
- package/dist/renderables/embed.js +71 -0
- package/dist/renderables/flexStrip.d.ts +41 -0
- package/dist/renderables/flexStrip.js +200 -0
- package/dist/renderables/group.d.ts +11 -0
- package/dist/renderables/group.js +17 -0
- package/dist/renderables/layout.d.ts +92 -0
- package/dist/renderables/layout.js +312 -0
- package/dist/renderables/live.d.ts +50 -0
- package/dist/renderables/live.js +175 -0
- package/dist/renderables/markdown.d.ts +24 -0
- package/dist/renderables/markdown.js +184 -0
- package/dist/renderables/padding.d.ts +38 -0
- package/dist/renderables/padding.js +141 -0
- package/dist/renderables/panel.d.ts +116 -0
- package/dist/renderables/panel.js +307 -0
- package/dist/renderables/progress.d.ts +90 -0
- package/dist/renderables/progress.js +238 -0
- package/dist/renderables/progressBar.d.ts +31 -0
- package/dist/renderables/progressBar.js +53 -0
- package/dist/renderables/prompt.d.ts +49 -0
- package/dist/renderables/prompt.js +125 -0
- package/dist/renderables/rule.d.ts +25 -0
- package/dist/renderables/rule.js +90 -0
- package/dist/renderables/spinner.d.ts +31 -0
- package/dist/renderables/spinner.js +70 -0
- package/dist/renderables/status.d.ts +23 -0
- package/dist/renderables/status.js +60 -0
- package/dist/renderables/syntax.d.ts +35 -0
- package/dist/renderables/syntax.js +115 -0
- package/dist/renderables/table.d.ts +194 -0
- package/dist/renderables/table.js +795 -0
- package/dist/renderables/traceback.d.ts +31 -0
- package/dist/renderables/traceback.js +108 -0
- package/dist/renderables/tree.d.ts +68 -0
- package/dist/renderables/tree.js +150 -0
- package/dist/renderables/viewport.d.ts +114 -0
- package/dist/renderables/viewport.js +198 -0
- package/dist/template-bindings/color-funcs.d.ts +87 -0
- package/dist/template-bindings/color-funcs.js +217 -0
- package/dist/template-bindings/helpers.d.ts +24 -0
- package/dist/template-bindings/helpers.js +48 -0
- package/dist/template-bindings/index.d.ts +107 -0
- package/dist/template-bindings/index.js +146 -0
- package/dist/template-bindings/palette-funcs.d.ts +94 -0
- package/dist/template-bindings/palette-funcs.js +141 -0
- package/dist/template-bindings/style-funcs.d.ts +28 -0
- package/dist/template-bindings/style-funcs.js +155 -0
- package/dist/themes/buildPalette.d.ts +36 -0
- package/dist/themes/buildPalette.js +56 -0
- package/dist/themes/colorMath.d.ts +82 -0
- package/dist/themes/colorMath.js +254 -0
- package/dist/themes/colorRef.d.ts +51 -0
- package/dist/themes/colorRef.js +96 -0
- package/dist/themes/data/ansi-tables.d.ts +14 -0
- package/dist/themes/data/ansi-tables.js +49 -0
- package/dist/themes/data/atom-one-dark.d.ts +3 -0
- package/dist/themes/data/atom-one-dark.js +177 -0
- package/dist/themes/data/atom-one-light.d.ts +3 -0
- package/dist/themes/data/atom-one-light.js +177 -0
- package/dist/themes/data/catppuccin-frappe.d.ts +3 -0
- package/dist/themes/data/catppuccin-frappe.js +177 -0
- package/dist/themes/data/catppuccin-latte.d.ts +3 -0
- package/dist/themes/data/catppuccin-latte.js +177 -0
- package/dist/themes/data/catppuccin-macchiato.d.ts +3 -0
- package/dist/themes/data/catppuccin-macchiato.js +177 -0
- package/dist/themes/data/catppuccin-mocha.d.ts +3 -0
- package/dist/themes/data/catppuccin-mocha.js +177 -0
- package/dist/themes/data/cyberpunk.d.ts +17 -0
- package/dist/themes/data/cyberpunk.js +193 -0
- package/dist/themes/data/default.d.ts +3 -0
- package/dist/themes/data/default.js +160 -0
- package/dist/themes/data/dracula.d.ts +3 -0
- package/dist/themes/data/dracula.js +177 -0
- package/dist/themes/data/flexoki.d.ts +3 -0
- package/dist/themes/data/flexoki.js +177 -0
- package/dist/themes/data/gruvbox.d.ts +3 -0
- package/dist/themes/data/gruvbox.js +177 -0
- package/dist/themes/data/index.d.ts +35 -0
- package/dist/themes/data/index.js +64 -0
- package/dist/themes/data/monokai.d.ts +3 -0
- package/dist/themes/data/monokai.js +177 -0
- package/dist/themes/data/nord.d.ts +3 -0
- package/dist/themes/data/nord.js +177 -0
- package/dist/themes/data/rose-pine-dawn.d.ts +3 -0
- package/dist/themes/data/rose-pine-dawn.js +177 -0
- package/dist/themes/data/rose-pine-moon.d.ts +3 -0
- package/dist/themes/data/rose-pine-moon.js +177 -0
- package/dist/themes/data/rose-pine.d.ts +3 -0
- package/dist/themes/data/rose-pine.js +177 -0
- package/dist/themes/data/solarized-dark.d.ts +3 -0
- package/dist/themes/data/solarized-dark.js +177 -0
- package/dist/themes/data/solarized-light.d.ts +3 -0
- package/dist/themes/data/solarized-light.js +177 -0
- package/dist/themes/data/svg-export.d.ts +3 -0
- package/dist/themes/data/svg-export.js +177 -0
- package/dist/themes/data/textual-ansi.d.ts +3 -0
- package/dist/themes/data/textual-ansi.js +160 -0
- package/dist/themes/data/textual-dark.d.ts +3 -0
- package/dist/themes/data/textual-dark.js +160 -0
- package/dist/themes/data/textual-light.d.ts +3 -0
- package/dist/themes/data/textual-light.js +160 -0
- package/dist/themes/data/tokyo-night.d.ts +3 -0
- package/dist/themes/data/tokyo-night.js +177 -0
- package/dist/themes/data/types.d.ts +31 -0
- package/dist/themes/data/types.js +10 -0
- package/dist/themes/palette.d.ts +21 -0
- package/dist/themes/palette.js +28 -0
- package/dist/themes/ramp.d.ts +83 -0
- package/dist/themes/ramp.js +122 -0
- package/dist/themes/registry.d.ts +44 -0
- package/dist/themes/registry.js +94 -0
- package/dist/themes/terminalThemes.d.ts +49 -0
- package/dist/themes/terminalThemes.js +63 -0
- package/dist/themes/transpose.d.ts +87 -0
- package/dist/themes/transpose.js +138 -0
- package/dist/widgets/button.d.ts +49 -0
- package/dist/widgets/button.js +208 -0
- package/dist/widgets/checkbox.d.ts +41 -0
- package/dist/widgets/checkbox.js +157 -0
- package/dist/widgets/dropdown.d.ts +67 -0
- package/dist/widgets/dropdown.js +450 -0
- package/dist/widgets/event-router.d.ts +71 -0
- package/dist/widgets/event-router.js +629 -0
- package/dist/widgets/focus-manager.d.ts +45 -0
- package/dist/widgets/focus-manager.js +191 -0
- package/dist/widgets/hit.d.ts +64 -0
- package/dist/widgets/hit.js +113 -0
- package/dist/widgets/index.d.ts +20 -0
- package/dist/widgets/index.js +14 -0
- package/dist/widgets/slider.d.ts +57 -0
- package/dist/widgets/slider.js +270 -0
- package/dist/widgets/static-item.d.ts +34 -0
- package/dist/widgets/static-item.js +54 -0
- package/dist/widgets/text-input.d.ts +274 -0
- package/dist/widgets/text-input.js +1165 -0
- package/dist/widgets/toggle.d.ts +44 -0
- package/dist/widgets/toggle.js +180 -0
- package/dist/widgets/types.d.ts +87 -0
- package/dist/widgets/types.js +30 -0
- package/dist/widgets/widget-app.d.ts +63 -0
- package/dist/widgets/widget-app.js +199 -0
- package/dist/widgets/widget-base.d.ts +40 -0
- package/dist/widgets/widget-base.js +165 -0
- package/package.json +6 -2
package/dist/core/box.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Box-drawing character sets for borders and table grids.
|
|
3
|
+
*
|
|
4
|
+
* A style is declared as an 8x4 character grid — one line per row a table can
|
|
5
|
+
* draw, one column per position within that row:
|
|
6
|
+
*
|
|
7
|
+
* ┌─┬┐ top border
|
|
8
|
+
* │ ││ header content
|
|
9
|
+
* ├─┼┤ header separator
|
|
10
|
+
* │ ││ body content
|
|
11
|
+
* ├─┼┤ row separator
|
|
12
|
+
* ├─┼┤ footer separator
|
|
13
|
+
* │ ││ footer content
|
|
14
|
+
* └─┴┘ bottom border
|
|
15
|
+
*
|
|
16
|
+
* [LAW:one-source-of-truth] The grid is the shape the reference implementation
|
|
17
|
+
* (Python Rich's `box.py`) publishes these glyphs in, so a constant below can
|
|
18
|
+
* be diffed against it character for character. The eighteen named fields this
|
|
19
|
+
* replaced were a second, differently-shaped map of the same territory, and it
|
|
20
|
+
* had drifted: seven of the nineteen constants carried wrong glyphs, and the
|
|
21
|
+
* field named `mid` held the reference's line 3 while the reference's `mid_*`
|
|
22
|
+
* is line 4 — so reading the reference name-for-name swapped a separator for a
|
|
23
|
+
* content row. Named row fields are gone for that reason; a row is reached by
|
|
24
|
+
* what it is (`getRow`, `getContentChars`), never by a name that can be
|
|
25
|
+
* mismatched to a line.
|
|
26
|
+
*/
|
|
27
|
+
import { cellLen } from "./cells.js";
|
|
28
|
+
import { Segment } from "./segment.js";
|
|
29
|
+
import { drawable } from "./protocol.js";
|
|
30
|
+
const GRID_ROWS = 8;
|
|
31
|
+
const GRID_COLUMNS = 4;
|
|
32
|
+
/** Corners that a legacy Windows terminal cannot draw, and their square kin. */
|
|
33
|
+
const SAFE_SUBSTITUTIONS = {
|
|
34
|
+
"╭": "┌",
|
|
35
|
+
"╮": "┐",
|
|
36
|
+
"╰": "└",
|
|
37
|
+
"╯": "┘",
|
|
38
|
+
};
|
|
39
|
+
const edgeOf = (row) => ({
|
|
40
|
+
left: row[0],
|
|
41
|
+
horizontal: row[1],
|
|
42
|
+
cross: row[2],
|
|
43
|
+
right: row[3],
|
|
44
|
+
});
|
|
45
|
+
const contentOf = (row) => ({
|
|
46
|
+
left: row[0],
|
|
47
|
+
vertical: row[2],
|
|
48
|
+
right: row[3],
|
|
49
|
+
});
|
|
50
|
+
// [LAW:one-type-per-behavior] Box is a single type for all border styles — instances differ by data
|
|
51
|
+
export class Box {
|
|
52
|
+
top;
|
|
53
|
+
bottom;
|
|
54
|
+
grid;
|
|
55
|
+
headContent;
|
|
56
|
+
headSeparator;
|
|
57
|
+
bodyContent;
|
|
58
|
+
rowSeparator;
|
|
59
|
+
footSeparator;
|
|
60
|
+
footContent;
|
|
61
|
+
/**
|
|
62
|
+
* [LAW:parse-dont-validate] The one crossing between a grid string and box
|
|
63
|
+
* glyphs. A `Box` cannot exist without eight rows of four characters, so no
|
|
64
|
+
* consumer below ever re-checks the shape of the data it reads.
|
|
65
|
+
*/
|
|
66
|
+
constructor(grid) {
|
|
67
|
+
const lines = grid.split("\n");
|
|
68
|
+
// A row is four characters because `getEdge` indexes the four positions,
|
|
69
|
+
// and four cells because that is what the terminal draws — a single astral
|
|
70
|
+
// glyph satisfies the first and breaks every frame the box appears in.
|
|
71
|
+
// [LAW:single-enforcer] `cellLen` is the cell-width authority.
|
|
72
|
+
const malformed = lines.length !== GRID_ROWS
|
|
73
|
+
|| lines.some((line) => Array.from(line).length !== GRID_COLUMNS)
|
|
74
|
+
|| lines.some((line) => cellLen(line) !== GRID_COLUMNS);
|
|
75
|
+
if (malformed) {
|
|
76
|
+
throw new Error(`A box grid is ${GRID_ROWS} lines of ${GRID_COLUMNS} single-cell `
|
|
77
|
+
+ `characters; got ${lines.length} line(s) measuring `
|
|
78
|
+
+ lines.map((line) => `${Array.from(line).length}/${cellLen(line)}`).join(", ")
|
|
79
|
+
+ " characters/cells");
|
|
80
|
+
}
|
|
81
|
+
const rows = lines.map((line) => Array.from(line));
|
|
82
|
+
this.grid = grid;
|
|
83
|
+
this.top = edgeOf(rows[0]);
|
|
84
|
+
this.headContent = contentOf(rows[1]);
|
|
85
|
+
this.headSeparator = edgeOf(rows[2]);
|
|
86
|
+
this.bodyContent = contentOf(rows[3]);
|
|
87
|
+
this.rowSeparator = edgeOf(rows[4]);
|
|
88
|
+
this.footSeparator = edgeOf(rows[5]);
|
|
89
|
+
this.footContent = contentOf(rows[6]);
|
|
90
|
+
this.bottom = edgeOf(rows[7]);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Renders the top border row for given column widths.
|
|
94
|
+
*/
|
|
95
|
+
getTop(widths, style, edge = true) {
|
|
96
|
+
return this.getEdge(widths, this.top, style, edge);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Renders the separator drawn *above* a row at `level` — the head separator
|
|
100
|
+
* under the header, the row separator between body rows, the foot separator
|
|
101
|
+
* above the footer.
|
|
102
|
+
*/
|
|
103
|
+
getRow(widths, level, style, edge = true) {
|
|
104
|
+
return this.getEdge(widths, this.getRowChars(level), style, edge);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The verticals that frame a content row at `level` — the counterpart to
|
|
108
|
+
* `getRow`, which draws the separator between two such rows.
|
|
109
|
+
*/
|
|
110
|
+
getContentChars(level) {
|
|
111
|
+
switch (level) {
|
|
112
|
+
case "head":
|
|
113
|
+
return this.headContent;
|
|
114
|
+
// `mid` is a spacer between body rows, so it is framed as one.
|
|
115
|
+
case "row":
|
|
116
|
+
case "mid":
|
|
117
|
+
return this.bodyContent;
|
|
118
|
+
case "foot":
|
|
119
|
+
return this.footContent;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Renders the bottom border row.
|
|
124
|
+
*/
|
|
125
|
+
getBottom(widths, style, edge = true) {
|
|
126
|
+
return this.getEdge(widths, this.bottom, style, edge);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The box to draw with when the platform cannot render this one as written.
|
|
130
|
+
*
|
|
131
|
+
* `asciiOnly` gives up a box that spends non-ASCII glyphs for `ASCII`, and
|
|
132
|
+
* leaves the four already-ASCII styles as they are — a caller that chose
|
|
133
|
+
* MARKDOWN or ASCII_DOUBLE_HEAD asked for that frame and it is already
|
|
134
|
+
* drawable, so answering with `ASCII` would trade a frame the terminal
|
|
135
|
+
* supports for a different one it equally supports. `safe` squares off the
|
|
136
|
+
* rounded corners a legacy Windows terminal draws as blanks.
|
|
137
|
+
*/
|
|
138
|
+
substitute(options = {}) {
|
|
139
|
+
// [LAW:one-source-of-truth] A box is wholly its grid, so the grid is what
|
|
140
|
+
// says whether it is already drawable — for a box built by `safeSubstitute`
|
|
141
|
+
// as much as for a shipped constant.
|
|
142
|
+
const drawn = drawable(options, this, ASCII, (box) => box.grid);
|
|
143
|
+
return drawn === this && options.safe === true ? this.safeSubstitute() : drawn;
|
|
144
|
+
}
|
|
145
|
+
safeSubstitute() {
|
|
146
|
+
return new Box(Array.from(this.grid, (char) => SAFE_SUBSTITUTIONS[char] ?? char).join(""));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The nearest box that spends no special glyphs on a header — what a table
|
|
150
|
+
* with `showHeader: false` draws with. A box whose head row already matches
|
|
151
|
+
* its body is its own answer, so this is the identity for fourteen of the
|
|
152
|
+
* nineteen shipped styles.
|
|
153
|
+
*
|
|
154
|
+
* It stands beside `substitute` rather than joining it because the two ask
|
|
155
|
+
* unrelated questions: `substitute` asks what the *platform* can draw, this
|
|
156
|
+
* asks what the *table* contains. Nothing correlates them, so a caller that
|
|
157
|
+
* wants both wants both, and a shared options bag would only multiply the
|
|
158
|
+
* combinations either one has to reason about. [LAW:no-mode-explosion]
|
|
159
|
+
*
|
|
160
|
+
* [LAW:types-are-the-program] The relation is keyed on the grid, not on
|
|
161
|
+
* object identity as the reference's dict is. A `Box` is wholly determined by
|
|
162
|
+
* its grid — that is what the constructor takes and all eight rows derive
|
|
163
|
+
* from — so two boxes with one grid must answer this alike. Identity keying
|
|
164
|
+
* would say otherwise the moment a box arrived by any route but the shipped
|
|
165
|
+
* constant, and `safeSubstitute` above builds exactly such a box: a fresh
|
|
166
|
+
* instance carrying an unchanged grid.
|
|
167
|
+
*/
|
|
168
|
+
plainHeaded() {
|
|
169
|
+
// `private` is scoped to the class, so the pairs below can be read by grid
|
|
170
|
+
// without exposing one.
|
|
171
|
+
return PLAIN_HEADED_SUBSTITUTIONS.find(([headed]) => headed.grid === this.grid)?.[1]
|
|
172
|
+
?? this;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* [LAW:dataflow-not-control-flow] Every full-width rule the box can draw is
|
|
176
|
+
* this one loop; which rule it is arrives as four characters, not a branch.
|
|
177
|
+
*/
|
|
178
|
+
getEdge(widths, chars, style, edge) {
|
|
179
|
+
const segments = [];
|
|
180
|
+
if (edge)
|
|
181
|
+
segments.push(new Segment(chars.left, style));
|
|
182
|
+
for (let i = 0; i < widths.length; i++) {
|
|
183
|
+
if (i > 0)
|
|
184
|
+
segments.push(new Segment(chars.cross, style));
|
|
185
|
+
segments.push(new Segment(chars.horizontal.repeat(widths[i]), style));
|
|
186
|
+
}
|
|
187
|
+
if (edge)
|
|
188
|
+
segments.push(new Segment(chars.right, style));
|
|
189
|
+
segments.push(Segment.line());
|
|
190
|
+
return segments;
|
|
191
|
+
}
|
|
192
|
+
getRowChars(level) {
|
|
193
|
+
switch (level) {
|
|
194
|
+
case "head":
|
|
195
|
+
return this.headSeparator;
|
|
196
|
+
case "row":
|
|
197
|
+
return this.rowSeparator;
|
|
198
|
+
case "foot":
|
|
199
|
+
return this.footSeparator;
|
|
200
|
+
// The reference's spacer row, not a rule: blank fill between the body
|
|
201
|
+
// row's verticals. Nothing draws it yet — rich-table-jx1 has the details.
|
|
202
|
+
case "mid":
|
|
203
|
+
return {
|
|
204
|
+
left: this.bodyContent.left,
|
|
205
|
+
horizontal: " ",
|
|
206
|
+
cross: this.bodyContent.vertical,
|
|
207
|
+
right: this.bodyContent.right,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// --- Pre-built box styles ---
|
|
213
|
+
// Transcribed from Python Rich's `rich/box.py` and verified grid-for-grid
|
|
214
|
+
// against it; see this module's header for the shape and why it is this shape.
|
|
215
|
+
export const ASCII = new Box("+--+\n" +
|
|
216
|
+
"| ||\n" +
|
|
217
|
+
"|-+|\n" +
|
|
218
|
+
"| ||\n" +
|
|
219
|
+
"|-+|\n" +
|
|
220
|
+
"|-+|\n" +
|
|
221
|
+
"| ||\n" +
|
|
222
|
+
"+--+");
|
|
223
|
+
export const ASCII2 = new Box("+-++\n" +
|
|
224
|
+
"| ||\n" +
|
|
225
|
+
"+-++\n" +
|
|
226
|
+
"| ||\n" +
|
|
227
|
+
"+-++\n" +
|
|
228
|
+
"+-++\n" +
|
|
229
|
+
"| ||\n" +
|
|
230
|
+
"+-++");
|
|
231
|
+
export const ASCII_DOUBLE_HEAD = new Box("+-++\n" +
|
|
232
|
+
"| ||\n" +
|
|
233
|
+
"+=++\n" +
|
|
234
|
+
"| ||\n" +
|
|
235
|
+
"+-++\n" +
|
|
236
|
+
"+-++\n" +
|
|
237
|
+
"| ||\n" +
|
|
238
|
+
"+-++");
|
|
239
|
+
export const SQUARE = new Box("┌─┬┐\n" +
|
|
240
|
+
"│ ││\n" +
|
|
241
|
+
"├─┼┤\n" +
|
|
242
|
+
"│ ││\n" +
|
|
243
|
+
"├─┼┤\n" +
|
|
244
|
+
"├─┼┤\n" +
|
|
245
|
+
"│ ││\n" +
|
|
246
|
+
"└─┴┘");
|
|
247
|
+
export const SQUARE_DOUBLE_HEAD = new Box("┌─┬┐\n" +
|
|
248
|
+
"│ ││\n" +
|
|
249
|
+
"╞═╪╡\n" +
|
|
250
|
+
"│ ││\n" +
|
|
251
|
+
"├─┼┤\n" +
|
|
252
|
+
"├─┼┤\n" +
|
|
253
|
+
"│ ││\n" +
|
|
254
|
+
"└─┴┘");
|
|
255
|
+
export const MINIMAL = new Box(" ╷ \n" +
|
|
256
|
+
" │ \n" +
|
|
257
|
+
"╶─┼╴\n" +
|
|
258
|
+
" │ \n" +
|
|
259
|
+
"╶─┼╴\n" +
|
|
260
|
+
"╶─┼╴\n" +
|
|
261
|
+
" │ \n" +
|
|
262
|
+
" ╵ ");
|
|
263
|
+
export const MINIMAL_HEAVY_HEAD = new Box(" ╷ \n" +
|
|
264
|
+
" │ \n" +
|
|
265
|
+
"╺━┿╸\n" +
|
|
266
|
+
" │ \n" +
|
|
267
|
+
"╶─┼╴\n" +
|
|
268
|
+
"╶─┼╴\n" +
|
|
269
|
+
" │ \n" +
|
|
270
|
+
" ╵ ");
|
|
271
|
+
export const MINIMAL_DOUBLE_HEAD = new Box(" ╷ \n" +
|
|
272
|
+
" │ \n" +
|
|
273
|
+
" ═╪ \n" +
|
|
274
|
+
" │ \n" +
|
|
275
|
+
" ─┼ \n" +
|
|
276
|
+
" ─┼ \n" +
|
|
277
|
+
" │ \n" +
|
|
278
|
+
" ╵ ");
|
|
279
|
+
export const SIMPLE = new Box(" \n" +
|
|
280
|
+
" \n" +
|
|
281
|
+
" ── \n" +
|
|
282
|
+
" \n" +
|
|
283
|
+
" \n" +
|
|
284
|
+
" ── \n" +
|
|
285
|
+
" \n" +
|
|
286
|
+
" ");
|
|
287
|
+
export const SIMPLE_HEAD = new Box(" \n" +
|
|
288
|
+
" \n" +
|
|
289
|
+
" ── \n" +
|
|
290
|
+
" \n" +
|
|
291
|
+
" \n" +
|
|
292
|
+
" \n" +
|
|
293
|
+
" \n" +
|
|
294
|
+
" ");
|
|
295
|
+
export const SIMPLE_HEAVY = new Box(" \n" +
|
|
296
|
+
" \n" +
|
|
297
|
+
" ━━ \n" +
|
|
298
|
+
" \n" +
|
|
299
|
+
" \n" +
|
|
300
|
+
" ━━ \n" +
|
|
301
|
+
" \n" +
|
|
302
|
+
" ");
|
|
303
|
+
export const HORIZONTALS = new Box(" ── \n" +
|
|
304
|
+
" \n" +
|
|
305
|
+
" ── \n" +
|
|
306
|
+
" \n" +
|
|
307
|
+
" ── \n" +
|
|
308
|
+
" ── \n" +
|
|
309
|
+
" \n" +
|
|
310
|
+
" ── ");
|
|
311
|
+
export const ROUNDED = new Box("╭─┬╮\n" +
|
|
312
|
+
"│ ││\n" +
|
|
313
|
+
"├─┼┤\n" +
|
|
314
|
+
"│ ││\n" +
|
|
315
|
+
"├─┼┤\n" +
|
|
316
|
+
"├─┼┤\n" +
|
|
317
|
+
"│ ││\n" +
|
|
318
|
+
"╰─┴╯");
|
|
319
|
+
export const HEAVY = new Box("┏━┳┓\n" +
|
|
320
|
+
"┃ ┃┃\n" +
|
|
321
|
+
"┣━╋┫\n" +
|
|
322
|
+
"┃ ┃┃\n" +
|
|
323
|
+
"┣━╋┫\n" +
|
|
324
|
+
"┣━╋┫\n" +
|
|
325
|
+
"┃ ┃┃\n" +
|
|
326
|
+
"┗━┻┛");
|
|
327
|
+
export const HEAVY_EDGE = new Box("┏━┯┓\n" +
|
|
328
|
+
"┃ │┃\n" +
|
|
329
|
+
"┠─┼┨\n" +
|
|
330
|
+
"┃ │┃\n" +
|
|
331
|
+
"┠─┼┨\n" +
|
|
332
|
+
"┠─┼┨\n" +
|
|
333
|
+
"┃ │┃\n" +
|
|
334
|
+
"┗━┷┛");
|
|
335
|
+
export const HEAVY_HEAD = new Box("┏━┳┓\n" +
|
|
336
|
+
"┃ ┃┃\n" +
|
|
337
|
+
"┡━╇┩\n" +
|
|
338
|
+
"│ ││\n" +
|
|
339
|
+
"├─┼┤\n" +
|
|
340
|
+
"├─┼┤\n" +
|
|
341
|
+
"│ ││\n" +
|
|
342
|
+
"└─┴┘");
|
|
343
|
+
export const DOUBLE = new Box("╔═╦╗\n" +
|
|
344
|
+
"║ ║║\n" +
|
|
345
|
+
"╠═╬╣\n" +
|
|
346
|
+
"║ ║║\n" +
|
|
347
|
+
"╠═╬╣\n" +
|
|
348
|
+
"╠═╬╣\n" +
|
|
349
|
+
"║ ║║\n" +
|
|
350
|
+
"╚═╩╝");
|
|
351
|
+
export const DOUBLE_EDGE = new Box("╔═╤╗\n" +
|
|
352
|
+
"║ │║\n" +
|
|
353
|
+
"╟─┼╢\n" +
|
|
354
|
+
"║ │║\n" +
|
|
355
|
+
"╟─┼╢\n" +
|
|
356
|
+
"╟─┼╢\n" +
|
|
357
|
+
"║ │║\n" +
|
|
358
|
+
"╚═╧╝");
|
|
359
|
+
export const MARKDOWN = new Box(" \n" +
|
|
360
|
+
"| ||\n" +
|
|
361
|
+
"|-||\n" +
|
|
362
|
+
"| ||\n" +
|
|
363
|
+
"|-||\n" +
|
|
364
|
+
"|-||\n" +
|
|
365
|
+
"| ||\n" +
|
|
366
|
+
" ");
|
|
367
|
+
/**
|
|
368
|
+
* Boxes whose header glyphs differ from their body's, paired with the kin that
|
|
369
|
+
* draws the same frame without them. Transcribed from the reference's
|
|
370
|
+
* `PLAIN_HEADED_SUBSTITUTIONS`; the fourteen styles absent here already draw a
|
|
371
|
+
* plain head, and `plainHeaded` returns them unchanged.
|
|
372
|
+
*/
|
|
373
|
+
const PLAIN_HEADED_SUBSTITUTIONS = [
|
|
374
|
+
[HEAVY_HEAD, SQUARE],
|
|
375
|
+
[SQUARE_DOUBLE_HEAD, SQUARE],
|
|
376
|
+
[MINIMAL_HEAVY_HEAD, MINIMAL],
|
|
377
|
+
[MINIMAL_DOUBLE_HEAD, MINIMAL],
|
|
378
|
+
[ASCII_DOUBLE_HEAD, ASCII2],
|
|
379
|
+
];
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal cell width calculation.
|
|
3
|
+
* Handles ASCII, CJK (double-width), and emoji characters.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Returns the terminal cell width of a string.
|
|
7
|
+
*/
|
|
8
|
+
export declare function cellLen(text: string): number;
|
|
9
|
+
declare const _cellCol: unique symbol;
|
|
10
|
+
declare const _codeUnit: unique symbol;
|
|
11
|
+
declare const _codePoint: unique symbol;
|
|
12
|
+
/** Terminal cell-column offset. Never interchangeable with a code-unit index. */
|
|
13
|
+
export type CellCol = number & {
|
|
14
|
+
readonly [_cellCol]: true;
|
|
15
|
+
};
|
|
16
|
+
/** JS string code-unit index. Never interchangeable with a cell-column offset. */
|
|
17
|
+
export type CodeUnit = number & {
|
|
18
|
+
readonly [_codeUnit]: true;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* A `CodeUnit` index that is additionally guaranteed to fall on a Unicode
|
|
22
|
+
* code-point boundary (i.e. never inside a surrogate pair). Assignable to
|
|
23
|
+
* `CodeUnit`; the reverse assignment is forbidden.
|
|
24
|
+
*/
|
|
25
|
+
export type CodePoint = CodeUnit & {
|
|
26
|
+
readonly [_codePoint]: true;
|
|
27
|
+
};
|
|
28
|
+
/** Brand a raw number as a CellCol. Use only at trust boundaries. */
|
|
29
|
+
export declare function asCellCol(n: number): CellCol;
|
|
30
|
+
/** Brand a raw number as a CodeUnit. Use only at trust boundaries. */
|
|
31
|
+
export declare function asCodeUnit(n: number): CodeUnit;
|
|
32
|
+
/** Brand a raw number as a CodePoint. Use only when the value is known to be
|
|
33
|
+
* on a Unicode code-point boundary. */
|
|
34
|
+
export declare function asCodePoint(n: number): CodePoint;
|
|
35
|
+
/**
|
|
36
|
+
* [LAW:parse-dont-validate] A caller's number as a count of cells — the checked
|
|
37
|
+
* counterpart to `asCellCol`'s unchecked brand, and the one place the rule "a
|
|
38
|
+
* cell count is a non-negative integer" is written.
|
|
39
|
+
*
|
|
40
|
+
* [LAW:one-source-of-truth] Every renderable is handed `RenderOptions.maxWidth`
|
|
41
|
+
* as a plain `number`, so every renderable used to answer this for itself and
|
|
42
|
+
* they disagreed: at NaN, `Table` collapsed to one cell of garbage, `Columns`
|
|
43
|
+
* and `Layout` threw `Invalid array length`, and `RichText` ignored the request
|
|
44
|
+
* and emitted its full natural width. Parse at the entry to `render`/`measure`
|
|
45
|
+
* and nothing downstream re-asks.
|
|
46
|
+
*
|
|
47
|
+
* The comparison is the point, not `Math.max(0, n)`: NaN fails every comparison
|
|
48
|
+
* and so floors here, where `Math.max` would return it and let it poison every
|
|
49
|
+
* bounds check downstream. Integral because a cell is not divisible — a
|
|
50
|
+
* fractional budget reaches a largest-remainder division, which grants a whole
|
|
51
|
+
* cell against a fractional residue and hands out more width than it was given.
|
|
52
|
+
*
|
|
53
|
+
* `Infinity` is outside what the comparison floors, so it passes through
|
|
54
|
+
* unchanged, and no clamp belongs here: the finite answer to an unbounded offer
|
|
55
|
+
* is the renderable's own natural width, which this function cannot see from
|
|
56
|
+
* the number alone. `withBoundedWidth` in `protocol.ts` is the second half of
|
|
57
|
+
* the parse and the only place that value is resolved.
|
|
58
|
+
*/
|
|
59
|
+
export declare function cellCount(n: number): CellCol;
|
|
60
|
+
/**
|
|
61
|
+
* Pads or crops a string to exactly `totalWidth` terminal cells.
|
|
62
|
+
* Invariant: cellLen(setCellSize(text, n)) === n (unless n is 0)
|
|
63
|
+
*/
|
|
64
|
+
export declare function setCellSize(text: string, totalWidth: CellCol): string;
|
|
65
|
+
/**
|
|
66
|
+
* Splits text at a cell position. Returns [left, right].
|
|
67
|
+
* When the position falls mid-wide-character, the left side is padded
|
|
68
|
+
* to reach exactly `position` cells. The wide char remains in the right side.
|
|
69
|
+
*/
|
|
70
|
+
export declare function splitText(text: string, position: CellCol): [string, string];
|
|
71
|
+
/**
|
|
72
|
+
* Wraps text into lines of at most `maxWidth` cells, preserving every code
|
|
73
|
+
* point: `chopCells(t, w).join("") === t`. Unlike `splitText` this never pads,
|
|
74
|
+
* so a line ending before a wide glyph is narrower than `maxWidth` rather than
|
|
75
|
+
* padded out to it.
|
|
76
|
+
*
|
|
77
|
+
* A line overflows exactly where the budget cannot be met: a glyph wider than
|
|
78
|
+
* `maxWidth` is force-taken whole rather than dropped, and a `maxWidth` of zero
|
|
79
|
+
* or less is no budget at all, so the text comes back as one line.
|
|
80
|
+
*/
|
|
81
|
+
export declare function chopCells(text: string, maxWidth: CellCol): string[];
|
|
82
|
+
/**
|
|
83
|
+
* The grapheme clusters of `text`, in order: the unit `string-width` measures,
|
|
84
|
+
* and so the unit `cellLen` of a whole string is the sum of. Summing `cellLen`
|
|
85
|
+
* over code points instead disagrees with it on every glyph built from several:
|
|
86
|
+
* `❤️` is 1 + 0 by code point and 2 as a cluster, `👨👩👧` is 6 and 2, and a cut
|
|
87
|
+
* between two of its code points leaves half a glyph.
|
|
88
|
+
*/
|
|
89
|
+
export declare function graphemes(text: string): string[];
|
|
90
|
+
/**
|
|
91
|
+
* Returns the largest prefix of `text` whose cell width fits within `cap` cells,
|
|
92
|
+
* cut between grapheme clusters. No padding — the returned string may be
|
|
93
|
+
* narrower than `cap` when the next glyph is wide and would overshoot. Never
|
|
94
|
+
* wider than `cap` cells.
|
|
95
|
+
*
|
|
96
|
+
* When the first glyph already exceeds `cap` cells, returns "" (the caller
|
|
97
|
+
* must decide whether to force-take the glyph or skip it).
|
|
98
|
+
*/
|
|
99
|
+
export declare function cellFit(text: string, cap: CellCol): string;
|
|
100
|
+
/** `cellFit` from the other end: the largest suffix of `text` within `cap` cells. */
|
|
101
|
+
export declare function cellFitEnd(text: string, cap: CellCol): string;
|
|
102
|
+
/**
|
|
103
|
+
* Returns the largest code-unit end offset starting from `startCU` whose
|
|
104
|
+
* prefix (from `startCU`) has cell width ≤ `cap`. Iterates from the given
|
|
105
|
+
* offset without slicing the tail, avoiding O(N²) allocation when called
|
|
106
|
+
* repeatedly across a long string.
|
|
107
|
+
*
|
|
108
|
+
* [LAW:types-are-the-program] returns CodePoint because for...of always
|
|
109
|
+
* stops on a code-point boundary.
|
|
110
|
+
*/
|
|
111
|
+
export declare function cellFitFrom(text: string, startCU: CodePoint, cap: CellCol): CodePoint;
|
|
112
|
+
/**
|
|
113
|
+
* Like `cellFitFrom`, but for any `startCU` inside `text` the result is
|
|
114
|
+
* strictly past it: a glyph too wide for `cap` is force-taken whole and
|
|
115
|
+
* overflows `cap` by its own width. So a `while (i < text.length)` loop driven
|
|
116
|
+
* by this terminates by construction, and the re-brand is honest — both
|
|
117
|
+
* operands land on code-point boundaries, so their max does too.
|
|
118
|
+
*
|
|
119
|
+
* [LAW:single-enforcer] `cellFit` documents that its caller must choose between
|
|
120
|
+
* force-taking and skipping; this is that choice, made once. It was made twice
|
|
121
|
+
* before and the copies drifted — the textarea's soft wrap force-took,
|
|
122
|
+
* `chopCells` did neither and hung.
|
|
123
|
+
*/
|
|
124
|
+
export declare function cellStepFrom(text: string, startCU: CodePoint, cap: CellCol): CodePoint;
|
|
125
|
+
/**
|
|
126
|
+
* Returns the largest code-unit offset into `content` whose prefix has
|
|
127
|
+
* cell width ≤ `cellCol`. When `cellCol` falls mid-wide-character the
|
|
128
|
+
* function stops before that character (never advances into it).
|
|
129
|
+
* Clamps to `content.length` if `cellCol` exceeds the string's total
|
|
130
|
+
* cell width.
|
|
131
|
+
*
|
|
132
|
+
* This is the inverse of `cellLen(content.slice(0, codeUnit))` — given a
|
|
133
|
+
* visual column, return the corresponding string index.
|
|
134
|
+
*
|
|
135
|
+
* Returns `CodePoint` because `for...of` iteration always stops on a
|
|
136
|
+
* code-point boundary.
|
|
137
|
+
*/
|
|
138
|
+
export declare function cellColToCodeUnitOffset(content: string, cellCol: CellCol): CodePoint;
|
|
139
|
+
/**
|
|
140
|
+
* Advance one full Unicode code point from `cu`, returning the code-unit
|
|
141
|
+
* offset of the start of the *next* code point. Returns `s.length` when
|
|
142
|
+
* already at or past the end.
|
|
143
|
+
*
|
|
144
|
+
* Handles surrogate pairs: when the code point at `cu` is a supplementary
|
|
145
|
+
* character (U+10000…U+10FFFF) it occupies 2 UTF-16 code units, so the
|
|
146
|
+
* returned offset advances by 2.
|
|
147
|
+
*/
|
|
148
|
+
export declare function nextCodePoint(s: string, cu: CodeUnit): CodePoint;
|
|
149
|
+
/**
|
|
150
|
+
* Step back one full Unicode code point from `cu`, returning the code-unit
|
|
151
|
+
* offset of the start of the *previous* code point. Returns 0 when already
|
|
152
|
+
* at the start.
|
|
153
|
+
*
|
|
154
|
+
* Handles surrogate pairs: when the code unit at `cu - 1` is a low surrogate
|
|
155
|
+
* AND the code unit at `cu - 2` is a high surrogate, steps back 2 code units.
|
|
156
|
+
* Unpaired surrogates are treated as 1-CU characters.
|
|
157
|
+
*/
|
|
158
|
+
export declare function prevCodePoint(s: string, cu: CodeUnit): CodePoint;
|
|
159
|
+
export {};
|