@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.
@@ -5,6 +5,7 @@
5
5
 
6
6
  import { C } from './colors.js';
7
7
  import { col, visualCol, wrapWords } from './text.js';
8
+ import { uiText } from './i18n.js';
8
9
  import { fmtTokens } from './format.js';
9
10
  import { SPIN_PLACEHOLDER } from './animate.js';
10
11
 
@@ -32,15 +33,28 @@ export function compactionRows(msg, width, expanded, spin) {
32
33
  const detail = (msg.tokensBefore != null && msg.tokensAfter != null)
33
34
  ? col(` (${fmtTokens(msg.tokensBefore)} → ${fmtTokens(msg.tokensAfter)} tokens)`, C.gray) : '';
34
35
  const hint = (msg.text || '').trim()
35
- ? col(` (Ctrl-O to ${expanded ? 'hide' : 'show'} compaction summary)`, C.gray) : '';
36
- head = bullet + col('Compaction complete', C.green + C.bold) + detail + hint;
36
+ ? col(uiText(` (Ctrl-O to ${expanded ? 'hide' : 'show'} compaction summary)`), C.gray) : '';
37
+ head = bullet + col(uiText('Compaction complete'), C.green + C.bold) + detail + hint;
37
38
  } else if (canceled) {
38
- head = bullet + col('Compaction cancelled', C.yellow + C.bold)
39
+ head = bullet + col(uiText('Compaction cancelled'), C.yellow + C.bold)
39
40
  + (msg.reason ? col(' (' + msg.reason + ')', C.gray) : '');
40
41
  } else {
41
- head = bullet + col('Compacting context…', C.cyan + C.bold);
42
+ head = bullet + col(uiText('Compacting context…'), C.cyan + C.bold);
42
43
  }
43
44
  const out = [{ text: head, ind: '', raw: true }];
45
+ // WHERE THE EARLIER CONVERSATION WENT.
46
+ //
47
+ // Compaction drops the oldest messages from the model's history AND the oldest rows from the displayed transcript, so
48
+ // a long conversation visibly loses its top. The row above only reported token counts, which reads as "something was
49
+ // deleted". This says what actually happened: the history was summarised, it is still in the session file, and
50
+ // `/recap` reads it back.
51
+ if (done) {
52
+ const trimmed = Number(msg.trimmed) || 0;
53
+ const note = trimmed > 0
54
+ ? uiText(`${trimmed} earlier message${trimmed === 1 ? '' : 's'} left the screen — summarised above, still in the session file (/recap to read it back)`)
55
+ : uiText('the full conversation is still in the session file (/recap to read it back)');
56
+ out.push({ text: INDENT + col(note, C.gray), ind: INDENT, raw: true });
57
+ }
44
58
  if (msg.instruction) {
45
59
  out.push({ text: INDENT + col(String(msg.instruction), C.gray), ind: INDENT, raw: true });
46
60
  }
@@ -90,14 +104,14 @@ export function thinkingRows(msg, width, expanded) {
90
104
  // The spinner frame is NOT baked in here: a glyph that changes every tick would invalidate this row's cache and
91
105
  // re-wrap the whole reasoning block each time, which is what made long thinking look laggy. The caller substitutes
92
106
  // the live frame outside the cache instead.
93
- out.push({ text: SPIN_PLACEHOLDER + ' thinking…', ind: '', color: C.gray });
107
+ out.push({ text: SPIN_PLACEHOLDER + uiText(' thinking…'), ind: '', color: C.gray });
94
108
  const vis = contentLines.length > PREVIEW ? contentLines.slice(-PREVIEW) : contentLines;
95
109
  for (const l of vis) out.push({ text: it(l), ind: INDENT, color: C.gray });
96
110
  } else {
97
111
  const shown = contentLines.slice(0, PREVIEW);
98
112
  shown.forEach((l, i) => out.push({ text: it(l), ind: i === 0 ? '● ' : INDENT, color: C.gray }));
99
113
  if (contentLines.length > PREVIEW) {
100
- out.push({ text: `… (${contentLines.length - PREVIEW} more lines, ctrl+o to expand)`, ind: INDENT, color: C.gray });
114
+ out.push({ text: uiText(`… (${contentLines.length - PREVIEW} more lines, ctrl+o to expand)`), ind: INDENT, color: C.gray });
101
115
  }
102
116
  }
103
117
  return out;
@@ -12,6 +12,7 @@
12
12
 
13
13
  import { C } from './colors.js';
14
14
  import { col, expandTabs, fitAnsi, sanitizeText, visualCol, wrapWords } from './text.js';
15
+ import { uiText } from './i18n.js';
15
16
  import { langForPath } from './highlight.js';
16
17
  import { paintCode } from './diff.js';
17
18
 
@@ -33,7 +34,13 @@ export function toolResultRows(msg, width, expanded) {
33
34
  // stray blank line would render as a lone "↳ " with no content.
34
35
  while (rawLines.length && rawLines[0].trim() === '') rawLines.shift();
35
36
  while (rawLines.length && rawLines[rawLines.length - 1].trim() === '') rawLines.pop();
36
- const MAX_RESULT_LINES = expanded ? Infinity : 12;
37
+ // COLLAPSED IS A GLANCE; EXPANDED IS A READ, NOT A DUMP.
38
+ //
39
+ // Expanded used to be `Infinity`, so Ctrl+O on a command that printed 3000 lines put all 3000 into the transcript: the
40
+ // conversation above it scrolled away, the scrollbar became meaningless, and every frame afterwards paid to lay out
41
+ // rows nobody would read. The cap here is what a person can actually scan; past that the output belongs in a file,
42
+ // which is what the hint says.
43
+ const MAX_RESULT_LINES = expanded ? 400 : 12;
37
44
  // The exit-code line is bookkeeping for the AGENT and is NEVER shown to the user — success or failure. A failure is
38
45
  // already legible from the RED result text below. `[error: …]` is kept: it carries the message, not just a code.
39
46
  const visible = rawLines.filter((ln) => !/^\[exit code:/.test(ln.trim()));
@@ -49,7 +56,7 @@ export function toolResultRows(msg, width, expanded) {
49
56
  if (hasDiff) {
50
57
  // Same collapse rule as elsewhere: a short preview by default, everything once Ctrl+O expands. 16 rows (not 12)
51
58
  // because a change renders as a PAIR: `- old` then `+ new`.
52
- const MAX = expanded ? Infinity : 16;
59
+ const MAX = expanded ? 400 : 16;
53
60
  // Show the unified diff WITH context rows (ctx = dim) so a change is not two orphaned blocks; `gap` rows mark where
54
61
  // unchanged lines were elided.
55
62
  const total = msg.diff.length;
@@ -104,7 +111,11 @@ export function toolResultRows(msg, width, expanded) {
104
111
  }
105
112
  if (total > emitted) {
106
113
  diffRows.push({
107
- text: col(`… (${total - emitted} more lines, ctrl+o to expand)`, C.gray),
114
+ // A DEAD-END Ctrl+O IS WORSE THAN NO HINT. Collapsed it expands; already expanded there is nothing left to
115
+ // reveal, so the line says how much was withheld instead of inviting a key that would do nothing.
116
+ text: col(expanded
117
+ ? uiText(`… (${total - emitted} more lines not shown; press ctrl+e for the whole thing)`)
118
+ : uiText(`… (${total - emitted} more lines, ctrl+o to expand)`), C.gray),
108
119
  ind: cont, raw: true,
109
120
  });
110
121
  }
@@ -168,18 +179,34 @@ export function toolResultRows(msg, width, expanded) {
168
179
  const termSurface = msg.toolName === 'Bash' || msg.toolName === 'Shell';
169
180
  wrapped.forEach((t, i) => {
170
181
  if (termSurface) {
171
- const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}`);
172
- diffRows.push({ text: C.termBg + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
182
+ const surfaced = t.replace(/\x1b\[0m/g, `\x1b[0m${C.termBg}${color}`);
183
+ // A FOREGROUND IS NOT OPTIONAL HERE.
184
+ //
185
+ // This set only the background, so the text fell back to the terminal's own default colour. Against the very
186
+ // dark `termBg` that is readable on a light default and invisible on a dark one — which is exactly what a
187
+ // dark-background terminal showed: a solid dark block with no words in it.
188
+ //
189
+ // The `\x1b[0m` inside the row closes EVERYTHING, background included, so both have to be re-asserted after
190
+ // each one: the wash and the colour.
191
+ diffRows.push({ text: C.termBg + color + fitAnsi(surfaced, bodyW) + C.bgReset, ind: idx === 0 && i === 0 ? pre : cont, raw: true });
173
192
  } else {
174
193
  diffRows.push({ text: t, ind: idx === 0 && i === 0 ? pre : cont, color });
175
194
  }
176
195
  });
177
196
  });
178
197
  if (visible.length > MAX_RESULT_LINES) {
179
- // The hint says HOW to see the rest: "… N more lines" alone gave the user no way to know Ctrl+O would reveal it.
180
- // Only reachable while COLLAPSED: expanded, `MAX_RESULT_LINES` is Infinity.
198
+ // THE HINT SAYS HOW TO SEE THE REST, and what "the rest" is depends on where we are.
199
+ //
200
+ // Collapsed: Ctrl+O expands — that is the whole point of the line.
201
+ // Already expanded: there is no more to reveal here, so inviting another Ctrl+O would be a dead end. Say how much
202
+ // was withheld and where it does live, which is the tool result the model was given (and, when the result was
203
+ // spilled, the file it was written to).
204
+ const rest = visible.length - MAX_RESULT_LINES;
205
+ const text = expanded
206
+ ? uiText(`… ${rest} more line${rest === 1 ? '' : 's'} not shown; ctrl+o collapses, ctrl+e shows all of it`)
207
+ : uiText(`… ${rest} more lines, ctrl+o to expand`);
181
208
  diffRows.push({
182
- text: `… ${visible.length - MAX_RESULT_LINES} more lines, ctrl+o to expand`,
209
+ text,
183
210
  ind: cont,
184
211
  color: failed ? C.red : C.gray,
185
212
  });
@@ -1,128 +1,153 @@
1
- /**
2
- * Text and styled strings → a Screen.
3
- *
4
- * The frame composer produces rows of ANSI-decorated strings, because that is how every surface in this app was
5
- * written. Rather than rewrite all of them at once, this module paints those rows into a cell grid: it parses
6
- * the SGR runs, interns each style once, and writes the characters with their widths.
7
- *
8
- * The result is what the painter wants — a grid it can diff cell by cell, and in which a bitmap is a region
9
- * with no characters in it rather than a row with bytes in it.
10
- */
11
- import { charWidth, CELL_WIDTH } from './screen.js';
12
-
13
- /** Matches an SGR sequence: `ESC [ params m`. */
14
- const SGR = /\x1b\[([0-9;]*)m/g;
15
-
16
- /** Matches any other escape sequence, which the painter does not place as text. */
17
- const OTHER_ESCAPE = /\x1b(\[[0-9;?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[@-_])/g;
18
-
19
- /**
20
- * Split a line into runs of `{ text, styleId }`, interning each style.
21
- *
22
- * Styles are tracked as the CURRENT sgr string in effect, accumulated until a reset. Two runs that end up with
23
- * the same accumulated string share an id, so a row of plain text costs one lookup.
24
- *
25
- * @param {string} line
26
- * @param {import('./screen.js').StylePool} stylePool
27
- * @returns {Array<{text: string, styleId: number}>}
28
- */
29
- export function styledRuns(line, stylePool) {
30
- const runs = [];
31
- let current = '';
32
- let pos = 0;
33
- SGR.lastIndex = 0;
34
-
35
- const push = (text) => {
36
- if (!text) return;
37
- runs.push({ text, styleId: stylePool.intern(current) });
38
- };
39
-
40
- let m;
41
- while ((m = SGR.exec(line))) {
42
- push(line.slice(pos, m.index));
43
- const params = m[1];
44
- // `0` and an empty parameter both mean "reset everything".
45
- if (params === '' || params === '0') current = '';
46
- else current += `\x1b[${params}m`;
47
- pos = m.index + m[0].length;
48
- }
49
- push(line.slice(pos));
50
- return runs;
51
- }
52
-
53
- /** Strip every escape sequence from a string, leaving what the terminal would draw as text. */
54
- export function stripEscapes(line) {
55
- return String(line)
56
- .replace(SGR, '')
57
- .replace(OTHER_ESCAPE, '')
58
- .replace(/\x1b/g, '');
59
- }
60
-
61
- /** Terminal width of a string once its escapes are ignored. */
62
- export function displayWidth(line) {
63
- let w = 0;
64
- for (const ch of stripEscapes(line)) {
65
- const cw = charWidth(ch);
66
- if (cw > 0) w += cw;
67
- }
68
- return w;
69
- }
70
-
71
- /**
72
- * Paint rows of styled text into a screen.
73
- *
74
- * @param {import('./screen.js').Screen} screen Destination, already the right size.
75
- * @param {string[]} rows One string per row; extra rows are ignored, missing ones stay blank.
76
- * @param {import('./screen.js').StylePool} stylePool
77
- */
78
- export function paintRows(screen, rows, stylePool) {
79
- screen._rowCount = rows.length;
80
- const n = Math.min(rows.length, screen.height);
81
- for (let y = 0; y < n; y++) {
82
- const line = rows[y];
83
- if (!line) continue;
84
- let x = 0;
85
- for (const run of styledRuns(line, stylePool)) {
86
- for (const ch of run.text) {
87
- const w = charWidth(ch);
88
- if (w === 0) continue; // a combining mark has no column of its own
89
- if (x + w > screen.width) break;
90
- screen.set(x, y, ch, run.styleId, w);
91
- x += w;
92
- }
93
- if (x >= screen.width) break;
94
- }
95
- screen._mark(0, y, screen.width);
96
- }
97
- }
98
-
99
- /**
100
- * Turn a screen back into rows of ANSI strings, for the paths that still expect text.
101
- *
102
- * This is the inverse of `paintRows` and is meant for the web mirror and for plugins that read the frame's
103
- * lines; the terminal itself is written from the cell diff, not from these strings.
104
- *
105
- * @param {import('./screen.js').Screen} screen
106
- * @returns {string[]}
107
- */
108
- export function screenToRows(screen) {
109
- const rows = [];
110
- for (let y = 0; y < screen.height; y++) {
111
- let out = '';
112
- let styleId = -1;
113
- for (let x = 0; x < screen.width; x++) {
114
- if (screen.isSpacer(x, y)) continue;
115
- const cell = screen.at(x, y);
116
- if (cell.styleId !== styleId) {
117
- out += '\x1b[0m' + screen.stylePool.get(cell.styleId);
118
- styleId = cell.styleId;
119
- }
120
- out += cell.ch;
121
- }
122
- if (styleId !== -1) out += '\x1b[0m';
123
- rows.push(out);
124
- }
125
- return rows;
126
- }
127
-
128
- export { CELL_WIDTH };
1
+ /**
2
+ * Text and styled strings → a Screen.
3
+ *
4
+ * The frame composer produces rows of ANSI-decorated strings, because that is how every surface in this app was
5
+ * written. Rather than rewrite all of them at once, this module paints those rows into a cell grid: it parses
6
+ * the SGR runs, interns each style once, and writes the characters with their widths.
7
+ *
8
+ * The result is what the painter wants — a grid it can diff cell by cell, and in which a bitmap is a region
9
+ * with no characters in it rather than a row with bytes in it.
10
+ */
11
+ import { charWidth, CELL_WIDTH } from './screen.js';
12
+
13
+ /** Matches an SGR sequence: `ESC [ params m`. */
14
+ const SGR = /\x1b\[([0-9;]*)m/g;
15
+
16
+ /** Matches any other escape sequence, which the painter does not place as text. */
17
+ const OTHER_ESCAPE = /\x1b(\[[0-9;?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[@-_])/g;
18
+
19
+ /**
20
+ * Split a line into runs of `{ text, styleId }`, interning each style.
21
+ *
22
+ * Styles are tracked as the CURRENT sgr string in effect, accumulated until a reset. Two runs that end up with
23
+ * the same accumulated string share an id, so a row of plain text costs one lookup.
24
+ *
25
+ * @param {string} line
26
+ * @param {import('./screen.js').StylePool} stylePool
27
+ * @returns {Array<{text: string, styleId: number}>}
28
+ */
29
+ export function styledRuns(line, stylePool) {
30
+ const runs = [];
31
+ let current = '';
32
+ let pos = 0;
33
+ SGR.lastIndex = 0;
34
+
35
+ const push = (text) => {
36
+ if (!text) return;
37
+ runs.push({ text, styleId: stylePool.intern(current) });
38
+ };
39
+
40
+ let m;
41
+ while ((m = SGR.exec(line))) {
42
+ push(line.slice(pos, m.index));
43
+ const params = m[1];
44
+ // `0` and an empty parameter both mean "reset everything".
45
+ if (params === '' || params === '0') current = '';
46
+ else current += `\x1b[${params}m`;
47
+ pos = m.index + m[0].length;
48
+ }
49
+ push(line.slice(pos));
50
+ return runs;
51
+ }
52
+
53
+ /** Strip every escape sequence from a string, leaving what the terminal would draw as text. */
54
+ export function stripEscapes(line) {
55
+ return String(line)
56
+ .replace(SGR, '')
57
+ .replace(OTHER_ESCAPE, '')
58
+ .replace(/\x1b/g, '');
59
+ }
60
+
61
+ /** Terminal width of a string once its escapes are ignored. */
62
+ export function displayWidth(line) {
63
+ let w = 0;
64
+ for (const ch of stripEscapes(line)) {
65
+ const cw = charWidth(ch);
66
+ if (cw > 0) w += cw;
67
+ }
68
+ return w;
69
+ }
70
+
71
+ /**
72
+ * Paint rows of styled text into a screen.
73
+ *
74
+ * NO INTERMEDIATE RUNS. This used to call `styledRuns`, which built a `{ text, styleId }` object per SGR run and
75
+ * sliced a fresh string for each one — a hundred-odd throwaway objects per frame for a 40-row screen, on top of the
76
+ * per-character `for…of` that allocates a one-character string per glyph.
77
+ *
78
+ * The loop below walks the escapes in place and copies each run straight into the cell grid, so the only allocation
79
+ * left is whatever `screen.set` interns. The painted cells are identical.
80
+ *
81
+ * @param {import('./screen.js').Screen} screen Destination, already the right size.
82
+ * @param {string[]} rows One string per row; extra rows are ignored, missing ones stay blank.
83
+ * @param {import('./screen.js').StylePool} stylePool
84
+ */
85
+ export function paintRows(screen, rows, stylePool) {
86
+ screen._rowCount = rows.length;
87
+ const n = Math.min(rows.length, screen.height);
88
+ const width = screen.width;
89
+ // A style is interned only when it CHANGES, so a run of plain text costs one lookup for the whole row.
90
+ let curStyle = '';
91
+ let curStyleId = -1;
92
+ for (let y = 0; y < n; y++) {
93
+ const line = rows[y];
94
+ if (!line) continue;
95
+ let x = 0;
96
+ let pos = 0;
97
+ let internedForRun = false;
98
+ SGR.lastIndex = 0;
99
+ for (;;) {
100
+ const m = SGR.exec(line);
101
+ const end = m ? m.index : line.length;
102
+ if (end > pos && x < width) {
103
+ if (!internedForRun) { curStyleId = stylePool.intern(curStyle); internedForRun = true; }
104
+ for (let i = pos; i < end; i++) {
105
+ const ch = line[i];
106
+ const w = charWidth(ch);
107
+ if (w === 0) continue; // a combining mark has no column of its own
108
+ if (x + w > width) break;
109
+ screen.set(x, y, ch, curStyleId, w);
110
+ x += w;
111
+ }
112
+ }
113
+ if (!m) break;
114
+ const params = m[1];
115
+ if (params === '' || params === '0') curStyle = '';
116
+ else curStyle += `\x1b[${params}m`;
117
+ pos = m.index + m[0].length;
118
+ internedForRun = false;
119
+ }
120
+ screen._mark(0, y, width);
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Turn a screen back into rows of ANSI strings, for the paths that still expect text.
126
+ *
127
+ * This is the inverse of `paintRows` and is meant for the web mirror and for plugins that read the frame's
128
+ * lines; the terminal itself is written from the cell diff, not from these strings.
129
+ *
130
+ * @param {import('./screen.js').Screen} screen
131
+ * @returns {string[]}
132
+ */
133
+ export function screenToRows(screen) {
134
+ const rows = [];
135
+ for (let y = 0; y < screen.height; y++) {
136
+ let out = '';
137
+ let styleId = -1;
138
+ for (let x = 0; x < screen.width; x++) {
139
+ if (screen.isSpacer(x, y)) continue;
140
+ const cell = screen.at(x, y);
141
+ if (cell.styleId !== styleId) {
142
+ out += '\x1b[0m' + screen.stylePool.get(cell.styleId);
143
+ styleId = cell.styleId;
144
+ }
145
+ out += cell.ch;
146
+ }
147
+ if (styleId !== -1) out += '\x1b[0m';
148
+ rows.push(out);
149
+ }
150
+ return rows;
151
+ }
152
+
153
+ export { CELL_WIDTH };