@hmharness/cli 0.14.10 → 0.14.11

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.
Files changed (3) hide show
  1. package/dist/tui.d.ts +29 -0
  2. package/dist/tui.js +109 -12
  3. package/package.json +1 -1
package/dist/tui.d.ts CHANGED
@@ -64,6 +64,29 @@ export declare function splitMouseReport(data: string): {
64
64
  data: string;
65
65
  pending: string;
66
66
  };
67
+ /**
68
+ * Bracketed-paste body cleanup: strip the 200~/201~ markers and any escape
69
+ * sequences (pasted terminal controls must not leak into the input line),
70
+ * and collapse newlines to single spaces - a multi-line paste becomes ONE
71
+ * reviewable input instead of auto-submitting each line as its own command.
72
+ * Capped so a runaway paste cannot balloon the input field.
73
+ */
74
+ export declare function sanitizePaste(raw: string): string;
75
+ /**
76
+ * /copy clipboard candidates for the platform/session: Wayland first when
77
+ * WAYLAND_DISPLAY is set (xclip does not exist on a pure wl session), then
78
+ * X11's xclip with xsel as fallback. Empty when nothing plausible applies.
79
+ * The caller tries them in order until one spawns; the final "output
80
+ * follows" degrade stays for systems with no clipboard utility at all
81
+ * (e.g. the KaihongOS guest terminal).
82
+ */
83
+ export declare function clipboardCandidates(platform: string, env: {
84
+ WAYLAND_DISPLAY?: string;
85
+ DISPLAY?: string;
86
+ }): Array<{
87
+ bin: string;
88
+ args: string[];
89
+ }>;
67
90
  export declare class TuiRuntime {
68
91
  private entries;
69
92
  private dirty;
@@ -110,6 +133,9 @@ export declare class TuiRuntime {
110
133
  /** tail of an SGR mouse report split across stdin chunks (fast wheel
111
134
  * bursts); prepended to the next chunk so parseWheel sees the sequence */
112
135
  private mousePending;
136
+ /** bracketed-paste accumulator: non-null while the 201~ end marker has
137
+ * not arrived yet; everything buffered is literal insert-on-completion */
138
+ private pasteBuf;
113
139
  /** screen row of each visible palette item (SGR click hit-testing); the
114
140
  * render loop records row = frame.length (1-based) as it pushes rows */
115
141
  private paletteClickRows;
@@ -253,6 +279,9 @@ export declare class TuiRuntime {
253
279
  clearScreen(): void;
254
280
  private totalLines;
255
281
  private onKey;
282
+ /** insert literal (sanitized paste) text at the caret - the same path
283
+ * printable typing takes, so caret/history/palette side-effects match */
284
+ private insertText;
256
285
  render(): void;
257
286
  private flushFrame;
258
287
  }
package/dist/tui.js CHANGED
@@ -243,6 +243,43 @@ export function splitMouseReport(data) {
243
243
  ? { data, pending: '' }
244
244
  : { data: data.slice(0, cut), pending: tail };
245
245
  }
246
+ /**
247
+ * Bracketed-paste body cleanup: strip the 200~/201~ markers and any escape
248
+ * sequences (pasted terminal controls must not leak into the input line),
249
+ * and collapse newlines to single spaces - a multi-line paste becomes ONE
250
+ * reviewable input instead of auto-submitting each line as its own command.
251
+ * Capped so a runaway paste cannot balloon the input field.
252
+ */
253
+ export function sanitizePaste(raw) {
254
+ return raw
255
+ .replace(/\x1b\[[0-9;?]*[a-zA-Z~]/g, '')
256
+ .replace(/\x1b/g, '')
257
+ .replace(/\r\n|\r|\n/g, ' ')
258
+ .replace(/[ \t]+$/, '')
259
+ .slice(0, 262144);
260
+ }
261
+ /**
262
+ * /copy clipboard candidates for the platform/session: Wayland first when
263
+ * WAYLAND_DISPLAY is set (xclip does not exist on a pure wl session), then
264
+ * X11's xclip with xsel as fallback. Empty when nothing plausible applies.
265
+ * The caller tries them in order until one spawns; the final "output
266
+ * follows" degrade stays for systems with no clipboard utility at all
267
+ * (e.g. the KaihongOS guest terminal).
268
+ */
269
+ export function clipboardCandidates(platform, env) {
270
+ if (platform === 'win32')
271
+ return [{ bin: 'clip', args: [] }];
272
+ if (platform === 'darwin')
273
+ return [{ bin: 'pbcopy', args: [] }];
274
+ const out = [];
275
+ if (env.WAYLAND_DISPLAY)
276
+ out.push({ bin: 'wl-copy', args: [] });
277
+ if (env.DISPLAY) {
278
+ out.push({ bin: 'xclip', args: ['-selection', 'clipboard'] });
279
+ out.push({ bin: 'xsel', args: ['--clipboard', '--input'] });
280
+ }
281
+ return out;
282
+ }
246
283
  export class TuiRuntime {
247
284
  entries = [];
248
285
  dirty = true;
@@ -289,6 +326,9 @@ export class TuiRuntime {
289
326
  /** tail of an SGR mouse report split across stdin chunks (fast wheel
290
327
  * bursts); prepended to the next chunk so parseWheel sees the sequence */
291
328
  mousePending = '';
329
+ /** bracketed-paste accumulator: non-null while the 201~ end marker has
330
+ * not arrived yet; everything buffered is literal insert-on-completion */
331
+ pasteBuf = null;
292
332
  /** screen row of each visible palette item (SGR click hit-testing); the
293
333
  * render loop records row = frame.length (1-based) as it pushes rows */
294
334
  paletteClickRows = [];
@@ -318,7 +358,9 @@ export class TuiRuntime {
318
358
  // that mode arrows arrive as SS3 (\x1bOA) and would be silently dropped.
319
359
  // SGR mouse reporting on from the first frame: the wheel must scroll on
320
360
  // every terminal, not only the ones that map wheel->arrows themselves.
321
- stdout.write('\x1b[?1049h\x1b[?25l\x1b[2J\x1b[?1l\x1b[?1000h\x1b[?1006h');
361
+ // Bracketed paste (?2004h): multi-line pastes arrive delimited and
362
+ // insert as one reviewable input instead of auto-submitting per line.
363
+ stdout.write('\x1b[?1049h\x1b[?25l\x1b[2J\x1b[?1l\x1b[?1000h\x1b[?1006h\x1b[?2004h');
322
364
  stdin.setRawMode?.(true);
323
365
  stdin.resume();
324
366
  stdin.setEncoding('utf8');
@@ -597,7 +639,7 @@ export class TuiRuntime {
597
639
  clearInterval(this.spinnerTimer);
598
640
  // ?1l restores default CSI cursor keys; reporting off whatever the
599
641
  // modal state was
600
- stdout.write((this.mouseReported ? '\x1b[?1000l\x1b[?1006l' : '') + '\x1b[?1l\x1b[?25h\x1b[?1049l');
642
+ stdout.write((this.mouseReported ? '\x1b[?1000l\x1b[?1006l' : '') + '\x1b[?2004l' + '\x1b[?1l\x1b[?25h\x1b[?1049l');
601
643
  stdin.setRawMode?.(false);
602
644
  stdin.pause();
603
645
  }
@@ -1055,6 +1097,43 @@ export class TuiRuntime {
1055
1097
  if (!split.data)
1056
1098
  return;
1057
1099
  data = split.data;
1100
+ // bracketed paste (T22): buffer until the 201~ end marker, then insert
1101
+ // the sanitized body at the caret; keys/bytes outside the markers keep
1102
+ // flowing through the normal path (recursion depth stays 1: indexOf
1103
+ // anchors on the FIRST 200~ so the outer chunk cannot contain another)
1104
+ if (this.pasteBuf !== null) {
1105
+ this.pasteBuf += data;
1106
+ const end = this.pasteBuf.indexOf('\x1b[201~');
1107
+ if (end !== -1) {
1108
+ const done = this.pasteBuf;
1109
+ this.pasteBuf = null;
1110
+ this.insertText(sanitizePaste(done));
1111
+ const after = done.slice(end + 6);
1112
+ if (after)
1113
+ this.onKey(after);
1114
+ }
1115
+ else if (this.pasteBuf.length > 1048576) {
1116
+ this.pasteBuf = null; // runaway paste: drop, never balloon the input
1117
+ }
1118
+ return;
1119
+ }
1120
+ const ps = data.indexOf('\x1b[200~');
1121
+ if (ps !== -1) {
1122
+ const before = data.slice(0, ps);
1123
+ if (before)
1124
+ this.onKey(before);
1125
+ this.pasteBuf = data.slice(ps + 6);
1126
+ const end = this.pasteBuf.indexOf('\x1b[201~');
1127
+ if (end !== -1) {
1128
+ const done = this.pasteBuf;
1129
+ this.pasteBuf = null;
1130
+ this.insertText(sanitizePaste(done));
1131
+ const after = done.slice(end + 6);
1132
+ if (after)
1133
+ this.onKey(after);
1134
+ }
1135
+ return;
1136
+ }
1058
1137
  // SS3 application-mode arrows (\x1bOA…H): terminals left in DECCKM by a
1059
1138
  // previous program send these; normalize to the CSI forms this UI
1060
1139
  // matches so navigation never silently dies
@@ -1367,6 +1446,17 @@ export class TuiRuntime {
1367
1446
  // '@' opens the file palette (B2); refreshAtPal() is a no-op otherwise
1368
1447
  this.refreshAtPal();
1369
1448
  }
1449
+ /** insert literal (sanitized paste) text at the caret - the same path
1450
+ * printable typing takes, so caret/history/palette side-effects match */
1451
+ insertText(text) {
1452
+ if (!text)
1453
+ return;
1454
+ this.input = this.input.slice(0, this.caret) + text + this.input.slice(this.caret);
1455
+ this.caret += text.length;
1456
+ this.cmdIdx = 0;
1457
+ this.dirty = true;
1458
+ this.refreshAtPal();
1459
+ }
1370
1460
  /* ---------------- rendering ---------------- */
1371
1461
  render() {
1372
1462
  if (!this.running || !this.dirty)
@@ -2066,21 +2156,28 @@ export async function tui(yes, noWeb = false, opts = {}) {
2066
2156
  rt.addText('(no AI output yet to copy)', 'dim');
2067
2157
  return;
2068
2158
  }
2069
- try {
2070
- const { spawn } = await import('node:child_process');
2071
- const bin = process.platform === 'win32' ? 'clip'
2072
- : process.platform === 'darwin' ? 'pbcopy'
2073
- : 'xclip';
2074
- const args = bin === 'xclip' ? ['-selection', 'clipboard'] : [];
2159
+ const { spawn } = await import('node:child_process');
2160
+ const tryBin = (bin, args) => new Promise((res) => {
2075
2161
  const child = spawn(bin, args, { windowsHide: true, stdio: ['pipe', 'ignore', 'ignore'] });
2162
+ child.on('error', () => res(false));
2163
+ child.stdin.on('error', () => { });
2076
2164
  child.stdin.write(lastAiText);
2077
2165
  child.stdin.end();
2078
- await new Promise((r) => child.on('close', r));
2079
- rt.addText(GREEN('✓') + ' ' + t.cmdCopied, 'plain');
2166
+ child.on('close', (code) => res(code === 0));
2167
+ });
2168
+ // candidate chain (T16 复案): Wayland wl-copy before X11 xclip/xsel -
2169
+ // a single hard-coded xclip is dead on a pure wl session (Omarchy)
2170
+ let copied = false;
2171
+ for (const c of clipboardCandidates(process.platform, process.env)) {
2172
+ if (await tryBin(c.bin, c.args)) {
2173
+ copied = true;
2174
+ break;
2175
+ }
2080
2176
  }
2081
- catch {
2177
+ if (copied)
2178
+ rt.addText(GREEN('✓') + ' ' + t.cmdCopied, 'plain');
2179
+ else
2082
2180
  rt.addText('(clipboard tool unavailable — output follows)\n' + lastAiText.slice(0, 2000), 'dim');
2083
- }
2084
2181
  return;
2085
2182
  }
2086
2183
  if (line === '/plan' || line.startsWith('/plan ')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/cli",
3
- "version": "0.14.10",
3
+ "version": "0.14.11",
4
4
  "description": "hmharness command line: one-shot tasks, an interactive REPL, a fullscreen TUI, the web frontend, and direct tool invocation.",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",