@yanzc/claude-pro 0.11.0 → 0.13.0

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/ui/editor.js CHANGED
@@ -1,6 +1,6 @@
1
- // The prompt input box: multi-line editing, history, paste handling and completions.
1
+ // The prompt input: multi-line editing, history, paste handling and Tab completion.
2
2
 
3
- import { c, glyphs, graphemes, graphemeWidth, stringWidth, truncate, padEnd } from './ansi.js';
3
+ import { c, graphemes, graphemeWidth, stringWidth, truncate, padEnd } from './ansi.js';
4
4
 
5
5
  const PASTE_THRESHOLD_CHARS = 800;
6
6
  const PASTE_THRESHOLD_LINES = 8;
@@ -25,29 +25,6 @@ export function shownQuotes(text) {
25
25
  return String(text).replace(/<quote(?: source="([^"]*)")?>\n([\s\S]*?)\n<\/quote>/g, (_, source = '', body) => quotePreview(body, source.replace(/&quot;/g, '"')));
26
26
  }
27
27
 
28
- // "╭─ left ───── right ─╮", the parts cut short to fit (the start of the directory goes first).
29
- export function topBorder(boxW, color, title) {
30
- const plain = () => color(glyphs.topLeft + '─'.repeat(boxW - 2) + glyphs.topRight);
31
- if (!title || (!title.left && !title.right)) return plain();
32
- const room = boxW - 6; // the corners, a dash at each end and a space each side of a part
33
- const right = title.right ? truncate(title.right, Math.max(0, Math.floor(room / 3))).replace(/\x1b\[0m$/, '') : '';
34
- const rightPart = right ? `${glyphs.lineGap}${right} ` : '';
35
- const rightW = stringWidth(rightPart);
36
- let left = title.left || '';
37
- const leftRoom = room - rightW - (right && left ? 1 : 0) - 2;
38
- if (leftRoom < 4) left = '';
39
- else if (stringWidth(left) > leftRoom) {
40
- const chars = [...left];
41
- while (chars.length && stringWidth(chars.join('')) > leftRoom - 1) chars.shift();
42
- left = '…' + chars.join('');
43
- }
44
- if (!left && !right) return plain();
45
- const leftPart = left ? ` ${left} ` : '';
46
- const dashes = boxW - 4 - stringWidth(leftPart) - stringWidth(rightPart);
47
- if (dashes < 1) return plain();
48
- return color(glyphs.topLeft + '─') + c.dim(leftPart) + color('─'.repeat(dashes)) + (right ? c.blue(rightPart) : '') + color('─' + glyphs.topRight);
49
- }
50
-
51
28
  // Start of the line the cursor is on. (lastIndexOf with a negative start still looks at index 0, so a
52
29
  // text that begins with a newline made the cursor at 0 appear to be on its second line.)
53
30
  function lineStartOf(text, cursor) {
@@ -134,12 +111,10 @@ export class Editor {
134
111
  this.imageFromPath = imageFromPath; // (path) => image | null, for pasted/dropped image files
135
112
  this.pendingSubmit = null;
136
113
  this.lastEscape = 0;
137
- this.suggestionsDismissedFor = null;
138
114
  this.onDoubleEscape = onDoubleEscape;
139
115
  this.textW = 76; // the width of the text in the box, as last drawn: up and down move by the rows shown
140
116
  this.goalCol = null; // the column up and down aim for, kept while they are pressed in a row
141
- this.selection = null; // {from, to}: text selected with the mouse (see mouse), indexes into `text`
142
- this.press = null; // the mouse button held down on the text: {at, moved}
117
+ this.completing = false; // Tab opened the completion list: it follows what is typed until it is closed
143
118
  }
144
119
 
145
120
  get mode() {
@@ -151,110 +126,12 @@ export class Editor {
151
126
  set(text, cursor = text.length) {
152
127
  this.text = text;
153
128
  this.cursor = Math.max(0, Math.min(cursor, text.length));
154
- this.selection = null;
155
- this.refreshSuggestions();
129
+ if (this.completing) this.refreshSuggestions();
156
130
  this.onChange?.();
157
131
  }
158
132
 
159
- // ------------------------------------------------------------ the mouse (fullscreen)
160
- //
161
- // As in Claude Code: a click on the text puts the cursor there. A drag selects from the cell it started in to the
162
- // one it is over, both included (as in the conversation, see Screen.mouse); a double click selects a word, a
163
- // triple click the line. A selection is there to be copied (ctrl+c, see App.onKey): any other key clears it and
164
- // then does what it does, so typing does not replace it (there is no undo to bring the text back).
165
-
166
- // The place in the text under a cell of the rows shown (see place): `row` among them, `col` the column in the
167
- // text. {i, end}: the index of the character drawn in that cell, or, for a cell past the text of the row, of the
168
- // row's end (end: true), where a click there puts the cursor.
169
- at(row, col) {
170
- const { rows, offset } = this.place();
171
- const r = rows[Math.max(0, Math.min(rows.length - 1, (this.viewTop || 0) + row))]; // the rows shown start at viewTop
172
- const x = Math.max(0, col);
173
- for (const st of r.stops) {
174
- if (st.g !== undefined && x >= st.col && x < st.col + graphemeWidth(st.g)) return { i: offset + st.i, end: false };
175
- }
176
- const tail = r.stops[r.stops.length - 1]; // the newline, the end, or (a row that wraps) its last character
177
- return { i: offset + tail.i + (tail.g?.length ?? 0), end: true };
178
- }
179
-
180
- // A mouse event on the text (see Screen.inputAt): {kind: 'press' | 'drag' | 'release', row, col, count}.
181
- mouse({ kind, row, col, count = 1 }) {
182
- const at = this.at(row, col);
183
- if (kind === 'press') {
184
- this.press = null;
185
- this.selection = null;
186
- if (count >= 3) this.selection = this.lineRange(at);
187
- else if (count === 2) this.selection = this.wordRange(at);
188
- else this.press = { at, moved: false };
189
- } else if (!this.press) {
190
- return;
191
- } else if (kind === 'drag') {
192
- const a = this.press.at;
193
- this.press.moved = true;
194
- // the cells in order; of two at the same index, the one past the end of a row comes first (it holds nothing)
195
- const [lo, hi] = at.i < a.i || (at.i === a.i && at.end) ? [at, a] : [a, at];
196
- const to = hi.end ? hi.i : nextBoundary(this.text, hi.i);
197
- this.selection = to > lo.i ? { from: lo.i, to } : null;
198
- } else { // released: without a drag, a click
199
- const click = !this.press.moved;
200
- this.press = null;
201
- if (click) { this.goalCol = null; this.set(this.text, at.i); return; }
202
- }
203
- this.onChange?.();
204
- }
205
-
206
- // The word (what is between spaces, as in the conversation) under `at`, or null.
207
- wordRange(at) {
208
- const t = this.text;
209
- const offset = this.mode === 'prompt' ? 0 : 1;
210
- const word = (ch) => ch !== undefined && !/\s/.test(ch);
211
- if (at.end || !word(t[at.i])) return null;
212
- let from = at.i;
213
- let to = at.i;
214
- while (from > offset && word(t[from - 1])) from--;
215
- while (to < t.length && word(t[to])) to++;
216
- return { from, to };
217
- }
218
-
219
- // The line `at` is on, without its newline, or null when it is empty.
220
- lineRange(at) {
221
- const from = Math.max(this.mode === 'prompt' ? 0 : 1, lineStartOf(this.text, at.i));
222
- const nl = this.text.indexOf('\n', at.i);
223
- const to = nl < 0 ? this.text.length : nl;
224
- return to > from ? { from, to } : null;
225
- }
226
-
227
- // The text selected, pasted text in it as what its placeholder stands for (see expanded).
228
- selectedText() {
229
- if (!this.selection) return '';
230
- let t = this.text.slice(this.selection.from, this.selection.to);
231
- for (const [label, content] of this.pastes) t = t.split(label).join(content);
232
- return t;
233
- }
234
-
235
- clearSelection() {
236
- if (!this.selection && !this.press) return false;
237
- this.selection = null;
238
- this.press = null;
239
- this.onChange?.();
240
- return true;
241
- }
242
-
243
- // A row of the rows shown (see layoutRows) with what is selected in reverse video, its newline as a space.
244
- paintRow(row, offset) {
245
- const { from, to } = this.selection;
246
- let out = '';
247
- let on = false;
248
- for (const st of row.stops) {
249
- const sel = offset + st.i >= from && offset + st.i < to;
250
- if (st.g === undefined && !sel) continue; // a newline, drawn as a space when selected (the end of the text never is)
251
- if (sel !== on) { out += sel ? '\x1b[7m' : '\x1b[27m'; on = sel; }
252
- out += st.g === undefined ? (row.width < this.textW ? ' ' : '') : st.g === '\t' ? ' ' : st.g;
253
- }
254
- return on ? out + '\x1b[27m' : out;
255
- }
256
-
257
133
  clear() {
134
+ this.closeSuggestions();
258
135
  this.set('');
259
136
  this.pastes = new Map();
260
137
  this.images = new Map();
@@ -336,23 +213,6 @@ export class Editor {
336
213
  return true;
337
214
  }
338
215
 
339
- // A part of the conversation quoted in the message (see App.quoteSelection), as ChatGPT's "Ask ChatGPT" does:
340
- // "[Quote #1 +12 lines]" on a line of its own, the cursor on the line after it, where the question goes. What is
341
- // sent has the text itself in a <quote>, with the line of the conversation it is under as its source (see
342
- // quoteBlock), so that the model knows which part is meant; the transcript shows its first lines (see displayed).
343
- // Deleted like a paste: one backspace takes the whole placeholder.
344
- insertQuote(text, source = '') {
345
- const lines = text.split('\n').length;
346
- const label = `[Quote #${++this.quoteCounter} +${lines} line${lines === 1 ? '' : 's'}]`;
347
- this.pastes.set(label, quoteBlock(text, source));
348
- this.quotes.set(label, quotePreview(text, source));
349
- const before = this.text.slice(0, this.cursor);
350
- const newline = this.text.slice(this.cursor).startsWith('\n');
351
- this.insert((before && !before.endsWith('\n') ? '\n' : '') + label + (newline ? '' : '\n'));
352
- if (newline) this.set(this.text, this.cursor + 1);
353
- return label;
354
- }
355
-
356
216
  insertImage(image) {
357
217
  const label = `[Image #${++this.imageCounter}]`;
358
218
  this.images.set(label, image);
@@ -398,13 +258,47 @@ export class Editor {
398
258
  return t;
399
259
  }
400
260
 
261
+ // The completion list again for the text as it is now (while it is open: see complete). It closes when nothing
262
+ // matches any more.
401
263
  refreshSuggestions() {
402
- if (!this.getSuggestions || this.suggestionsDismissedFor === this.text) { this.suggestions = null; return; }
403
- const s = this.getSuggestions(this.text, this.cursor);
264
+ if (!this.completing || !this.getSuggestions) return;
265
+ const s = this.getSuggestions(this.text, this.cursor, { quiet: true });
404
266
  this.suggestions = s && s.items.length ? s : null;
267
+ if (!this.suggestions) this.completing = false;
405
268
  if (!this.suggestions || this.selected >= this.suggestions.items.length) this.selected = 0;
406
269
  }
407
270
 
271
+ closeSuggestions() {
272
+ this.completing = false;
273
+ this.suggestions = null;
274
+ this.selected = 0;
275
+ }
276
+
277
+ // Tab, as in a shell: one match is put in at once; several put in what they all start with and open the list,
278
+ // which Tab and down then go through (Enter takes the one selected, Esc closes it).
279
+ complete() {
280
+ if (this.suggestions) { this.selected = (this.selected + 1) % this.suggestions.items.length; this.onChange?.(); return; }
281
+ const s = this.getSuggestions?.(this.text, this.cursor);
282
+ // nothing (yet: the file list may still be loading, see App's FileIndex, which then completes again)
283
+ this.lastMiss = s?.items.length ? null : { text: this.text, cursor: this.cursor, at: Date.now() };
284
+ if (!s?.items.length) return;
285
+ const typed = this.text.slice(s.from, s.to);
286
+ // what starts with what is typed counts first ("/he": /help, not /bashes, which only contains it)
287
+ const starting = s.items.filter((item) => item.value.toLowerCase().startsWith(typed.toLowerCase()));
288
+ if (starting.length && starting.length < s.items.length) s.items = starting;
289
+ if (s.items.length === 1) { this.suggestions = s; this.selected = 0; this.acceptSuggestion(); return; }
290
+ let common = s.items[0].value;
291
+ for (const item of s.items) while (!item.value.toLowerCase().startsWith(common.toLowerCase())) common = common.slice(0, -1);
292
+ this.completing = true;
293
+ if (common.length > typed.length && common.toLowerCase().startsWith(typed.toLowerCase())) {
294
+ this.set(this.text.slice(0, s.from) + common + this.text.slice(s.to), s.from + common.length); // reopens the list
295
+ } else {
296
+ this.suggestions = s;
297
+ this.selected = 0;
298
+ this.onChange?.();
299
+ }
300
+ }
301
+
408
302
  acceptSuggestion() {
409
303
  const s = this.suggestions;
410
304
  if (!s) return null;
@@ -412,8 +306,8 @@ export class Editor {
412
306
  const before = this.text.slice(0, s.from);
413
307
  const after = this.text.slice(s.to);
414
308
  const value = item.value + (item.noSpace ? '' : ' ');
309
+ this.closeSuggestions();
415
310
  this.set(before + value + after.replace(/^ /, ''), before.length + value.length);
416
- this.suggestions = null;
417
311
  return item;
418
312
  }
419
313
 
@@ -463,11 +357,16 @@ export class Editor {
463
357
  return true;
464
358
  }
465
359
  if (name === 'tab' && !key.shift) {
466
- if (this.suggestions) this.acceptSuggestion();
360
+ this.complete();
361
+ return true;
362
+ }
363
+ if (name === 'tab' && this.suggestions) { // shift+tab: back up the list
364
+ this.selected = (this.selected - 1 + this.suggestions.items.length) % this.suggestions.items.length;
365
+ this.onChange?.();
467
366
  return true;
468
367
  }
469
368
  if (name === 'escape') {
470
- if (this.suggestions) { this.suggestionsDismissedFor = this.text; this.suggestions = null; return true; }
369
+ if (this.completing) { this.closeSuggestions(); this.onChange?.(); return true; }
471
370
  const now = Date.now();
472
371
  const twice = key.meta || now - this.lastEscape < 800; // meta: ESC ESC arriving in one chunk
473
372
  if (twice && this.text) { this.discard(); this.lastEscape = 0; return true; }
@@ -584,42 +483,37 @@ export class Editor {
584
483
  else this.restoreDraft(entry.text, entry.text.length, entry.parts);
585
484
  }
586
485
 
587
- // Render as a rounded box. Returns {lines, cursor:{row,col}}.
588
- // `title`: {left, right}, shown in the top border (left: the directory; right: the session's name).
486
+ // Draw the input without a box: "> " before its first row, two spaces before the others (the ! or # of bash and
487
+ // memory mode take the place of ">"). Returns {lines, cursor: {row, col}}.
589
488
  // `maxRows`: the most rows of text to show. A longer text shows a window of them that follows the cursor (from
590
- // viewTop), and the bottom border says how many rows are above and below it: the whole box used to be drawn and
591
- // the screen cut its top off, so that the cursor stood on a row that was not the one typed in.
592
- render(width, { placeholder = '', active = true, borderColor, title = null, maxRows = Infinity } = {}) {
593
- const boxW = Math.max(20, width - 1);
594
- const textW = boxW - 6;
489
+ // viewTop), and a line under it says how many rows are above and below.
490
+ render(width, { placeholder = '', active = true, maxRows = Infinity } = {}) {
491
+ const textW = Math.max(10, width - 1 - 2);
595
492
  const mode = this.mode;
596
- const color = borderColor || (mode === 'bash' ? c.magenta : mode === 'memory' ? c.blue : c.gray);
493
+ const color = mode === 'bash' ? c.magenta : mode === 'memory' ? c.blue : c.claude;
597
494
  const promptCh = mode === 'bash' ? '!' : mode === 'memory' ? '#' : '>';
598
495
  const body = mode === 'prompt' ? this.text : this.text.slice(1);
599
496
  this.textW = textW;
600
- const { rows: laid, r, col, offset } = this.place();
497
+ const { rows: laid, r, col } = this.place();
601
498
  const shown = Math.max(1, Math.min(laid.length, Math.floor(maxRows)));
602
499
  let top = Math.min(this.viewTop || 0, laid.length - shown); // the window moves only as far as the cursor needs
603
500
  if (r < top) top = r;
604
501
  else if (r >= top + shown) top = r - shown + 1;
605
502
  this.viewTop = top = Math.max(0, top);
606
503
 
607
- const lines = [topBorder(boxW, color, title)];
504
+ const lines = [];
608
505
  for (let i = top; i < top + shown; i++) {
609
506
  let content = laid[i].text;
610
507
  if (!body && i === 0 && placeholder) content = c.dim(truncate(placeholder, textW));
611
- else if (this.selection) content = this.paintRow(laid[i], offset);
612
- const lead = i === 0 ? (mode === 'prompt' ? promptCh : color(promptCh)) + ' ' : ' ';
613
- lines.push(color('│') + ' ' + lead + padEnd(content, textW) + ' ' + color('│'));
508
+ lines.push((i === 0 ? color(promptCh) + ' ' : ' ') + content);
614
509
  }
615
510
  const below = laid.length - top - shown;
616
511
  const hidden = [top ? `↑ ${top} more rows` : '', below ? `↓ ${below} more rows` : ''].filter(Boolean).join(' · ');
617
- const label = hidden && boxW > stringWidth(hidden) + 6 ? ` ${hidden} ` : '';
618
- lines.push(color(glyphs.bottomLeft + '─') + c.dim(label) + color('─'.repeat(boxW - 3 - stringWidth(label)) + glyphs.bottomRight));
619
- // `text`: where the text is in `lines` (its first row, how many, the column it starts at), for the mouse
620
- return { lines, cursor: active ? { row: r - top + 1, col: 4 + col } : null, text: { row: 1, rows: shown, col: 4 } };
512
+ if (hidden) lines.push(c.dim(` ${hidden}`));
513
+ return { lines, cursor: active ? { row: r - top, col: 2 + col } : null };
621
514
  }
622
515
 
516
+ // The completion list (Tab): a window of 8 items around the selected one.
623
517
  renderSuggestions(width) {
624
518
  if (!this.suggestions) return [];
625
519
  const items = this.suggestions.items;
@@ -627,11 +521,13 @@ export class Editor {
627
521
  const start = Math.max(0, Math.min(this.selected - Math.floor(max / 2), items.length - max));
628
522
  const shown = items.slice(start, start + max);
629
523
  const labelW = Math.min(30, Math.max(...shown.map((i) => stringWidth(i.label))) + 2);
630
- return shown.map((item, k) => {
524
+ const lines = shown.map((item, k) => {
631
525
  const sel = start + k === this.selected;
632
- const label = padEnd(truncate(item.label, labelW), labelW);
526
+ const label = padEnd(truncate(item.label, labelW - 2), labelW);
633
527
  const desc = item.description ? truncate(item.description, Math.max(10, width - labelW - 6)) : '';
634
528
  return sel ? ' ' + c.permission(label) + c.permission(desc) : ' ' + c.gray(label) + c.dim(desc);
635
529
  });
530
+ if (items.length > max) lines.push(c.dim(` ${this.selected + 1}/${items.length} · tab/↓ next · ⏎ choose · esc close`));
531
+ return lines;
636
532
  }
637
533
  }