aegiscode 6.6.1 → 6.7.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/README.md +46 -0
- package/bin/aegiscode.js +87 -0
- package/package.json +1 -1
- package/src/app.js +156 -11
- package/src/art.js +114 -60
- package/src/autonomous.js +8 -2
- package/src/caps.js +597 -0
- package/src/chatflow.js +59 -6
- package/src/cloudsync.js +86 -6
- package/src/commands.js +243 -23
- package/src/config.js +87 -0
- package/src/diff.js +330 -0
- package/src/diffview.js +286 -0
- package/src/events.js +21 -10
- package/src/panels.js +59 -1
- package/src/render.js +39 -17
- package/src/screen.js +20 -20
- package/src/theme.js +334 -125
- package/vendor/desktop/lib/local/autonomous.js +45 -4
- package/vendor/desktop/lib/local/engine.js +38 -0
- package/vendor/desktop/lib/local/queue.js +4 -0
- package/vendor/desktop/lib/local/session-rounds.js +19 -2
package/src/caps.js
ADDED
|
@@ -0,0 +1,597 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Terminal capabilities — ONE probe, resolved once, consulted by everything.
|
|
5
|
+
*
|
|
6
|
+
* The UI used to decide how to draw itself from `process.platform`: Windows got
|
|
7
|
+
* the ASCII stencil, macOS got a half-stencil, Linux got the native mark. That
|
|
8
|
+
* is the wrong axis. The platform of the *process* says nothing about the
|
|
9
|
+
* terminal it is talking to, and the two most common real-world runtimes prove
|
|
10
|
+
* it:
|
|
11
|
+
*
|
|
12
|
+
* · PowerShell *inside Windows Terminal* (`WT_SESSION` set) draws
|
|
13
|
+
* █▓▒░▐▛▜▌▝▘ and ✦ exactly like zsh does — but `process.platform` is still
|
|
14
|
+
* `win32`, so it was being handed the hyphen-and-hash ASCII mark.
|
|
15
|
+
* · `zsh` over SSH from a Windows box, inside `mintty`/`ConEmu`, is `linux`
|
|
16
|
+
* on the far side and `win32` on the client — the mark must be chosen once,
|
|
17
|
+
* from what the *terminal* can draw.
|
|
18
|
+
*
|
|
19
|
+
* So the decision is made from the environment the process is actually running
|
|
20
|
+
* in — `TERM`, `COLORTERM`, `TERM_PROGRAM`, the Windows terminal markers
|
|
21
|
+
* (`WT_SESSION`, `ConEmuANSI`, `ANSICON`, …), the locale's character set, and
|
|
22
|
+
* whether stdout is a TTY. Every axis is overridable, because only the person
|
|
23
|
+
* looking at the screen can settle the cases no environment variable can
|
|
24
|
+
* (a Windows console with a font that lacks the block glyphs, a macOS terminal
|
|
25
|
+
* that renders ✦ as a wide emoji).
|
|
26
|
+
*
|
|
27
|
+
* The axes:
|
|
28
|
+
* glyphs 'unicode' | 'ascii' — can the mark's palette be drawn at all?
|
|
29
|
+
* face 'native' | 'edges' — do the quartile edges ▐▛▜▌▝▘ exist in the
|
|
30
|
+
* terminal's font, or must they be stencilled?
|
|
31
|
+
* star 'native' | 'narrow' — is ✦ a single-cell text glyph, or does the
|
|
32
|
+
* terminal fall back to a wide emoji?
|
|
33
|
+
* depth 24 | 8 | 4 | 0 — truecolor, 256, the 16 ANSI slots, none
|
|
34
|
+
* vt boolean — are cursor/erase escapes honoured?
|
|
35
|
+
* cols/rows — the frame, with `AEGIS_WIDTH`/`COLUMNS` over
|
|
36
|
+
* the reported size and a stable 80 fallback,
|
|
37
|
+
* wideRunes — runes the terminal draws two cells wide
|
|
38
|
+
* that the wcwidth table does not know about
|
|
39
|
+
* (`AEGIS_WIDE_RUNES=✦`), so every width
|
|
40
|
+
* decision stays exact.
|
|
41
|
+
*
|
|
42
|
+
* Overrides (all read from the environment, all documented in README.md):
|
|
43
|
+
* AEGIS_ART=unicode|ascii AEGIS_ASCII=1 AEGIS_UNICODE=0|1
|
|
44
|
+
* AEGIS_STAR=native|narrow AEGIS_COLOR=0|1|2|3
|
|
45
|
+
* AEGIS_VT=0|1 AEGIS_WIDTH=<cols> AEGIS_WIDE_RUNES=<runes>
|
|
46
|
+
*
|
|
47
|
+
* `caps()` is cached; `setCaps()` re-resolves and notifies subscribers, which is
|
|
48
|
+
* how `theme.js` re-materialises its palette and glyph table when the user runs
|
|
49
|
+
* `aegiscode --ascii` (the flag is applied before the app is required, so the
|
|
50
|
+
* whole session draws with the pass the user asked for) or toggles it in the
|
|
51
|
+
* `/terminal` command.
|
|
52
|
+
*
|
|
53
|
+
* This module requires nothing — no cycles, and it is safe to load first.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
// ── the cell-width core ──────────────────────────────────────────────────────
|
|
57
|
+
// Real terminal cell width (wcwidth-style), copied verbatim from
|
|
58
|
+
// aegiscodex-dev/src/screen.js. Combining marks and variation selectors occupy
|
|
59
|
+
// no cell; CJK fullwidth forms and emoji-presentation glyphs occupy two;
|
|
60
|
+
// everything else one. A plain code-point count misaligns every width decision
|
|
61
|
+
// the moment a line contains CJK, emoji or a terminal's wide fallback glyph.
|
|
62
|
+
const RE_ZERO = /[\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF\u20D0-\u20FF\uFE00-\uFE0F\uFE20-\uFE2F]/u;
|
|
63
|
+
const RE_WIDE = /[\u1100-\u115F\u2E80-\u303E\u3041-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uA000-\uA4CF\uA960-\uA97F\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE4F\uFF00-\uFF60\uFFE0-\uFFE6\u{1F1E6}-\u{1F1FF}\u{1F300}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}\u{1FA70}-\u{1FAFF}\u{20000}-\u{3FFFD}]/u;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Windows terminals that draw the full mark. `WT_SESSION` is set by Windows
|
|
67
|
+
* Terminal for every shell it hosts (pwsh, cmd, wsl, git-bash); `TERM_PROGRAM`
|
|
68
|
+
* by VS Code and the other Electron/ConPTY hosts; the rest are the classic
|
|
69
|
+
* ConPTY-era consoles. Presence of any of them means the console is VT-capable
|
|
70
|
+
* and its default font carries the block-element range.
|
|
71
|
+
*/
|
|
72
|
+
const MODERN_WINDOWS = [
|
|
73
|
+
'WT_SESSION',
|
|
74
|
+
'WT_PROFILE_ID',
|
|
75
|
+
'ConEmuANSI',
|
|
76
|
+
'ANSICON',
|
|
77
|
+
'TERM_PROGRAM',
|
|
78
|
+
'WEZTERM_PANE',
|
|
79
|
+
'ALACRITTY_SOCKET',
|
|
80
|
+
'KITTY_WINDOW_ID',
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* `TERM_PROGRAM` values whose font renders the whole palette natively, `✦`
|
|
85
|
+
* included. `Apple_Terminal` is deliberately NOT here — see `resolveGlyphs`.
|
|
86
|
+
*/
|
|
87
|
+
const NATIVE_TERM_PROGRAMS = new Set([
|
|
88
|
+
'vscode',
|
|
89
|
+
'vscode-insiders',
|
|
90
|
+
'iTerm.app',
|
|
91
|
+
'WezTerm',
|
|
92
|
+
'kitty',
|
|
93
|
+
'ghostty',
|
|
94
|
+
'Alacritty',
|
|
95
|
+
'Hyper',
|
|
96
|
+
'Tabby',
|
|
97
|
+
'WarpTerminal',
|
|
98
|
+
'Warp',
|
|
99
|
+
'rio',
|
|
100
|
+
'mintty',
|
|
101
|
+
'JetBrains-JediTerm',
|
|
102
|
+
]);
|
|
103
|
+
|
|
104
|
+
const TRUTHY_RE = /^(1|true|yes|on)$/i;
|
|
105
|
+
|
|
106
|
+
/** Is an env value "on"? (`WT_SESSION=` with no value still counts.) */
|
|
107
|
+
function truthy(v) {
|
|
108
|
+
if (v == null) return false;
|
|
109
|
+
return v === '' || TRUTHY_RE.test(String(v).trim());
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** First defined, non-empty value among `names`. */
|
|
113
|
+
function firstEnv(env, names) {
|
|
114
|
+
for (const n of names) {
|
|
115
|
+
const v = env[n];
|
|
116
|
+
if (v != null && v !== '') return { name: n, value: String(v) };
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function modernWindows(env) {
|
|
122
|
+
return firstEnv(env, MODERN_WINDOWS);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The locale's character set. POSIX terminals signal UTF-8 support here and
|
|
127
|
+
* nowhere else: a terminal launched with `LANG=C` cannot draw ░▒▓, and no
|
|
128
|
+
* amount of TERM sniffing will tell you that.
|
|
129
|
+
*/
|
|
130
|
+
function localeOf(env) {
|
|
131
|
+
const hit = firstEnv(env, ['LC_ALL', 'LC_CTYPE', 'LANG']);
|
|
132
|
+
return hit ? hit.value : '';
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function utf8Locale(env) {
|
|
136
|
+
const loc = localeOf(env);
|
|
137
|
+
if (!loc) return true; // unset: every shell we ship to defaults to UTF-8
|
|
138
|
+
return /utf-?8/i.test(loc);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The explicit art mode, or 'auto'. */
|
|
142
|
+
function artMode(env) {
|
|
143
|
+
const raw = String(env.AEGIS_ART || '').trim().toLowerCase();
|
|
144
|
+
if (raw === 'ascii' || raw === 'plain' || raw === 'text') return 'ascii';
|
|
145
|
+
if (raw === 'unicode' || raw === 'native' || raw === 'utf8' || raw === 'utf-8') return 'unicode';
|
|
146
|
+
if (truthy(env.AEGIS_ASCII) || env.AEGIS_UNICODE === '0') return 'ascii';
|
|
147
|
+
if (truthy(env.AEGIS_UNICODE)) return 'unicode';
|
|
148
|
+
return 'auto';
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Resolve what the mark may be drawn with. Order matters: an explicit request
|
|
153
|
+
* always wins, then the terminal's own declarations, then the two signals that
|
|
154
|
+
* mean "this cannot work" (a dumb terminal, a non-UTF-8 locale), then the
|
|
155
|
+
* legacy-Windows console.
|
|
156
|
+
*/
|
|
157
|
+
function resolveGlyphs(env, platform) {
|
|
158
|
+
const starPref = String(env.AEGIS_STAR || '').trim().toLowerCase();
|
|
159
|
+
const star = /^(native|wide)$/.test(starPref)
|
|
160
|
+
? 'native'
|
|
161
|
+
: /^(narrow|ascii)$/.test(starPref)
|
|
162
|
+
? 'narrow'
|
|
163
|
+
: null;
|
|
164
|
+
const mode = artMode(env);
|
|
165
|
+
|
|
166
|
+
if (mode === 'ascii') {
|
|
167
|
+
return { glyphs: 'ascii', face: 'edges', star: 'narrow', reason: 'AEGIS_ART=ascii' };
|
|
168
|
+
}
|
|
169
|
+
if (mode === 'unicode') {
|
|
170
|
+
return { glyphs: 'unicode', face: 'native', star: star || 'native', reason: 'AEGIS_ART=unicode' };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (String(env.TERM || '').toLowerCase() === 'dumb') {
|
|
174
|
+
return { glyphs: 'ascii', face: 'edges', star: 'narrow', reason: 'TERM=dumb' };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (platform === 'win32') {
|
|
178
|
+
const modern = modernWindows(env);
|
|
179
|
+
if (!modern) {
|
|
180
|
+
return {
|
|
181
|
+
glyphs: 'ascii',
|
|
182
|
+
face: 'edges',
|
|
183
|
+
star: 'narrow',
|
|
184
|
+
reason: 'legacy Windows console (no WT_SESSION/ConEmu/TERM_PROGRAM) — Consolas has no block glyphs',
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
// A ConPTY host (Windows Terminal, VS Code, ConEmu) renders the block
|
|
188
|
+
// palette, and ConPTY does font fallback — Cascadia Mono lacks ✦ but the
|
|
189
|
+
// system symbol fonts carry it single-width, so the star is drawn natively
|
|
190
|
+
// and the welcome mark is byte-identical to the one zsh prints. That parity
|
|
191
|
+
// is the point: `star: 'narrow'` here would swap all eleven ✦ in the mark
|
|
192
|
+
// for '*', which is the difference a PowerShell user sees.
|
|
193
|
+
//
|
|
194
|
+
// The escape hatches are for the font that does *not* fall back cleanly:
|
|
195
|
+
// AEGIS_STAR=narrow swaps ✦ for '*', AEGIS_WIDE_RUNES=✦ tells the width
|
|
196
|
+
// maths the glyph is two cells wide (which is what a bad fallback actually
|
|
197
|
+
// breaks — the row alignment, not the rune).
|
|
198
|
+
return {
|
|
199
|
+
glyphs: 'unicode',
|
|
200
|
+
face: 'native',
|
|
201
|
+
star: star || 'native',
|
|
202
|
+
reason: `Windows VT host (${modern.name})`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!utf8Locale(env)) {
|
|
207
|
+
return { glyphs: 'ascii', face: 'edges', star: 'narrow', reason: `locale is not UTF-8 (${localeOf(env)})` };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const program = String(env.TERM_PROGRAM || '');
|
|
211
|
+
if (platform === 'darwin' && program === 'Apple_Terminal') {
|
|
212
|
+
// Terminal.app's font stack has no ▐▛▜▌▝▘ and renders ✦ with the emoji
|
|
213
|
+
// fallback (two cells), so it gets the edge pass and a narrow star — the
|
|
214
|
+
// shipped behaviour on macOS, now expressed as a capability, not a platform.
|
|
215
|
+
return { glyphs: 'unicode', face: 'edges', star: 'narrow', reason: 'Apple_Terminal font stack' };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return {
|
|
219
|
+
glyphs: 'unicode',
|
|
220
|
+
face: 'native',
|
|
221
|
+
star: star || 'native',
|
|
222
|
+
reason: program ? `${program} terminal` : `TERM=${env.TERM || 'unset'}`,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Colour depth in bits per channel: 24 truecolor, 8 the 256-colour cube, 4 the
|
|
228
|
+
* 16 ANSI slots, 0 no colour at all.
|
|
229
|
+
*
|
|
230
|
+
* The default is truecolor, because that is the honest answer for every
|
|
231
|
+
* terminal we can name: the 256-colour terminals in use today all pass 24-bit
|
|
232
|
+
* SGR through, and a terminal that cannot simply downsamples it itself — which
|
|
233
|
+
* is the correct authority for the decision. Only a provably narrow terminal
|
|
234
|
+
* (legacy Windows console, `TERM=linux|vt100|ansi`, `NO_COLOR`) is downgraded,
|
|
235
|
+
* so the palette cannot be silently flattened on a machine that could have
|
|
236
|
+
* shown it.
|
|
237
|
+
*/
|
|
238
|
+
function resolveDepth(env, platform, isTTY) {
|
|
239
|
+
if (env.NO_COLOR != null && env.NO_COLOR !== '') return { depth: 0, reason: 'NO_COLOR' };
|
|
240
|
+
const forced = String(env.FORCE_COLOR || '').trim();
|
|
241
|
+
if (forced === '0') return { depth: 0, reason: 'FORCE_COLOR=0' };
|
|
242
|
+
if (/^[123]$/.test(forced)) {
|
|
243
|
+
return { depth: forced === '3' ? 24 : forced === '2' ? 8 : 4, reason: `FORCE_COLOR=${forced}` };
|
|
244
|
+
}
|
|
245
|
+
const aegis = String(env.AEGIS_COLOR || '').trim();
|
|
246
|
+
if (/^[0-3]$/.test(aegis)) {
|
|
247
|
+
return { depth: aegis === '0' ? 0 : aegis === '1' ? 4 : aegis === '2' ? 8 : 24, reason: `AEGIS_COLOR=${aegis}` };
|
|
248
|
+
}
|
|
249
|
+
if (String(env.COLORTERM || '').trim() !== '' && /^(truecolor|24bit)$/i.test(env.COLORTERM)) {
|
|
250
|
+
return { depth: 24, reason: `COLORTERM=${env.COLORTERM}` };
|
|
251
|
+
}
|
|
252
|
+
const term = String(env.TERM || '').toLowerCase();
|
|
253
|
+
if (term === 'dumb') return { depth: 0, reason: 'TERM=dumb' };
|
|
254
|
+
if (platform === 'win32') {
|
|
255
|
+
const modern = modernWindows(env);
|
|
256
|
+
if (modern) return { depth: 24, reason: `Windows VT host (${modern.name})` };
|
|
257
|
+
// The classic console: 16 slots, and only when it is actually a console.
|
|
258
|
+
if (isTTY) return { depth: 4, reason: 'legacy Windows console' };
|
|
259
|
+
return { depth: 24, reason: 'redirected output' };
|
|
260
|
+
}
|
|
261
|
+
if (/^(linux|vt100|vt220|ansi|cons25|sun|dumb)$/.test(term) || /^xterm-color$/.test(term)) {
|
|
262
|
+
return { depth: 4, reason: `TERM=${term}` };
|
|
263
|
+
}
|
|
264
|
+
if (env.AEGIS_FORCE_TRUECOLOR === '0' || env.TERM_PROGRAM === 'Apple_Terminal') {
|
|
265
|
+
// Terminal.app advertises truecolor and honours 24-bit SGR on every release
|
|
266
|
+
// that still runs, so it keeps its depth; the check exists for the older
|
|
267
|
+
// builds that dithered. AEGIS_FORCE_TRUECOLOR=0 is the user's override.
|
|
268
|
+
if (env.AEGIS_FORCE_TRUECOLOR === '0') return { depth: 8, reason: 'AEGIS_FORCE_TRUECOLOR=0' };
|
|
269
|
+
}
|
|
270
|
+
return { depth: 24, reason: term ? `TERM=${term}` : 'default' };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Cursor and erase control. Never claimed for a dumb terminal or redirected
|
|
275
|
+
* output on Windows (where `\x1b[2K` would be printed as text), and always
|
|
276
|
+
* assumed for a POSIX terminal — the live spinner line depends on it.
|
|
277
|
+
*/
|
|
278
|
+
function resolveVt(env, platform, isTTY) {
|
|
279
|
+
const pref = String(env.AEGIS_VT == null ? '' : env.AEGIS_VT).trim();
|
|
280
|
+
if (pref === '0') return { vt: false, reason: 'AEGIS_VT=0' };
|
|
281
|
+
if (pref === '1' || /^(true|yes|on)$/i.test(pref)) return { vt: true, reason: 'AEGIS_VT=1' };
|
|
282
|
+
if (String(env.TERM || '').toLowerCase() === 'dumb') return { vt: false, reason: 'TERM=dumb' };
|
|
283
|
+
if (platform === 'win32') {
|
|
284
|
+
const modern = modernWindows(env);
|
|
285
|
+
if (modern) return { vt: true, reason: `Windows VT host (${modern.name})` };
|
|
286
|
+
if (isTTY) return { vt: true, reason: 'console host (Node enables VT processing)' };
|
|
287
|
+
return { vt: false, reason: 'redirected output' };
|
|
288
|
+
}
|
|
289
|
+
return { vt: true, reason: 'POSIX terminal' };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The frame. `AEGIS_WIDTH`, then the size the terminal actually reports, then
|
|
294
|
+
* `COLUMNS`, then a fixed 80.
|
|
295
|
+
*
|
|
296
|
+
* The real report outranks `COLUMNS` on purpose: `COLUMNS` is a shell variable
|
|
297
|
+
* that bash exports and then *stops updating* on SIGWINCH, so honouring it over
|
|
298
|
+
* `stdout.columns` would pin a resized terminal to its old width. A fixed
|
|
299
|
+
* fallback rather than a platform guess keeps a redirected run on PowerShell
|
|
300
|
+
* byte-identical to the same run in zsh.
|
|
301
|
+
*/
|
|
302
|
+
function resolveSize(env, stdout) {
|
|
303
|
+
const forced = parseInt(env.AEGIS_WIDTH || '', 10);
|
|
304
|
+
const columns = parseInt(env.COLUMNS || '', 10);
|
|
305
|
+
const reported = stdout && Number.isFinite(stdout.columns) && stdout.columns > 0 ? stdout.columns : 0;
|
|
306
|
+
const rows = stdout && Number.isFinite(stdout.rows) && stdout.rows > 0 ? stdout.rows : 0;
|
|
307
|
+
const wanted = forced > 0 ? forced : reported || (columns > 0 ? columns : 80);
|
|
308
|
+
return {
|
|
309
|
+
cols: Math.max(20, Math.min(400, wanted)),
|
|
310
|
+
rows: Math.max(5, rows || 24),
|
|
311
|
+
reason: forced > 0 ? 'AEGIS_WIDTH' : reported ? 'stdout.columns' : columns > 0 ? 'COLUMNS' : 'default 80',
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Runes the terminal draws two cells wide that wcwidth does not know about. */
|
|
316
|
+
function resolveWideRunes(env) {
|
|
317
|
+
const out = new Map();
|
|
318
|
+
for (const ch of String(env.AEGIS_WIDE_RUNES || '')) {
|
|
319
|
+
if (ch.trim()) out.set(ch, 2);
|
|
320
|
+
}
|
|
321
|
+
return out;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Resolve every axis, purely, from an environment and a stream. Exported so the
|
|
326
|
+
* platform matrix is a plain function call in a test — no subprocess, no TTY.
|
|
327
|
+
*
|
|
328
|
+
* @param {object} [env] defaults to `process.env`
|
|
329
|
+
* @param {object} [stdout] defaults to `process.stdout`
|
|
330
|
+
* @param {string} [platform] defaults to `process.platform`
|
|
331
|
+
*/
|
|
332
|
+
function detect(env = process.env, stdout = process.stdout, platform = process.platform) {
|
|
333
|
+
const isTTY = !!(stdout && stdout.isTTY);
|
|
334
|
+
const glyph = resolveGlyphs(env, platform);
|
|
335
|
+
const color = resolveDepth(env, platform, isTTY);
|
|
336
|
+
const vt = resolveVt(env, platform, isTTY);
|
|
337
|
+
const size = resolveSize(env, stdout);
|
|
338
|
+
return {
|
|
339
|
+
platform,
|
|
340
|
+
isTTY,
|
|
341
|
+
term: String(env.TERM || ''),
|
|
342
|
+
termProgram: String(env.TERM_PROGRAM || ''),
|
|
343
|
+
locale: localeOf(env),
|
|
344
|
+
glyphs: glyph.glyphs,
|
|
345
|
+
face: glyph.face,
|
|
346
|
+
star: glyph.star,
|
|
347
|
+
depth: color.depth,
|
|
348
|
+
color: color.depth > 0,
|
|
349
|
+
vt: vt.vt,
|
|
350
|
+
cols: size.cols,
|
|
351
|
+
rows: size.rows,
|
|
352
|
+
eol: platform === 'win32' && !isTTY ? '\r\n' : '\n',
|
|
353
|
+
wideRunes: resolveWideRunes(env),
|
|
354
|
+
reasons: {
|
|
355
|
+
glyphs: glyph.reason,
|
|
356
|
+
depth: color.reason,
|
|
357
|
+
vt: vt.reason,
|
|
358
|
+
size: size.reason,
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// ── the resolved singleton ───────────────────────────────────────────────────
|
|
364
|
+
|
|
365
|
+
let cached = null;
|
|
366
|
+
let sizePinned = false;
|
|
367
|
+
/**
|
|
368
|
+
* The environment `setCaps()` last resolved against. The live size re-read has
|
|
369
|
+
* to use THIS, not `process.env`: the CLI entry point builds an environment
|
|
370
|
+
* *copy* (`{...process.env, AEGIS_WIDTH}`) so that `--width`/`--ascii` do not
|
|
371
|
+
* leak into the session's own child processes. Reading `process.env` in
|
|
372
|
+
* `caps()` therefore discarded every override — `aegiscode --width 66` reported
|
|
373
|
+
* `80x24 (default 80)` and rendered at 80.
|
|
374
|
+
*/
|
|
375
|
+
let sizeEnv = null;
|
|
376
|
+
const listeners = [];
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* The derived axes — glyphs, face, star, depth, vt, wideRunes — resolved once
|
|
380
|
+
* per process and re-resolved only by `setCaps()`. The frame is deliberately
|
|
381
|
+
* NOT part of this: `process.stdout.columns` changes on SIGWINCH, and the
|
|
382
|
+
* window is resized far more often than the palette. Caching the size is how a
|
|
383
|
+
* resized terminal ends up pinned to the width it had at module load, which is
|
|
384
|
+
* the one bug this split exists to prevent.
|
|
385
|
+
*/
|
|
386
|
+
function axes() {
|
|
387
|
+
if (!cached) cached = detect();
|
|
388
|
+
return cached;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* The resolved capabilities for this process.
|
|
393
|
+
*
|
|
394
|
+
* The size is read from the live stream on every call (unless pinned by
|
|
395
|
+
* `setCaps({cols,rows})` or `AEGIS_WIDTH`), so a resize is picked up by the
|
|
396
|
+
* next frame; everything else comes from the cache. `cols`/`rows` are written
|
|
397
|
+
* back onto the same object rather than replaced, so a holder that captured the
|
|
398
|
+
* result — `theme.js` keeps one — sees the new frame without re-subscribing.
|
|
399
|
+
*/
|
|
400
|
+
function caps() {
|
|
401
|
+
const c = axes();
|
|
402
|
+
if (!sizePinned) {
|
|
403
|
+
const size = resolveSize(sizeEnv || process.env, process.stdout);
|
|
404
|
+
c.cols = size.cols;
|
|
405
|
+
c.rows = size.rows;
|
|
406
|
+
c.reasons.size = size.reason;
|
|
407
|
+
}
|
|
408
|
+
// Surfaced so `/terminal` can warn that a pinned frame ignores resizes.
|
|
409
|
+
c.pinned = sizePinned;
|
|
410
|
+
return c;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Re-resolve — with `patch` applied on top when given — and tell subscribers.
|
|
415
|
+
* `theme.js` subscribes so its palette and glyph table follow a mid-session
|
|
416
|
+
* change (`/terminal ascii`), and so does the CLI entry point, which applies
|
|
417
|
+
* `--ascii` / `--no-color` before the app module is ever required.
|
|
418
|
+
* @param {object} [patch] partial caps, e.g. `{ glyphs: 'ascii', star: 'narrow' }`
|
|
419
|
+
* @param {object} [env]
|
|
420
|
+
*/
|
|
421
|
+
function setCaps(patch = null, env = process.env, stdout = process.stdout) {
|
|
422
|
+
cached = Object.assign(detect(env, stdout), patch || {});
|
|
423
|
+
// Every later frame resolves its size against this same environment, so an
|
|
424
|
+
// inherited `AEGIS_WIDTH`/`COLUMNS` override keeps working after a resize
|
|
425
|
+
// instead of silently reverting to the stream's own report.
|
|
426
|
+
sizeEnv = env || process.env;
|
|
427
|
+
// An explicit frame in the patch is a deliberate override, so it must survive
|
|
428
|
+
// the live re-read in `caps()`.
|
|
429
|
+
sizePinned = !!(patch && (patch.cols != null || patch.rows != null));
|
|
430
|
+
// A pinned frame is not re-read from the stream, so the reason the last
|
|
431
|
+
// `detect()` recorded (`stdout.columns`) describes where the size came from
|
|
432
|
+
// BEFORE the pin — the report would claim the width was the stream's when the
|
|
433
|
+
// user had just overridden it with `/terminal width`.
|
|
434
|
+
if (patch && patch.cols != null && cached.reasons) cached.reasons.size = 'pinned (override)';
|
|
435
|
+
for (const fn of listeners) {
|
|
436
|
+
try {
|
|
437
|
+
fn(cached);
|
|
438
|
+
} catch {
|
|
439
|
+
/* a listener must never break the session's capability resolution */
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return cached;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** Drop the cache so the next `caps()` re-probes (used by tests). */
|
|
446
|
+
function resetCaps() {
|
|
447
|
+
cached = null;
|
|
448
|
+
sizePinned = false;
|
|
449
|
+
sizeEnv = null;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** Subscribe to `setCaps`. Returns an unsubscribe function. */
|
|
453
|
+
function onCapsChange(fn) {
|
|
454
|
+
listeners.push(fn);
|
|
455
|
+
return () => {
|
|
456
|
+
const i = listeners.indexOf(fn);
|
|
457
|
+
if (i >= 0) listeners.splice(i, 1);
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// ── width, through the resolved capabilities ─────────────────────────────────
|
|
462
|
+
|
|
463
|
+
/** Cell width of one code point, honouring `caps.wideRunes`. */
|
|
464
|
+
function cellWidth(ch) {
|
|
465
|
+
// `axes()`, not `caps()`: this is called once per code point by `cells()`, on
|
|
466
|
+
// every pad and clip in the render path. Re-resolving the frame here would
|
|
467
|
+
// parse the environment thousands of times per frame to answer a question
|
|
468
|
+
// about fonts, so the hot path reads the cached wide-rune table directly.
|
|
469
|
+
const c = axes();
|
|
470
|
+
if (c.wideRunes && c.wideRunes.size) {
|
|
471
|
+
const o = c.wideRunes.get(ch);
|
|
472
|
+
if (o != null) return o;
|
|
473
|
+
}
|
|
474
|
+
if (RE_ZERO.test(ch)) return 0;
|
|
475
|
+
if (RE_WIDE.test(ch)) return 2;
|
|
476
|
+
return 1;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** Display width of a string in terminal cells (ANSI-free input). */
|
|
480
|
+
function cells(s) {
|
|
481
|
+
let n = 0;
|
|
482
|
+
for (const ch of String(s)) n += cellWidth(ch);
|
|
483
|
+
return n;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Pad a string with spaces to `n` cells (never truncates). */
|
|
487
|
+
function padCells(s, n) {
|
|
488
|
+
const d = n - cells(s);
|
|
489
|
+
return d > 0 ? s + ' '.repeat(d) : s;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// ── colour, through the resolved depth ──────────────────────────────────────
|
|
493
|
+
|
|
494
|
+
/** Nearest slot in the xterm-256 cube/greyscale ladder for an RGB triple. */
|
|
495
|
+
function rgbTo256(r, g, b) {
|
|
496
|
+
if (r === g && g === b) {
|
|
497
|
+
if (r < 8) return 16;
|
|
498
|
+
if (r > 248) return 231;
|
|
499
|
+
return 232 + Math.round(((r - 8) / 247) * 24);
|
|
500
|
+
}
|
|
501
|
+
const q = (v) => Math.round((v / 255) * 5);
|
|
502
|
+
return 16 + 36 * q(r) + 6 * q(g) + q(b);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** Nearest of the 8 base colours (the bright half is a bold/attribute matter). */
|
|
506
|
+
function rgbTo16(r, g, b) {
|
|
507
|
+
const named = [
|
|
508
|
+
[0, 0, 0, 30],
|
|
509
|
+
[205, 0, 0, 31],
|
|
510
|
+
[0, 205, 0, 32],
|
|
511
|
+
[205, 205, 0, 33],
|
|
512
|
+
[0, 0, 238, 34],
|
|
513
|
+
[205, 0, 205, 35],
|
|
514
|
+
[0, 205, 205, 36],
|
|
515
|
+
[229, 229, 229, 37],
|
|
516
|
+
];
|
|
517
|
+
let best = named[7];
|
|
518
|
+
let bestD = Infinity;
|
|
519
|
+
for (const c of named) {
|
|
520
|
+
const d = (r - c[0]) ** 2 + (g - c[1]) ** 2 + (b - c[2]) ** 2;
|
|
521
|
+
if (d < bestD) {
|
|
522
|
+
bestD = d;
|
|
523
|
+
best = c;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return best[3];
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* A foreground SGR prefix for the resolved depth: 24-bit, the 256-colour cube,
|
|
531
|
+
* the 16 ANSI slots, or nothing at all (`NO_COLOR`, `TERM=dumb`).
|
|
532
|
+
*
|
|
533
|
+
* Every colour in the UI goes through this — `theme.js` builds its palettes
|
|
534
|
+
* from these calls — so a terminal that cannot show truecolor gets a palette it
|
|
535
|
+
* *can* show instead of a wall of literal escape text.
|
|
536
|
+
*/
|
|
537
|
+
function rgb(r, g, b) {
|
|
538
|
+
const d = caps().depth;
|
|
539
|
+
if (d >= 24) return `\x1b[38;2;${r};${g};${b}m`;
|
|
540
|
+
if (d >= 8) return `\x1b[38;5;${rgbTo256(r, g, b)}m`;
|
|
541
|
+
if (d >= 4) return `\x1b[${rgbTo16(r, g, b)}m`;
|
|
542
|
+
return '';
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/** A background SGR prefix for the resolved depth (or nothing). */
|
|
546
|
+
function bg(r, g, b) {
|
|
547
|
+
const d = caps().depth;
|
|
548
|
+
if (d >= 24) return `\x1b[48;2;${r};${g};${b}m`;
|
|
549
|
+
if (d >= 8) return `\x1b[48;5;${rgbTo256(r, g, b)}m`;
|
|
550
|
+
if (d >= 4) return `\x1b[${rgbTo16(r, g, b) + 10}m`;
|
|
551
|
+
return '';
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// ── reporting (the /terminal command, `aegiscode --terminal`) ────────────────
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* The resolved capabilities as `[label, value]` rows — one shape, same on every
|
|
558
|
+
* platform, so a support paste from PowerShell reads like one from zsh.
|
|
559
|
+
*/
|
|
560
|
+
function describe(c = caps()) {
|
|
561
|
+
const eol = c.eol === '\r\n' ? '\\r\\n' : '\\n';
|
|
562
|
+
return [
|
|
563
|
+
['platform', `${c.platform}${c.isTTY ? '' : ' (no tty)'}`],
|
|
564
|
+
['terminal', c.termProgram || c.term || 'unknown'],
|
|
565
|
+
['locale', c.locale || 'unset'],
|
|
566
|
+
['mark', c.glyphs === 'ascii' ? 'ascii' : c.face === 'edges' ? 'unicode (edges stencilled)' : 'unicode'],
|
|
567
|
+
['star', c.star],
|
|
568
|
+
['color', c.depth === 0 ? 'none' : c.depth >= 24 ? 'truecolor' : c.depth >= 8 ? '256' : '16'],
|
|
569
|
+
['control', c.vt ? 'ansi' : 'plain'],
|
|
570
|
+
['size', `${c.cols}x${c.rows} (${c.reasons.size})`],
|
|
571
|
+
['eol', eol],
|
|
572
|
+
['why', `${c.reasons.glyphs}; ${c.reasons.depth}`],
|
|
573
|
+
];
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
module.exports = {
|
|
577
|
+
detect,
|
|
578
|
+
caps,
|
|
579
|
+
setCaps,
|
|
580
|
+
resetCaps,
|
|
581
|
+
onCapsChange,
|
|
582
|
+
cellWidth,
|
|
583
|
+
cells,
|
|
584
|
+
padCells,
|
|
585
|
+
rgb,
|
|
586
|
+
bg,
|
|
587
|
+
rgbTo256,
|
|
588
|
+
rgbTo16,
|
|
589
|
+
describe,
|
|
590
|
+
// internals the tests drive directly
|
|
591
|
+
resolveGlyphs,
|
|
592
|
+
resolveDepth,
|
|
593
|
+
resolveVt,
|
|
594
|
+
resolveSize,
|
|
595
|
+
MODERN_WINDOWS,
|
|
596
|
+
NATIVE_TERM_PROGRAMS,
|
|
597
|
+
};
|