@ac-kit/app-terminal 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # @ac-kit/app-terminal
2
+
3
+ What a renderer needs to know about the terminal it is drawing to — as a value
4
+ it is handed, not as a global it reads.
5
+
6
+ That distinction is what makes terminal output testable. A renderer that checks
7
+ `process.stdout.isTTY` and `process.stdout.columns` can only be exercised under
8
+ a real pty; one that takes a `Terminal` can be driven with a plain object and
9
+ asserted on.
10
+
11
+ ```typescript
12
+ import { createLiveRegion, glyphFor, styleTextFor } from "@ac-kit/app-terminal";
13
+ import { nodeTerminal } from "@ac-kit/app-system";
14
+
15
+ const terminal = nodeTerminal(process.stdout);
16
+
17
+ const tick = glyphFor(terminal, "success"); // "✔", or an ASCII fallback
18
+ const heading = styleTextFor(terminal, "Results", { styles: ["bold"] });
19
+
20
+ const region = createLiveRegion(terminal);
21
+ process.stdout.write(region(["building…"]));
22
+ process.stdout.write(region([`${tick} done`]));
23
+ ```
24
+
25
+ `glyphFor` and `styleTextFor` degrade with the terminal: no Unicode means an
26
+ ASCII glyph, and a non-interactive terminal means no escape sequences at all, so
27
+ the same code produces sensible output when piped to a file. A `LiveRegion`
28
+ _returns_ the string to write rather than writing it, so the caller keeps
29
+ control of the stream.
30
+
31
+ ## What it exposes
32
+
33
+ - `Terminal` — interactivity, `columns`/`rows`, `colorDepth`, `unicode`,
34
+ `hyperlinks`, a `resize` event, and optional alt-screen entry/exit.
35
+ - `createLiveRegion`, `isRedrawable`, `liveRegionHeight` — redrawing a block of
36
+ lines in place, clamped to the viewport so scrollback is never corrupted.
37
+ - `glyphFor` and `GlyphRole` — the right character for a role, given the
38
+ terminal's Unicode support.
39
+ - `styleTextFor` and the semantic palette — `@ac-kit/format-ansi` styling that
40
+ reads `colorDepth` off the `Terminal` rather than taking it as an argument.
41
+
42
+ The Node adapter, `nodeTerminal`, lives in `@ac-kit/app-system`, so this package
43
+ itself stays portable.
44
+
45
+ See the
46
+ [generated API docs](https://anthochamp.github.io/node-essentials/api/app-terminal/)
47
+ for the full reference.
@@ -0,0 +1,22 @@
1
+ import type { Terminal } from "./terminal.js";
2
+ /**
3
+ * What a symbol means in a rendered report, independent of which character
4
+ * draws it.
5
+ *
6
+ * The layer that keeps `@ac-kit/format-monospace` free of vocabulary it has no
7
+ * business owning: it ships `star` and `triangleUp`, this decides that a winner
8
+ * is a star. Same split as colour, where the palette names roles and
9
+ * `@ac-kit/math-color` only knows values.
10
+ */
11
+ export type GlyphRole = "winner" | "success" | "failure" | "warning" | "error" | "info" | "better" | "worse" | "unchanged" | "pending" | "bullet" | "more";
12
+ /**
13
+ * The symbol for `role`, in whichever repertoire the terminal can render.
14
+ *
15
+ * `better` and `worse` are drawn as **down** and **up** triangles: the roles
16
+ * name the judgement, not the direction, so a higher-is-better measure passes
17
+ * `better` for a rise and still gets the glyph its readers associate with good
18
+ * news. A renderer that means "the number went up" wants `triangleUp` from the
19
+ * inventory directly.
20
+ */
21
+ export declare function glyphFor(terminal: Terminal, role: GlyphRole): string;
22
+ //# sourceMappingURL=glyph-for.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"glyph-for.d.ts","sourceRoot":"","sources":["../src/glyph-for.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAClB,QAAQ,GACR,SAAS,GACT,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,QAAQ,GACR,OAAO,GACP,WAAW,GACX,SAAS,GACT,QAAQ,GACR,MAAM,CAAC;AAiBV;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,SAAS,GAAG,MAAM,CAEpE"}
@@ -0,0 +1,28 @@
1
+ import { glyph } from "@ac-kit/format-monospace";
2
+ const ROLE_GLYPHS = {
3
+ winner: "star",
4
+ success: "check",
5
+ failure: "cross",
6
+ warning: "warningSign",
7
+ error: "heavyCross",
8
+ info: "info",
9
+ better: "triangleDown",
10
+ worse: "triangleUp",
11
+ unchanged: "middleDot",
12
+ pending: "circleOutline",
13
+ bullet: "bullet",
14
+ more: "ellipsis",
15
+ };
16
+ /**
17
+ * The symbol for `role`, in whichever repertoire the terminal can render.
18
+ *
19
+ * `better` and `worse` are drawn as **down** and **up** triangles: the roles
20
+ * name the judgement, not the direction, so a higher-is-better measure passes
21
+ * `better` for a rise and still gets the glyph its readers associate with good
22
+ * news. A renderer that means "the number went up" wants `triangleUp` from the
23
+ * inventory directly.
24
+ */
25
+ export function glyphFor(terminal, role) {
26
+ return glyph(ROLE_GLYPHS[role], terminal.unicode ? "unicode" : "ascii");
27
+ }
28
+ //# sourceMappingURL=glyph-for.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"glyph-for.js","sourceRoot":"","sources":["../src/glyph-for.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AA2BjD,MAAM,WAAW,GAA2C;IAC3D,MAAM,EAAE,MAAM;IACd,OAAO,EAAE,OAAO;IAChB,OAAO,EAAE,OAAO;IAChB,OAAO,EAAE,aAAa;IACtB,KAAK,EAAE,YAAY;IACnB,IAAI,EAAE,MAAM;IACZ,MAAM,EAAE,cAAc;IACtB,KAAK,EAAE,YAAY;IACnB,SAAS,EAAE,WAAW;IACtB,OAAO,EAAE,eAAe;IACxB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,UAAU;CAChB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,QAAkB,EAAE,IAAe;IAC3D,OAAO,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,6 @@
1
+ export * from "./glyph-for.js";
2
+ export * from "./live-region.js";
3
+ export * from "./semantic-palette.js";
4
+ export * from "./style-text-for.js";
5
+ export * from "./terminal.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./glyph-for.js";
2
+ export * from "./live-region.js";
3
+ export * from "./semantic-palette.js";
4
+ export * from "./style-text-for.js";
5
+ export * from "./terminal.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { Terminal } from "./terminal.js";
2
+ /**
3
+ * Whether text can be redrawn in place on this terminal.
4
+ *
5
+ * `cursorUp` counts rows, so the arithmetic only holds while every written line
6
+ * occupies exactly one — which needs a width to clamp against — and while the
7
+ * region stays within reach of the cursor, which needs a height.
8
+ */
9
+ export declare function isRedrawable(terminal: Terminal): boolean;
10
+ /**
11
+ * How many rows a live region may occupy on this terminal.
12
+ *
13
+ * One less than the viewport: the row the cursor rests on after the last line
14
+ * is written belongs to the region too, and `cursorUp` cannot reach past the
15
+ * top of the screen. A caller choosing _what_ to show budgets against this; the
16
+ * region enforces it regardless.
17
+ */
18
+ export declare function liveRegionHeight(terminal: Terminal): number;
19
+ /**
20
+ * Emits the next frame of a live region: erases whatever the previous call
21
+ * wrote, appends any permanent lines, then draws the region again.
22
+ *
23
+ * @param region Redrawn in place, and erased by the following call.
24
+ * @param scrollback Written above the region and kept.
25
+ * @returns The text to write. Empty when there is nothing to do.
26
+ */
27
+ export type LiveRegion = (region: readonly string[], scrollback?: readonly string[]) => string;
28
+ /**
29
+ * A live region bound to one terminal.
30
+ *
31
+ * Closes over the height of the last frame, which is the only thing that makes
32
+ * the next erase correct — so one region owns one cursor position, and two
33
+ * regions must never share a stream.
34
+ *
35
+ * Entries are split on newlines first: a caller renders one _item_ per entry —
36
+ * an event, a whole table — and an entry holding two lines occupies two rows,
37
+ * which the erase has to count and the clamp has to measure separately.
38
+ *
39
+ * Lines are clamped to the terminal's width, because a wrapped line occupies
40
+ * two rows and leaves the erase one short; and the region is clamped to `rows -
41
+ * 1`, because `cursorUp` cannot reach above the viewport and anything that
42
+ * scrolled off would be stranded in scrollback. A region that overflows keeps
43
+ * its **last** lines: those are the newest, and the ones it drops were never
44
+ * written, so there is nothing left behind.
45
+ *
46
+ * Committing a finished region is the same operation with the finished text as
47
+ * `scrollback` and an empty region.
48
+ */
49
+ export declare function createLiveRegion(terminal: Terminal): LiveRegion;
50
+ //# sourceMappingURL=live-region.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-region.d.ts","sourceRoot":"","sources":["../src/live-region.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAExD;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAE3D;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,CACxB,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,UAAU,CAAC,EAAE,SAAS,MAAM,EAAE,KAC1B,MAAM,CAAC;AAEZ;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,QAAQ,GAAG,UAAU,CAoB/D"}
@@ -0,0 +1,92 @@
1
+ import { cursorUp, ERASE_FROM_CURSOR_TO_END, stripAnsiEscapes, } from "@ac-kit/format-ansi";
2
+ import { truncateToWidth, visibleWidth } from "@ac-kit/format-monospace";
3
+ /**
4
+ * Whether text can be redrawn in place on this terminal.
5
+ *
6
+ * `cursorUp` counts rows, so the arithmetic only holds while every written line
7
+ * occupies exactly one — which needs a width to clamp against — and while the
8
+ * region stays within reach of the cursor, which needs a height.
9
+ */
10
+ export function isRedrawable(terminal) {
11
+ return terminal.interactive && terminal.columns > 0 && terminal.rows > 1;
12
+ }
13
+ /**
14
+ * How many rows a live region may occupy on this terminal.
15
+ *
16
+ * One less than the viewport: the row the cursor rests on after the last line
17
+ * is written belongs to the region too, and `cursorUp` cannot reach past the
18
+ * top of the screen. A caller choosing _what_ to show budgets against this; the
19
+ * region enforces it regardless.
20
+ */
21
+ export function liveRegionHeight(terminal) {
22
+ return Math.max(terminal.rows - 1, 0);
23
+ }
24
+ /**
25
+ * A live region bound to one terminal.
26
+ *
27
+ * Closes over the height of the last frame, which is the only thing that makes
28
+ * the next erase correct — so one region owns one cursor position, and two
29
+ * regions must never share a stream.
30
+ *
31
+ * Entries are split on newlines first: a caller renders one _item_ per entry —
32
+ * an event, a whole table — and an entry holding two lines occupies two rows,
33
+ * which the erase has to count and the clamp has to measure separately.
34
+ *
35
+ * Lines are clamped to the terminal's width, because a wrapped line occupies
36
+ * two rows and leaves the erase one short; and the region is clamped to `rows -
37
+ * 1`, because `cursorUp` cannot reach above the viewport and anything that
38
+ * scrolled off would be stranded in scrollback. A region that overflows keeps
39
+ * its **last** lines: those are the newest, and the ones it drops were never
40
+ * written, so there is nothing left behind.
41
+ *
42
+ * Committing a finished region is the same operation with the finished text as
43
+ * `scrollback` and an empty region.
44
+ */
45
+ export function createLiveRegion(terminal) {
46
+ let previousHeight = 0;
47
+ return (region, scrollback) => {
48
+ const budget = liveRegionHeight(terminal);
49
+ const lines = physicalLines_(region);
50
+ const visible = lines.length > budget ? lines.slice(lines.length - budget) : lines;
51
+ let output = previousHeight > 0 ? erase_(previousHeight) : "";
52
+ for (const line of physicalLines_(scrollback ?? [])) {
53
+ output += `${clamp_(line, terminal.columns)}\n`;
54
+ }
55
+ for (const line of visible) {
56
+ output += `${clamp_(line, terminal.columns)}\n`;
57
+ }
58
+ previousHeight = visible.length;
59
+ return output;
60
+ };
61
+ }
62
+ /** One entry per row on screen, whatever the caller batched into a string. */
63
+ function physicalLines_(entries) {
64
+ const lines = [];
65
+ for (const entry of entries) {
66
+ for (const line of entry.replace(/\n$/, "").split("\n")) {
67
+ lines.push(line);
68
+ }
69
+ }
70
+ return lines;
71
+ }
72
+ function erase_(height) {
73
+ return cursorUp(height) + ERASE_FROM_CURSOR_TO_END;
74
+ }
75
+ /**
76
+ * `visibleWidth` strips control bytes but not the rest of an SGR sequence, so
77
+ * `[38;2;220;50;47m` still counts as printable text — measuring a styled line
78
+ * directly would cut a short line to nothing. Measure the stripped text, and
79
+ * only lose the styling on a line that has to be cut anyway; a cut landing
80
+ * inside a sequence would otherwise print its digits as text.
81
+ */
82
+ function clamp_(line, columns) {
83
+ if (columns <= 0) {
84
+ return line;
85
+ }
86
+ const stripped = stripAnsiEscapes(line);
87
+ if (visibleWidth(stripped) <= columns) {
88
+ return line;
89
+ }
90
+ return truncateToWidth(stripped, columns);
91
+ }
92
+ //# sourceMappingURL=live-region.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"live-region.js","sourceRoot":"","sources":["../src/live-region.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EACR,wBAAwB,EACxB,gBAAgB,GAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAIzE;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,QAAkB;IAC9C,OAAO,QAAQ,CAAC,WAAW,IAAI,QAAQ,CAAC,OAAO,GAAG,CAAC,IAAI,QAAQ,CAAC,IAAI,GAAG,CAAC,CAAC;AAC1E,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAkB;IAClD,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;AACvC,CAAC;AAeD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAkB;IAClD,IAAI,cAAc,GAAG,CAAC,CAAC;IAEvB,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC;QAC1C,MAAM,KAAK,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;QACrC,MAAM,OAAO,GACZ,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QAEpE,IAAI,MAAM,GAAG,cAAc,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9D,KAAK,MAAM,IAAI,IAAI,cAAc,CAAC,UAAU,IAAI,EAAE,CAAC,EAAE,CAAC;YACrD,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC;QACjD,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;YAC5B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC;QACjD,CAAC;QAED,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;QAChC,OAAO,MAAM,CAAC;IACf,CAAC,CAAC;AACH,CAAC;AAED,8EAA8E;AAC9E,SAAS,cAAc,CAAC,OAA0B;IACjD,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACzD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAED,SAAS,MAAM,CAAC,MAAc;IAC7B,OAAO,QAAQ,CAAC,MAAM,CAAC,GAAG,wBAAwB,CAAC;AACpD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,MAAM,CAAC,IAAY,EAAE,OAAe;IAC5C,IAAI,OAAO,IAAI,CAAC,EAAE,CAAC;QAClB,OAAO,IAAI,CAAC;IACb,CAAC;IAED,MAAM,QAAQ,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACxC,IAAI,YAAY,CAAC,QAAQ,CAAC,IAAI,OAAO,EAAE,CAAC;QACvC,OAAO,IAAI,CAAC;IACb,CAAC;IACD,OAAO,eAAe,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { SgrStyleName } from "@ac-kit/format-ansi";
2
+ import type { Rgb8 } from "@ac-kit/math-color";
3
+ import type { Terminal } from "./terminal.js";
4
+ /**
5
+ * What a piece of text means, rather than what colour it should be.
6
+ *
7
+ * Shares its vocabulary with {@link GlyphRole} wherever the two overlap, so a
8
+ * glyph and the colour beside it are asked for by the same name. Roles a glyph
9
+ * has no answer for (`muted`, `critical`) and glyphs no colour applies to
10
+ * (`bullet`, `more`) are why the two are separate lists rather than one.
11
+ */
12
+ export type SemanticRole =
13
+ /** De-emphasised: a detail, a value not settled yet, a row still running. */
14
+ "muted" | "info" | "success" | "warning" | "error"
15
+ /** Severe enough to take a background rather than only a foreground. */
16
+ | "critical" | "winner" | "better" | "worse" | "unchanged";
17
+ /**
18
+ * Colour alone — SGR attributes stay at the call site.
19
+ *
20
+ * `bold` on a log level's label and `bold` on a table's winning row are the
21
+ * caller's emphasis decisions, not properties of what the text means, and
22
+ * baking them in would make a palette impossible to reuse across the two.
23
+ */
24
+ export type SemanticStyle = {
25
+ readonly foreground?: Rgb8;
26
+ readonly background?: Rgb8;
27
+ };
28
+ export type SemanticPalette = Readonly<Record<SemanticRole, SemanticStyle>>;
29
+ /**
30
+ * The default palette.
31
+ *
32
+ * Chosen for a dark background. Pass a palette of your own to
33
+ * {@link styleSemanticFor}/{@link semanticStyle} for a light one.
34
+ */
35
+ export declare const SEMANTIC_PALETTE: SemanticPalette;
36
+ /** The colour a role carries, for a caller that styles text itself. */
37
+ export declare function semanticStyle(role: SemanticRole, palette?: SemanticPalette): SemanticStyle;
38
+ export type StyleSemanticOptions = {
39
+ /** SGR attributes to apply alongside the role's colour. */
40
+ readonly styles?: readonly SgrStyleName[];
41
+ readonly palette?: SemanticPalette;
42
+ };
43
+ /** Styles `text` as `role`, downsampled to what `terminal` can render. */
44
+ export declare function styleSemanticFor(terminal: Terminal, text: string, role: SemanticRole, options?: StyleSemanticOptions): string;
45
+ //# sourceMappingURL=semantic-palette.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semantic-palette.d.ts","sourceRoot":"","sources":["../src/semantic-palette.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG/C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY;AACvB,6EAA6E;AAC3E,OAAO,GACP,MAAM,GACN,SAAS,GACT,SAAS,GACT,OAAO;AACT,wEAAwE;GACtE,UAAU,GACV,QAAQ,GACR,QAAQ,GACR,OAAO,GACP,WAAW,CAAC;AAEf;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG;IAC3B,QAAQ,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC;IAC3B,QAAQ,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,aAAa,CAAC,CAAC,CAAC;AAE5E;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,EAAE,eAc9B,CAAC;AAEF,uEAAuE;AACvE,wBAAgB,aAAa,CAC5B,IAAI,EAAE,YAAY,EAClB,OAAO,GAAE,eAAkC,GACzC,aAAa,CAEf;AAED,MAAM,MAAM,oBAAoB,GAAG;IAClC,2DAA2D;IAC3D,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,YAAY,EAAE,CAAC;IAC1C,QAAQ,CAAC,OAAO,CAAC,EAAE,eAAe,CAAC;CACnC,CAAC;AAEF,0EAA0E;AAC1E,wBAAgB,gBAAgB,CAC/B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,YAAY,EAClB,OAAO,CAAC,EAAE,oBAAoB,GAC5B,MAAM,CAKR"}
@@ -0,0 +1,34 @@
1
+ import { styleTextFor } from "./style-text-for.js";
2
+ /**
3
+ * The default palette.
4
+ *
5
+ * Chosen for a dark background. Pass a palette of your own to
6
+ * {@link styleSemanticFor}/{@link semanticStyle} for a light one.
7
+ */
8
+ export const SEMANTIC_PALETTE = {
9
+ muted: { foreground: { r8: 128, g8: 128, b8: 128 } },
10
+ info: { foreground: { r8: 88, g8: 166, b8: 255 } },
11
+ success: { foreground: { r8: 0, g8: 200, b8: 0 } },
12
+ warning: { foreground: { r8: 220, g8: 180, b8: 0 } },
13
+ error: { foreground: { r8: 220, g8: 50, b8: 47 } },
14
+ critical: {
15
+ foreground: { r8: 255, g8: 255, b8: 255 },
16
+ background: { r8: 180, g8: 0, b8: 0 },
17
+ },
18
+ winner: { foreground: { r8: 255, g8: 215, b8: 0 } },
19
+ better: { foreground: { r8: 0, g8: 200, b8: 0 } },
20
+ worse: { foreground: { r8: 220, g8: 50, b8: 47 } },
21
+ unchanged: { foreground: { r8: 128, g8: 128, b8: 128 } },
22
+ };
23
+ /** The colour a role carries, for a caller that styles text itself. */
24
+ export function semanticStyle(role, palette = SEMANTIC_PALETTE) {
25
+ return palette[role];
26
+ }
27
+ /** Styles `text` as `role`, downsampled to what `terminal` can render. */
28
+ export function styleSemanticFor(terminal, text, role, options) {
29
+ return styleTextFor(terminal, text, {
30
+ ...semanticStyle(role, options?.palette),
31
+ styles: options?.styles,
32
+ });
33
+ }
34
+ //# sourceMappingURL=semantic-palette.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"semantic-palette.js","sourceRoot":"","sources":["../src/semantic-palette.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAuCnD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAoB;IAChD,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE;IACpD,IAAI,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE;IAClD,OAAO,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;IAClD,OAAO,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;IACpD,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;IAClD,QAAQ,EAAE;QACT,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE;QACzC,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE;KACrC;IACD,MAAM,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;IACnD,MAAM,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE;IACjD,KAAK,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE;IAClD,SAAS,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE;CACxD,CAAC;AAEF,uEAAuE;AACvE,MAAM,UAAU,aAAa,CAC5B,IAAkB,EAClB,OAAO,GAAoB,gBAAgB;IAE3C,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;AACtB,CAAC;AAQD,0EAA0E;AAC1E,MAAM,UAAU,gBAAgB,CAC/B,QAAkB,EAClB,IAAY,EACZ,IAAkB,EAClB,OAA8B;IAE9B,OAAO,YAAY,CAAC,QAAQ,EAAE,IAAI,EAAE;QACnC,GAAG,aAAa,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC;QACxC,MAAM,EAAE,OAAO,EAAE,MAAM;KACvB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { type StyleTextOptions } from "@ac-kit/format-ansi";
2
+ import type { Terminal } from "./terminal.js";
3
+ /**
4
+ * `styleText`, reading `colorDepth` off `terminal` instead of the caller
5
+ * passing it.
6
+ */
7
+ export declare function styleTextFor(terminal: Terminal, text: string, options: Omit<StyleTextOptions, "colorDepth">): string;
8
+ //# sourceMappingURL=style-text-for.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"style-text-for.d.ts","sourceRoot":"","sources":["../src/style-text-for.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C;;;GAGG;AACH,wBAAgB,YAAY,CAC3B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,IAAI,CAAC,gBAAgB,EAAE,YAAY,CAAC,GAC3C,MAAM,CAMR"}
@@ -0,0 +1,12 @@
1
+ import { styleText } from "@ac-kit/format-ansi";
2
+ /**
3
+ * `styleText`, reading `colorDepth` off `terminal` instead of the caller
4
+ * passing it.
5
+ */
6
+ export function styleTextFor(terminal, text, options) {
7
+ if (!terminal.interactive) {
8
+ return text;
9
+ }
10
+ return styleText(text, { ...options, colorDepth: terminal.colorDepth });
11
+ }
12
+ //# sourceMappingURL=style-text-for.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"style-text-for.js","sourceRoot":"","sources":["../src/style-text-for.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyB,MAAM,qBAAqB,CAAC;AAIvE;;;GAGG;AACH,MAAM,UAAU,YAAY,CAC3B,QAAkB,EAClB,IAAY,EACZ,OAA6C;IAE7C,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO,SAAS,CAAC,IAAI,EAAE,EAAE,GAAG,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,44 @@
1
+ import type { IEventDispatcher } from "@ac-kit/async";
2
+ import { ColorDepth } from "@ac-kit/format-ansi";
3
+ /**
4
+ * Terminal properties a rendering sink needs, injected rather than read from
5
+ * `process.stdout` directly so the renderer stays portable and testable without
6
+ * a pty. The Node adapter lives in `@ac-kit/app-system`.
7
+ */
8
+ export type Terminal = {
9
+ /** Whether the terminal is interactive (i.e., connected to a TTY). */
10
+ interactive: boolean;
11
+ /**
12
+ * Viewport width in columns. Load-bearing even outside a full-screen
13
+ * reporter: a live region wider than the viewport corrupts scrollback, so a
14
+ * renderer must clamp against it.
15
+ */
16
+ columns: number;
17
+ /**
18
+ * Viewport height. Load-bearing even outside a full-screen reporter: a live
19
+ * region taller than the viewport corrupts scrollback, so a renderer must
20
+ * clamp against it.
21
+ */
22
+ rows: number;
23
+ /** Color depth supported by the terminal. */
24
+ colorDepth: ColorDepth;
25
+ /** Whether the terminal supports Unicode characters. */
26
+ unicode: boolean;
27
+ /** Whether the terminal supports hyperlinks. */
28
+ hyperlinks: boolean;
29
+ /** Fires whenever `columns`/`rows` change. */
30
+ resize: IEventDispatcher;
31
+ /**
32
+ * Enter the alternate screen buffer. Present only on terminals that support
33
+ * it.
34
+ */
35
+ enterAltScreen?(): void;
36
+ /**
37
+ * Exit the alternate screen buffer. Present only on terminals that support
38
+ * it.
39
+ */
40
+ exitAltScreen?(): void;
41
+ /** Erases the screen and its scrollback. Present only when interactive. */
42
+ clear?(): void;
43
+ };
44
+ //# sourceMappingURL=terminal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal.d.ts","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG;IACtB,sEAAsE;IACtE,WAAW,EAAE,OAAO,CAAC;IAErB;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb,6CAA6C;IAC7C,UAAU,EAAE,UAAU,CAAC;IAEvB,wDAAwD;IACxD,OAAO,EAAE,OAAO,CAAC;IAEjB,gDAAgD;IAChD,UAAU,EAAE,OAAO,CAAC;IAEpB,8CAA8C;IAC9C,MAAM,EAAE,gBAAgB,CAAC;IAEzB;;;OAGG;IACH,cAAc,CAAC,IAAI,IAAI,CAAC;IACxB;;;OAGG;IACH,aAAa,CAAC,IAAI,IAAI,CAAC;IAEvB,2EAA2E;IAC3E,KAAK,CAAC,IAAI,IAAI,CAAC;CACf,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=terminal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"terminal.js","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@ac-kit/app-terminal",
3
+ "version": "0.1.0",
4
+ "description": "The Terminal descriptor a rendering sink needs — interactivity, dimensions, colour depth, resize events, alt-screen support — injected rather than read from process.stdout.",
5
+ "keywords": [
6
+ "ansi",
7
+ "color-depth",
8
+ "portable",
9
+ "terminal",
10
+ "tty"
11
+ ],
12
+ "homepage": "https://anthochamp.github.io/node-essentials/api/app-terminal/",
13
+ "bugs": {
14
+ "url": "https://github.com/anthochamp/node-essentials/issues"
15
+ },
16
+ "license": "BSD-3-Clause",
17
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/anthochamp/node-essentials.git",
21
+ "directory": "packages/app-terminal"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./dist/index.js",
29
+ "exports": {
30
+ ".": "./dist/index.js"
31
+ },
32
+ "publishConfig": {
33
+ "main": "./dist/index.js",
34
+ "exports": {
35
+ ".": "./dist/index.js"
36
+ },
37
+ "access": "public"
38
+ },
39
+ "scripts": {
40
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
41
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
42
+ "clean": "rimraf dist",
43
+ "build": "yarn clean && yarn compile",
44
+ "test": "yarn run -T vitest run",
45
+ "prepack": "yarn build"
46
+ },
47
+ "dependencies": {
48
+ "@ac-kit/async": "^0.1.0",
49
+ "@ac-kit/format-ansi": "^0.1.0",
50
+ "@ac-kit/format-monospace": "^0.1.0",
51
+ "@ac-kit/math-color": "^0.1.0",
52
+ "@types/web": "^0.0.354"
53
+ },
54
+ "devDependencies": {
55
+ "@ac-kit/tsconfig": "0.1.0",
56
+ "@ac-kit/typedoc-config": "0.1.0",
57
+ "@ac-kit/vitest-config": "0.1.0",
58
+ "@typescript/native": "typescript@7.0.2",
59
+ "rimraf": "6.1.3",
60
+ "typedoc": "0.28.20",
61
+ "typescript": "@typescript/typescript6@6.0.2"
62
+ },
63
+ "engines": {
64
+ "node": ">=24"
65
+ }
66
+ }