@hncode/hncode 0.8.1 → 0.8.2

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/src/team-panel.js CHANGED
@@ -21,6 +21,7 @@ import { readTeamFile } from './team/store.js';
21
21
  import { readMailbox, writeToMailbox } from './team/mailbox.js';
22
22
  import { readTasks, formatTasks } from './team/tasks.js';
23
23
  import { TEAM_LEAD_NAME } from './team/constants.js';
24
+ import { uiText } from './i18n.js';
24
25
 
25
26
 
26
27
  /** The state a panel needs, created once when it opens. */
@@ -303,26 +304,26 @@ export function renderTeamPanel(panel, cols, rows) {
303
304
  const m = roster[Math.min(panel.selectedIndex, roster.length - 1)];
304
305
  push('');
305
306
  push(` ${C.cyan}${C.bold}@${m.name}${C.reset} ${C.gray}${m.state || ''}${C.reset}`);
306
- push(` ${C.gray}${m.model || '(no model set)'}${m.model ? '' : ''}${C.reset}`);
307
+ push(` ${C.gray}${m.model || uiText('(no model set)')}${m.model ? '' : ''}${C.reset}`);
307
308
  push(` ${C.gray}mode: ${m.permissionMode || 'ask'}${C.reset}`);
308
309
  if (m.taskId) push(` ${C.gray}task: ${m.taskId}${C.reset}`);
309
310
  if (m.cwd) push(` ${C.gray}cwd: ${m.cwd}${C.reset}`);
310
311
  push('');
311
- push(` ${C.gray}── what it is doing ──${C.reset}`);
312
+ push(` ${C.gray}── ${uiText('what it is doing')} ──${C.reset}`);
312
313
  const live = (panel.chat && panel.chat[m.name]) || [];
313
314
  const preview = previewLines(panel, m.name, Math.max(3, rows - 14), cols);
314
- if (!preview.length) push(` ${C.gray}(nothing yet)${C.reset}`);
315
+ if (!preview.length) push(` ${C.gray}${uiText('(nothing yet)')}${C.reset}`);
315
316
  for (const line of preview) push(` ${C.gray}${line}${C.reset}`);
316
317
  void live;
317
318
  while (out.length < rows - 3) push('');
318
319
  push(C.border + '─'.repeat(cols) + C.reset);
319
- push(` ${C.gray}← back · Enter message · k kill · s stop · m mode${C.reset}`);
320
+ push(` ${C.gray}${uiText('← back · Enter message · k kill · s stop · m mode')}${C.reset}`);
320
321
  push('');
321
322
  return out.slice(0, rows);
322
323
  }
323
324
 
324
325
  if (!roster.length) {
325
- push(' ' + C.gray + 'No members. Add one with /team add <name>.' + C.reset);
326
+ push(' ' + C.gray + uiText('No members. Add one with /team add <name>.') + C.reset);
326
327
  }
327
328
 
328
329
  // The roster, then the selected member's traffic, then the composer.
@@ -479,9 +480,9 @@ export function renderTeamPanel(panel, cols, rows) {
479
480
  const req = panel.pendingPermission;
480
481
  if (req && out.length < rows - composerRows - 3) {
481
482
  const cut2 = (s) => String(s || '').replace(/\s+/g, ' ').trim().slice(0, Math.max(8, cols - 4));
482
- const who = String(req.agentId || 'a member');
483
- const what = String(req.description || req.toolName || 'a tool call');
484
- push(` ${C.yellow}${cut2(`${who} wants ${req.toolName}: ${what}`)}${C.reset}`);
483
+ const who = uiText(String(req.agentId || 'a member'));
484
+ const what = uiText(String(req.description || req.toolName || 'a tool call'));
485
+ push(` ${C.yellow}${cut2(uiText(`${who} wants ${req.toolName}: ${what}`))}${C.reset}`);
485
486
  const inputLine = req.input && typeof req.input === 'object'
486
487
  ? Object.entries(req.input).slice(0, 2).map(([k, v]) => `${k}=${String(v).slice(0, 40)}`).join(' ')
487
488
  : '';
@@ -503,12 +504,12 @@ export function renderTeamPanel(panel, cols, rows) {
503
504
  push(C.border + '─'.repeat(cols) + C.reset);
504
505
  if (panel.mode === 'compose') {
505
506
  push(` ${C.cyan}→ ${sel ? sel.name : '?'}${C.reset} ${panel.composing}${C.cyan}█${C.reset}`);
506
- push(' ' + C.gray + 'Enter send · Esc cancel' + C.reset);
507
+ push(' ' + C.gray + uiText('Enter send · Esc cancel') + C.reset);
507
508
  } else {
508
- push(' ' + C.gray + 'Enter write · Tab next · ↑↓ select · Esc close' + C.reset);
509
+ push(' ' + C.gray + uiText('Enter write · Tab next · ↑↓ select · Esc close') + C.reset);
509
510
  // The roster actions, named so they are discoverable rather than folklore. A second line because
510
511
  // squeezing six shortcuts onto one makes all of them unreadable.
511
- push(' ' + C.gray + 'k kill · s stop · p prune idle · m mode · r read' + C.reset);
512
+ push(' ' + C.gray + uiText('k kill · s stop · p prune idle · m mode · r read') + C.reset);
512
513
  }
513
514
 
514
515
  return out.slice(0, rows);
package/src/text.js CHANGED
@@ -1,149 +1,192 @@
1
- // Text measurement, colouring and wrapping.
2
- //
3
- // These are the leaves of the renderer: they take a string and some numbers and return a string. Nothing here knows
4
- // what a transcript is, so the same helpers serve a chat row, a popup and the composer.
5
-
6
- import { C } from './colors.js';
7
- import { visualWidth, expandTabs } from './term.js';
8
-
9
- // The ESC byte, as its own string. Every escape scan starts by comparing against this rather than writing '\x1b',
10
- // which keeps the regexes and the string walks recognisably the same test.
11
- export const ESC = '\x1b';
12
-
13
- export { expandTabs };
14
-
15
- export function stripAnsi(s) {
16
- return String(s)
17
- .replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '')
18
- .replace(/\x1b\[[0-9;?]* [a-zA-Z]/g, '')
19
- .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, '');
20
- }
21
-
22
- // Make UNTRUSTED text safe to paint: file contents from Read, command output from Bash, diff bodies, tool arguments.
23
- //
24
- // The goal is to keep the content as ORIGINAL as possible while stopping the terminal from OBEYING it. Only the ESC
25
- // byte is rewritten — every following byte (the `[31m`, the `[10;20H` parameters) is shown verbatim, so nothing is
26
- // lost:
27
- //
28
- // file contains: ESC [ 3 1 m E R R O R ESC [ 0 m
29
- // screen shows : ^[ [ 3 1 m E R R O R ^[ [ 0 m
30
- //
31
- // A terminal cannot act on a sequence whose ESC has been split into `^[`, so `ESC[31m` no longer recolours half the
32
- // transcript, `ESC[0m` no longer resets the row mid-line, and `ESC[10;20H` no longer moves the cursor.
33
- //
34
- // `^[` is the convention `cat -v`, `less` and `bat` use for ESC, so it reads as "there is an escape here" rather
35
- // than as content.
36
- export function sanitizeText(s) {
37
- return String(s == null ? '' : s)
38
- .replace(/\x1b/g, '^[')
39
- .replace(/\x07/g, '^G')
40
- .replace(/\x08/g, '^H')
41
- .replace(/[\x0b\x0c]/g, ' ')
42
- .replace(/[\x00-\x06\x0e-\x1f\x7f]/g, '');
43
- }
44
-
45
- /**
46
- * How much of a running command's output to KEEP, not just display.
47
- *
48
- * This string is mirrored into the hub row (and so into the SSE patch the browser applies on every frame), which
49
- * means an unbounded buffer grows the terminal's row, the hub's row and the browser's copy at the same time. Only
50
- * the tail is ever drawn, so only the tail is worth holding.
51
- */
52
- export const LIVE_OUTPUT_MAX_CHARS = 64 * 1024;
53
-
54
- /** The last `max` CHARACTERS of `s`, cut on a line boundary so the tail never starts mid-line. */
55
- export function tailChars(s, max) {
56
- const str = String(s == null ? '' : s);
57
- if (str.length <= max) return str;
58
- const cut = str.slice(str.length - max);
59
- const nl = cut.indexOf('\n');
60
- return nl >= 0 ? cut.slice(nl + 1) : cut;
61
- }
62
-
63
- /** Visible width of a string that may carry ANSI escapes. */
64
- export function visualCol(s) { return visualWidth(stripAnsi(String(s))); }
65
-
66
- /** Wrap `text` in colour `c`, closing with a full reset. */
67
- export function col(text, c) { return c + String(text) + C.reset; }
68
-
69
- /**
70
- * Pad or truncate to exactly `w` VISIBLE columns, carrying escapes through untouched.
71
- *
72
- * Padding is what makes a row's background reach the edge; truncation keeps a long line from wrapping and pushing
73
- * the layout down a row.
74
- */
75
- export function fitAnsi(s, w) {
76
- const str = String(s);
77
- const v = visualCol(str);
78
- if (v < w) return str + ' '.repeat(w - v);
79
- if (v === w) return str;
80
- let out = '';
81
- let n = 0;
82
- let i = 0;
83
- while (i < str.length) {
84
- if (str[i] === ESC) {
85
- const m = /^\x1b\[[0-9;?]*[a-zA-Z]/.exec(str.slice(i))
86
- || /^\x1b\[[0-9;?]* [a-zA-Z]/.exec(str.slice(i))
87
- || /^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/.exec(str.slice(i));
88
- if (m) { out += m[0]; i += m[0].length; continue; }
89
- }
90
- const cp = str.codePointAt(i);
91
- const ch = String.fromCodePoint(cp);
92
- const cw = visualWidth(ch);
93
- if (n + cw > w) break;
94
- n += cw;
95
- out += ch;
96
- i += cp > 0xffff ? 2 : 1;
97
- }
98
- if (n < w) out += ' '.repeat(w - n);
99
- return out;
100
- }
101
-
102
- /**
103
- * Greedy word wrap to `width` VISIBLE columns.
104
- *
105
- * The running width is ACCUMULATED rather than re-measured. Recomputing `visualCol(line + ' ' + w)` per word is O(line)
106
- * per word and therefore O(n²) per paragraph — a single long streamed paragraph cost ~46ms at 20KB and froze the UI on
107
- * every markdown chunk. Widths are additive for the plain text this receives, because no ANSI is present until
108
- * `inlineMarkdown` runs later.
109
- *
110
- * A word longer than the line is hard-split BY VISUAL COLUMNS, not by `.length`: CJK characters occupy two cells, so
111
- * slicing by length emitted lines twice as wide as the box and let the terminal wrap them.
112
- */
113
- export function wrapWords(str, width) {
114
- width = Math.max(1, width | 0);
115
- const out = [];
116
- let line = '';
117
- let lineW = 0;
118
- // Tabs are expanded first: `visualCol` counts '\t' as 0 columns while the terminal advances to the next stop, which
119
- // made the row wider than computed and wrapped it onto the following line.
120
- const words = expandTabs(str).split(' ');
121
- for (const w of words) {
122
- const wW = visualCol(w);
123
- if (wW > width) {
124
- if (line) { out.push(line); line = ''; lineW = 0; }
125
- let chunk = '';
126
- let cw = 0;
127
- for (const ch of w) {
128
- const chw = visualCol(ch);
129
- if (cw + chw > width) { out.push(chunk); chunk = ''; cw = 0; }
130
- chunk += ch; cw += chw;
131
- }
132
- if (chunk) { line = chunk; lineW = cw; }
133
- continue;
134
- }
135
- const sep = line ? 1 : 0;
136
- if (lineW + sep + wW <= width) {
137
- line = line ? line + ' ' + w : w;
138
- lineW += sep + wW;
139
- } else {
140
- if (line) out.push(line);
141
- line = w;
142
- lineW = wW;
143
- }
144
- }
145
- if (line) out.push(line);
146
- else if (out.length === 0) out.push('');
147
- return out;
148
- }
149
-
1
+ // Text measurement, colouring and wrapping.
2
+ //
3
+ // These are the leaves of the renderer: they take a string and some numbers and return a string. Nothing here knows
4
+ // what a transcript is, so the same helpers serve a chat row, a popup and the composer.
5
+
6
+ import { C } from './colors.js';
7
+ import { visualWidth, expandTabs } from './term.js';
8
+
9
+ // The ESC byte, as its own string. Every escape scan starts by comparing against this rather than writing '\x1b',
10
+ // which keeps the regexes and the string walks recognisably the same test.
11
+ export const ESC = '\x1b';
12
+
13
+ export { expandTabs };
14
+
15
+ /**
16
+ * Remove every escape sequence, leaving what the terminal would actually draw.
17
+ *
18
+ * THE NO-ESCAPE CASE IS FREE, AND IT IS MOST ROWS.
19
+ *
20
+ * Three `.replace` calls each walk the whole string and allocate a copy even when they match nothing — which is what
21
+ * happens for the composer, the status line, and every row of plain text. Checking for the ESC byte first and returning
22
+ * the input unchanged costs one `indexOf` and saves all three regex scans: measured 6.5x faster on a plain row.
23
+ *
24
+ * A HAND-WRITTEN SCANNER FOR THE STYLED CASE WAS TRIED AND REJECTED. It was twice as SLOW as the regexes on a row that
25
+ * does carry escapes — V8's regex engine beats a character loop for this — and since styled rows are the common case in
26
+ * the transcript, keeping it would have been a net loss.
27
+ */
28
+ export function stripAnsi(s) {
29
+ const str = String(s);
30
+ // One pass to find the ESC; almost every row has none, and those rows are then copied zero times.
31
+ if (str.indexOf('\x1b') < 0) return str;
32
+ return str
33
+ .replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '')
34
+ .replace(/\x1b\[[0-9;?]* [a-zA-Z]/g, '')
35
+ .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, '');
36
+ }
37
+
38
+ // Make UNTRUSTED text safe to paint: file contents from Read, command output from Bash, diff bodies, tool arguments.
39
+ //
40
+ // The goal is to keep the content as ORIGINAL as possible while stopping the terminal from OBEYING it. Only the ESC
41
+ // byte is rewritten — every following byte (the `[31m`, the `[10;20H` parameters) is shown verbatim, so nothing is
42
+ // lost:
43
+ //
44
+ // file contains: ESC [ 3 1 m E R R O R ESC [ 0 m
45
+ // screen shows : ^[ [ 3 1 m E R R O R ^[ [ 0 m
46
+ //
47
+ // A terminal cannot act on a sequence whose ESC has been split into `^[`, so `ESC[31m` no longer recolours half the
48
+ // transcript, `ESC[0m` no longer resets the row mid-line, and `ESC[10;20H` no longer moves the cursor.
49
+ //
50
+ // `^[` is the convention `cat -v`, `less` and `bat` use for ESC, so it reads as "there is an escape here" rather
51
+ // than as content.
52
+ export function sanitizeText(s) {
53
+ return String(s == null ? '' : s)
54
+ .replace(/\x1b/g, '^[')
55
+ .replace(/\x07/g, '^G')
56
+ .replace(/\x08/g, '^H')
57
+ .replace(/[\x0b\x0c]/g, ' ')
58
+ .replace(/[\x00-\x06\x0e-\x1f\x7f]/g, '');
59
+ }
60
+
61
+ /**
62
+ * How much of a running command's output to KEEP, not just display.
63
+ *
64
+ * This string is mirrored into the hub row (and so into the SSE patch the browser applies on every frame), which
65
+ * means an unbounded buffer grows the terminal's row, the hub's row and the browser's copy at the same time. Only
66
+ * the tail is ever drawn, so only the tail is worth holding.
67
+ */
68
+ export const LIVE_OUTPUT_MAX_CHARS = 64 * 1024;
69
+
70
+ /** The last `max` CHARACTERS of `s`, cut on a line boundary so the tail never starts mid-line. */
71
+ export function tailChars(s, max) {
72
+ const str = String(s == null ? '' : s);
73
+ if (str.length <= max) return str;
74
+ const cut = str.slice(str.length - max);
75
+ const nl = cut.indexOf('\n');
76
+ return nl >= 0 ? cut.slice(nl + 1) : cut;
77
+ }
78
+
79
+ /**
80
+ * Visible width of a string that may carry ANSI escapes.
81
+ *
82
+ * THE PRINTABLE-ASCII FAST PATH IS THE COMMON CASE BY A WIDE MARGIN. Most rows in a frame are plain ASCII with no
83
+ * escapes — the filler, the padding, a notice, the composer's own text — and for those the width IS the length.
84
+ *
85
+ * The range is 0x20..0x7e, NOT everything below 0x7f. A first attempt accepted every code point under 0x7f and broke
86
+ * the frame: TAB (0x09) is below the bound but `visualWidth` counts it as zero columns while `length` counts one, so a
87
+ * row containing a tab was measured one column too wide and `fitAnsi` padded it short — the painted screen then
88
+ * diverged from the frame by exactly the tab's width. Control characters in general are zero-width, so they have to
89
+ * fall through.
90
+ */
91
+ export function visualCol(s) {
92
+ const str = String(s);
93
+ const len = str.length;
94
+ let i = 0;
95
+ while (i < len) {
96
+ const c = str.charCodeAt(i);
97
+ // Printable ASCII only: below 0x20 is a control, 0x7f is DEL, above is non-ASCII.
98
+ if (c < 0x20 || c > 0x7e) break;
99
+ i++;
100
+ }
101
+ if (i === len) return len;
102
+ // NO ESCAPE, NO STRIPPING. The scan above stopped on a non-ASCII or control character, which is NOT the same thing as
103
+ // a styled row: a row with one CJK character or one TAB fell through to `stripAnsi`, which allocated a fresh string
104
+ // through three regex passes just to hand it to a width walk. Only a row that actually contains ESC needs that.
105
+ if (str.indexOf('\x1b') < 0) return visualWidth(str);
106
+ return visualWidth(stripAnsi(str));
107
+ }
108
+
109
+ /** Wrap `text` in colour `c`, closing with a full reset. */
110
+ export function col(text, c) { return c + String(text) + C.reset; }
111
+
112
+ /**
113
+ * Pad or truncate to exactly `w` VISIBLE columns, carrying escapes through untouched.
114
+ *
115
+ * Padding is what makes a row's background reach the edge; truncation keeps a long line from wrapping and pushing
116
+ * the layout down a row.
117
+ */
118
+ export function fitAnsi(s, w) {
119
+ const str = String(s);
120
+ const v = visualCol(str);
121
+ if (v < w) return str + ' '.repeat(w - v);
122
+ if (v === w) return str;
123
+ let out = '';
124
+ let n = 0;
125
+ let i = 0;
126
+ while (i < str.length) {
127
+ if (str[i] === ESC) {
128
+ const m = /^\x1b\[[0-9;?]*[a-zA-Z]/.exec(str.slice(i))
129
+ || /^\x1b\[[0-9;?]* [a-zA-Z]/.exec(str.slice(i))
130
+ || /^\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/.exec(str.slice(i));
131
+ if (m) { out += m[0]; i += m[0].length; continue; }
132
+ }
133
+ const cp = str.codePointAt(i);
134
+ const ch = String.fromCodePoint(cp);
135
+ const cw = visualWidth(ch);
136
+ if (n + cw > w) break;
137
+ n += cw;
138
+ out += ch;
139
+ i += cp > 0xffff ? 2 : 1;
140
+ }
141
+ if (n < w) out += ' '.repeat(w - n);
142
+ return out;
143
+ }
144
+
145
+ /**
146
+ * Greedy word wrap to `width` VISIBLE columns.
147
+ *
148
+ * The running width is ACCUMULATED rather than re-measured. Recomputing `visualCol(line + ' ' + w)` per word is O(line)
149
+ * per word and therefore O(n²) per paragraph — a single long streamed paragraph cost ~46ms at 20KB and froze the UI on
150
+ * every markdown chunk. Widths are additive for the plain text this receives, because no ANSI is present until
151
+ * `inlineMarkdown` runs later.
152
+ *
153
+ * A word longer than the line is hard-split BY VISUAL COLUMNS, not by `.length`: CJK characters occupy two cells, so
154
+ * slicing by length emitted lines twice as wide as the box and let the terminal wrap them.
155
+ */
156
+ export function wrapWords(str, width) {
157
+ width = Math.max(1, width | 0);
158
+ const out = [];
159
+ let line = '';
160
+ let lineW = 0;
161
+ // Tabs are expanded first: `visualCol` counts '\t' as 0 columns while the terminal advances to the next stop, which
162
+ // made the row wider than computed and wrapped it onto the following line.
163
+ const words = expandTabs(str).split(' ');
164
+ for (const w of words) {
165
+ const wW = visualCol(w);
166
+ if (wW > width) {
167
+ if (line) { out.push(line); line = ''; lineW = 0; }
168
+ let chunk = '';
169
+ let cw = 0;
170
+ for (const ch of w) {
171
+ const chw = visualCol(ch);
172
+ if (cw + chw > width) { out.push(chunk); chunk = ''; cw = 0; }
173
+ chunk += ch; cw += chw;
174
+ }
175
+ if (chunk) { line = chunk; lineW = cw; }
176
+ continue;
177
+ }
178
+ const sep = line ? 1 : 0;
179
+ if (lineW + sep + wW <= width) {
180
+ line = line ? line + ' ' + w : w;
181
+ lineW += sep + wW;
182
+ } else {
183
+ if (line) out.push(line);
184
+ line = w;
185
+ lineW = wW;
186
+ }
187
+ }
188
+ if (line) out.push(line);
189
+ else if (out.length === 0) out.push('');
190
+ return out;
191
+ }
192
+
package/src/tool-line.js CHANGED
@@ -180,14 +180,19 @@ export function formatToolLine(msg, workspace, spin, pulseStart, shimmerEdge, ex
180
180
  // A running call counts up from its start, so a slow tool shows progress rather than looking frozen; a finished one
181
181
  // shows the total. Same slot either way, so the line does not reflow when the call lands.
182
182
  //
183
- // Nothing is shown when the start is unknown — an older session, or a restored transcript — because a duration
183
+ // NOTHING UNDER A SECOND. `Read` and `Glob` land well inside one, and `(0s)` beside every one of them was pure noise —
184
+ // the figure only says something once it is long enough to be worth reading. The slot then simply stays empty, so the
185
+ // line does not reflow when a call does cross the second.
186
+ //
187
+ // Nothing is shown when the start is unknown either — an older session, or a restored transcript — because a duration
184
188
  // invented from "now" would be a lie that grows every frame.
185
189
  let timeStr = '';
186
190
  if (typeof msg.startAt === 'number' && msg.startAt > 0) {
187
191
  const ended = typeof msg.endedAt === 'number' && msg.endedAt > 0
188
192
  ? msg.endedAt
189
193
  : (msg.pending ? Date.now() : 0);
190
- if (ended >= msg.startAt) timeStr = ' ' + col(`(${fmtDuration(ended - msg.startAt)})`, C.gray);
194
+ const tookMs = ended >= msg.startAt ? ended - msg.startAt : 0;
195
+ if (tookMs >= 1000) timeStr = ' ' + col(`(${fmtDuration(tookMs)})`, C.gray);
191
196
  }
192
197
  return col(verb, C.white) + ' ' + nameDisplay + argText + col(sizeStr, C.gray) + diffStr + timeStr;
193
198
  }