Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed

Versions 73 to 74

Compare

+47 −0 in 1 place
new file, 47 lines
1+// What the client prints is mostly text a server sent: version messages, news,
2+// CI logs, file paths, agent names. So nothing in it gets to drive the terminal.
3+// Tabs, newlines and colors (SGR, ESC[...m) pass. Colors pass only to a terminal
4+// with NO_COLOR unset, and are dropped otherwise. Every other control character,
5+// and with it every other escape sequence, is shown as text (\x1b, \x07, \u009b),
6+// so nothing vanishes silently.
7+
8+/** Whether colors pass: a terminal, and NO_COLOR unset or empty (no-color.org). */
9+export function colorsPass(stream: { isTTY?: boolean }, env: Readonly<Record<string, string | undefined>>): boolean {
10+ return stream.isTTY === true && !env.NO_COLOR;
11+}
13+/** The length of the SGR sequence (ESC [ digits ; : m) starting at i, or 0 when there isn't one. */
14+function sgrAt(text: string, i: number): number {
15+ if (text.charCodeAt(i) !== 0x1b || text[i + 1] !== "[") return 0;
16+ for (let j = i + 2; j < text.length && j < i + 64; j++) {
17+ const c = text[j]!;
18+ if (c === "m") return j - i + 1;
19+ if (!/[0-9;:]/.test(c)) return 0;
20+ }
21+ return 0;
22+}
24+/** Whether a character code is one the terminal acts on: C0 but tab and newline, DEL, and C1. */
25+const control = (c: number) => (c < 0x20 && c !== 0x09 && c !== 0x0a) || (c >= 0x7f && c <= 0x9f);
27+/** `text` as it is safe to print: colors kept only when `colors`, every other control character shown as text. */
28+export function terminalText(text: string, colors: boolean): string {
29+ let i = 0;
30+ while (i < text.length && !control(text.charCodeAt(i))) i++;
31+ if (i === text.length) return text;
32+ let out = text.slice(0, i);
33+ for (; i < text.length; i++) {
34+ const c = text.charCodeAt(i);
35+ if (!control(c)) {
36+ out += text[i];
37+ continue;
38+ }
39+ const sgr = sgrAt(text, i);
40+ if (sgr > 0) {
41+ if (colors) out += text.slice(i, i + sgr);
42+ i += sgr - 1;
43+ } else if (c === 0x0d && text.charCodeAt(i + 1) === 0x0a) out += "\r";
44+ else out += c < 0x80 ? `\\x${c.toString(16).padStart(2, "0")}` : `\\u${c.toString(16).padStart(4, "0")}`;
45+ }
46+ return out;
47+}
MCP
compare on this site's /mcp
CLI
abso diff 73 74 client/src/terminal-text.ts --json
JSON
/christopher/abso-core/compare/73..74?file=client%2Fsrc%2Fterminal-text.ts&format=json