@hncode/hncode 0.8.5 → 0.8.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hncode/hncode",
3
- "version": "0.8.5",
3
+ "version": "0.8.6",
4
4
  "description": "hncode — A cyan-blue TUI AI coding agent with zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/term.js CHANGED
@@ -62,6 +62,15 @@ export function strWidth(s) {
62
62
  // * everything else is memoised (see WIDTH_CACHE_MAX). Rows are re-measured
63
63
  // every frame while a reply streams, and the same substrings come back
64
64
  // again and again.
65
+ /** Matches any escape sequence, for the width walk to skip. */
66
+ const ESC_RE = /\x1b/;
67
+ function stripEscapes(s) {
68
+ return String(s)
69
+ .replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '')
70
+ .replace(/\x1b\[[0-9;?]* [a-zA-Z]/g, '')
71
+ .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)/g, '');
72
+ }
73
+
65
74
  export function visualWidth(s) {
66
75
  if (!s) return 0;
67
76
  const str = typeof s === 'string' ? s : String(s);
@@ -69,7 +78,17 @@ export function visualWidth(s) {
69
78
  if (isPrintableAscii(str)) return str.length;
70
79
  const hit = widthCache.get(str);
71
80
  if (hit !== undefined) return hit;
72
- const w = clusterWidth(str);
81
+
82
+ // ANSI ESCAPES ARE NOT GLYPHS.
83
+ //
84
+ // This measured the raw string, so every colour sequence counted as its own characters:
85
+ // `col('● ', orange)` — `ESC[38;2;240;160;80m` + `● ` + `ESC[0m` — came out TWENTY-TWO
86
+ // columns wide instead of two. Every styled row in the frame was therefore measured far
87
+ // wider than it draws, which put `fitAnsi` short, pushed the scrollbar off the edge, and
88
+ // made the row/column arithmetic in the mouse mapping disagree with the screen by however
89
+ // much styling that row happened to carry — an offset that changes from place to place.
90
+ const plain = ESC_RE.test(str) ? stripEscapes(str) : str;
91
+ const w = clusterWidth(plain);
73
92
  if (widthCache.size >= WIDTH_CACHE_MAX) {
74
93
  // Cheap eviction: drop the oldest insertion (Map preserves order).
75
94
  const oldest = widthCache.keys().next().value;
package/src/tool-line.js CHANGED
@@ -55,7 +55,7 @@ const MAX_ARG = 60;
55
55
  * of characters, and printing it whole wrapped the row across the screen for every call. Ctrl+O is the escape hatch —
56
56
  * whoever expands tool output to read it wants to see the command they are reading the output OF.
57
57
  */
58
- export function keyArgument(name, args, workspace, expanded = false) {
58
+ export function keyArgument(name, args, workspace, expanded = false, budget = MAX_ARG) {
59
59
  if (!args || typeof args !== 'object') return '';
60
60
  const keys = KEY_ARG[name] || Object.keys(args);
61
61
  // Expanded: the whole value, with newlines kept, so a multi-line script shows as the lines it was written as rather
@@ -81,15 +81,15 @@ export function keyArgument(name, args, workspace, expanded = false) {
81
81
  while (valueLines.length > 1 && valueLines[valueLines.length - 1].trim() === '') valueLines.pop();
82
82
  let text = valueLines[0];
83
83
  const rest = valueLines.length > 1 ? valueLines.length - 1 : 0;
84
- if (text.length > MAX_ARG) {
84
+ if (text.length > budget) {
85
85
  text = (k === 'path' || k === 'file_path')
86
- ? '…' + text.slice(text.length - (MAX_ARG - 1))
87
- : text.slice(0, MAX_ARG - 1) + '…';
86
+ ? '…' + text.slice(text.length - (budget - 1))
87
+ : text.slice(0, budget - 1) + '…';
88
88
  } else if (rest) {
89
89
  // Make room for the marker before adding it, so the row never overflows: the count is dropped first because the
90
90
  // ellipsis already carries the important half of the message.
91
91
  const withCount = `… (+${rest} line${rest === 1 ? '' : 's'})`;
92
- text += text.length + withCount.length <= MAX_ARG
92
+ text += text.length + withCount.length <= budget
93
93
  ? withCount
94
94
  : '…';
95
95
  }
@@ -109,7 +109,14 @@ export function keyArgument(name, args, workspace, expanded = false) {
109
109
  */
110
110
  export function formatToolLine(msg, workspace, spin, pulseStart, shimmerEdge, expanded = false, reducedMotion = false, width = 0) {
111
111
  const name = msg.toolName || '';
112
- const arg = keyArgument(name, msg.toolArgs, workspace, expanded);
112
+ // THE ARGUMENT IS CUT TO WHAT IS LEFT OF THE ROW.
113
+ //
114
+ // It was cut to a hardcoded 60 characters, so `● Using Bash (…)` measured 13 + 60 = 73
115
+ // columns on every terminal. Anything narrower than that was wrapped BY THE TERMINAL,
116
+ // taking two display rows where the frame counted one, and every row below moved down.
117
+ // `width` is the row the caller has; the verb and the name come out of it first.
118
+ const argBudget = Math.max(8, (width || 0) - (msg.pending ? 'Using' : 'Used').length - 1 - name.length - 3);
119
+ const arg = keyArgument(name, msg.toolArgs, workspace, expanded, argBudget);
113
120
  const verb = msg.pending ? 'Using' : 'Used';
114
121
  let nameDisplay;
115
122
  if (msg.pending) {
package/src/tui.js CHANGED
@@ -1469,9 +1469,23 @@ function messageLines(msg, width, workspace, expanded, spin, pulseStart, rawMode
1469
1469
  // is not a continuation and gets no mark — the break is already unmistakable.
1470
1470
  const wrapped = [];
1471
1471
  for (const segment of segments) {
1472
- const parts = wrapAnsiWords(segment, bodyW);
1472
+ // THE CONTINUATION MARK NEEDS A COLUMN OF ITS OWN, AND ONLY ONCE.
1473
+ //
1474
+ // `parts[i]` fills the row exactly, so appending `…` made the row ONE COLUMN WIDER than
1475
+ // the terminal. The terminal then wraps it itself, taking two display rows where this
1476
+ // function counted one — and every row below shifts by one, which is why the offset
1477
+ // moved around instead of being a constant.
1478
+ //
1479
+ // And only when the segment does not already end in one: `keyArgument` truncates the
1480
+ // argument it was handed, so a wrapped command could come out `… …` — two marks and a
1481
+ // space between them, on a row that was already at its limit.
1482
+ const willBreak = visualCol(segment) > bodyW;
1483
+ const room = willBreak ? Math.max(1, bodyW - 1) : bodyW;
1484
+ const parts = wrapAnsiWords(segment, room);
1473
1485
  for (let i = 0; i < parts.length; i++) {
1474
- wrapped.push(i < parts.length - 1 ? parts[i] + col('…', C.gray) : parts[i]);
1486
+ const last = i === parts.length - 1;
1487
+ const alreadyMarked = /…\s*$/.test(stripAnsi(parts[i]));
1488
+ wrapped.push(last || alreadyMarked ? parts[i] : parts[i] + col('…', C.gray));
1475
1489
  }
1476
1490
  }
1477
1491
  const out = wrapped.map((t, i) => ({
@@ -17309,13 +17323,25 @@ export async function startTUI(opts) {
17309
17323
  // frame and only the body branch sets it).
17310
17324
  if (!allowHidden && state._bodyScreenTop == null) return '';
17311
17325
  const { cols } = dims();
17312
- // The FULL transcript, and that is correct here: `selection.row` is an ABSOLUTE
17313
- // transcript row, not a screen row. mouseToCell() already converted it
17314
- // (`_bodyTop + rowInBody`), and composeFrame's highlight indexes the windowed
17315
- // array with the same absolute number — the windowed array keeps the transcript's
17316
- // absolute length and fills off-screen slots with '', so index N is the same row
17317
- // in both. Verified by printing drawn-vs-full side by side at several scrolls.
17318
- const rawChat = renderChatLines(state, cols);
17326
+ // THE SAME RENDER THE SCREEN USED.
17327
+ //
17328
+ // `selection.row` is an index into the ROW ARRAY, and that array is built by
17329
+ // renderChatLines. Two calls with the same state can still disagree about how many rows
17330
+ // the transcript has: windowed, a message far from the viewport is counted from its cached
17331
+ // `_count`; rendered whole, every message is laid out for real, and a message whose rows
17332
+ // come out empty — a `tool_result` with no tool name, of which a long session has dozens —
17333
+ // counts differently.
17334
+ //
17335
+ // Measured on a real 2677-message session: windowed 10576 rows, whole 10604. The frame's
17336
+ // `_bodyTop` comes from the windowed count, so indexing the whole one put every copy 28
17337
+ // rows off — further down the transcript, further off, which is exactly "it depends where
17338
+ // I click".
17339
+ //
17340
+ // So the copy asks for the SAME array the frame asked for, with the same viewport.
17341
+ const bodyH = state._bodyH || 0;
17342
+ const rawChat = state._forceFullRender || !bodyH
17343
+ ? renderChatLines(state, cols)
17344
+ : renderChatLines(state, cols, { bodyH, scroll: state.scroll || 0 });
17319
17345
  const a = sel.anchor, h = sel.head;
17320
17346
  const start = (h.row < a.row || (h.row === a.row && h.col < a.col)) ? h : a;
17321
17347
  const end = start === a ? h : a;
@@ -17358,10 +17384,17 @@ export async function startTUI(opts) {
17358
17384
  return out.join('\n');
17359
17385
  }
17360
17386
  function slicePlainByCol(text, c0, c1) {
17361
- // A full-row slice keeps its content but loses the RIGHT PADDING. The row is padded to the terminal
17362
- // width, and `c1 === Infinity` means "to the end of the row" — copying that padding would put
17363
- // invisible spaces on the clipboard that were never selected.
17364
- if (c0 === 0 && c1 === Infinity) return text.replace(/\s+$/, '');
17387
+ // A slice that runs TO THE END OF THE ROW loses the right padding. The row is padded to
17388
+ // the terminal width, and that padding is not content — the highlight stops at the last
17389
+ // character, so the copy has to as well, or the clipboard comes back with a line of
17390
+ // invisible spaces that were never selected.
17391
+ //
17392
+ // `c0 === 0` USED TO BE PART OF THIS TEST, and that was the bug: a selection whose first
17393
+ // row starts at column 1 — which is every row, since column 0 is the margin the copy
17394
+ // skips — took the other path and kept its padding. Dragging across two or more rows and
17395
+ // pasting gave the first line followed by thirty-odd spaces.
17396
+ const toEnd = c1 === Infinity;
17397
+ if (c0 === 0 && toEnd) return text.replace(/\s+$/, '');
17365
17398
  let out = '';
17366
17399
  let col = 0;
17367
17400
  const s = String(text || '');
@@ -17369,12 +17402,12 @@ export async function startTUI(opts) {
17369
17402
  const cp = s.codePointAt(i);
17370
17403
  const ch = String.fromCodePoint(cp);
17371
17404
  const cw = visualWidth(ch);
17372
- if (col >= c0 && (c1 === Infinity || col < c1)) out += ch;
17405
+ if (col >= c0 && (toEnd || col < c1)) out += ch;
17373
17406
  col += cw;
17374
17407
  i += cp > 0xffff ? 2 : 1;
17375
- if (c1 !== Infinity && col >= c1) break;
17408
+ if (!toEnd && col >= c1) break;
17376
17409
  }
17377
- return out;
17410
+ return toEnd ? out.replace(/\s+$/, '') : out;
17378
17411
  }
17379
17412
  function copyToClipboard(text) {
17380
17413
  // Empty text is NOT a success. `copyText` returns false without touching the
@@ -18352,7 +18385,7 @@ export async function startTUI(opts) {
18352
18385
  return;
18353
18386
  }
18354
18387
 
18355
- if (t.key === 'c-s-m' && t.held) return; // the terminal's auto-repeat, not a new press
18388
+ if (t.key === 'c-s-m' && t.held) return; // the terminal's auto-repeat, not a new press
18356
18389
  if (t.key === 'c-s-m') {
18357
18390
  // Ctrl+Shift+M, HELD: the key going down opens the microphone and letting it up
18358
18391
  // closes it. A hold rather than a tap because the user knows when they have