@mumbrane/arrakis 0.6.0 → 0.6.1

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/tui.js +50 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mumbrane/arrakis",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Arrakis: a coding agent for your terminal, built by Mumbrane",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/src/tui.js CHANGED
@@ -45,7 +45,15 @@ export class FancyUI extends UI {
45
45
  this.cwd = process.cwd();
46
46
  this.fileCache = null;
47
47
  this.onKey = (str, key) => this.handleKey(str, key ?? {});
48
- this.onResize = () => this.render();
48
+ // Everything printed above the input box, so it can be reprinted when the
49
+ // terminal width changes (old lines re-wrap unpredictably otherwise).
50
+ this.transcript = [];
51
+ this.transcriptSize = 0;
52
+ this.lastCols = this.out.columns;
53
+ this.onResize = () => {
54
+ clearTimeout(this.resizeTimer);
55
+ this.resizeTimer = setTimeout(() => this.handleResize(), 60);
56
+ };
49
57
  process.once('exit', () => this.cleanup());
50
58
  }
51
59
 
@@ -67,7 +75,6 @@ export class FancyUI extends UI {
67
75
  this.historyList = fs.readFileSync(this.historyFile, 'utf8').split('\n').filter(Boolean).slice(-1000);
68
76
  } catch {}
69
77
  }
70
- this.render();
71
78
  }
72
79
 
73
80
  suspend() {
@@ -82,6 +89,7 @@ export class FancyUI extends UI {
82
89
 
83
90
  resume() {
84
91
  this.start();
92
+ this.render();
85
93
  }
86
94
 
87
95
  close() {
@@ -504,6 +512,9 @@ export class FancyUI extends UI {
504
512
 
505
513
  // Print complete lines above the live region.
506
514
  printAbove(s) {
515
+ this.transcript.push(s);
516
+ this.transcriptSize += s.length;
517
+ while (this.transcriptSize > 2_000_000 && this.transcript.length > 1) this.transcriptSize -= this.transcript.shift().length;
507
518
  if (!this.active) {
508
519
  this.out.write(s);
509
520
  return;
@@ -513,6 +524,16 @@ export class FancyUI extends UI {
513
524
  this.drawRegion();
514
525
  }
515
526
 
527
+ handleResize() {
528
+ const cols = this.out.columns;
529
+ if (cols === this.lastCols || !this.active) return this.render();
530
+ this.lastCols = cols;
531
+ // Clear screen and scrollback, reprint the conversation at the new width.
532
+ this.out.write('\x1b[?25l\x1b[2J\x1b[3J\x1b[H' + this.transcript.join(''));
533
+ this.regionRows = 0;
534
+ this.drawRegion();
535
+ }
536
+
516
537
  write(stream, s) {
517
538
  if (!s) return;
518
539
  this.partial += s;
@@ -623,21 +644,37 @@ export class FancyUI extends UI {
623
644
 
624
645
  // ---- live region ---------------------------------------------------------
625
646
 
647
+ // How many screen rows the cursor is below the top of the drawn region.
648
+ // If the terminal got narrower since we drew, long lines have re-wrapped
649
+ // onto extra rows, so count them at the current width.
650
+ rowsAboveCursor() {
651
+ const cols = Math.max(1, this.out.columns || 100);
652
+ let rows = 0;
653
+ for (let i = 0; i < this.regionCursorRow; i++) rows += Math.max(1, Math.ceil((this.regionWidths[i] ?? 0) / cols));
654
+ return rows + Math.floor((this.regionCursorCol ?? 0) / cols);
655
+ }
656
+
626
657
  eraseRegion() {
627
658
  if (!this.regionRows) return;
628
- if (this.regionCursorRow) this.out.write(`\x1b[${this.regionCursorRow}A`);
629
- this.out.write('\r\x1b[J');
659
+ const up = this.rowsAboveCursor();
660
+ this.out.write(`${up ? `\x1b[${up}A` : ''}\r\x1b[J`);
630
661
  this.regionRows = 0;
631
662
  this.regionCursorRow = 0;
663
+ this.regionCursorCol = 0;
664
+ this.regionWidths = [];
632
665
  }
633
666
 
634
667
  render() {
635
668
  if (!this.active) return;
636
669
  const { lines, cursorRow, cursorCol } = this.layout();
637
670
  let s = '\x1b[?25l';
638
- if (this.regionRows && this.regionCursorRow) s += `\x1b[${this.regionCursorRow}A`;
671
+ if (this.regionRows) {
672
+ const up = this.rowsAboveCursor();
673
+ if (up) s += `\x1b[${up}A`;
674
+ }
639
675
  s += '\r';
640
- s += lines.map((l) => `\x1b[2K${l}`).join('\n') + '\x1b[J';
676
+ const max = Math.max(1, (this.out.columns || 100) - 1);
677
+ s += lines.map((l) => `\x1b[2K${visibleLength(l) > max ? truncate(l, max) : l}`).join('\n') + '\x1b[J';
641
678
  const up = lines.length - 1 - cursorRow;
642
679
  if (up > 0) s += `\x1b[${up}A`;
643
680
  s += `\r${cursorCol > 0 ? `\x1b[${cursorCol}C` : ''}`;
@@ -645,17 +682,21 @@ export class FancyUI extends UI {
645
682
  this.out.write(s);
646
683
  this.regionRows = lines.length;
647
684
  this.regionCursorRow = cursorRow ?? lines.length - 1;
685
+ this.regionCursorCol = cursorCol;
686
+ this.regionWidths = lines.map((l) => Math.min(visibleLength(l), max));
648
687
  }
649
688
 
650
689
  drawRegion() {
651
690
  this.regionRows = 0;
652
691
  this.regionCursorRow = 0;
692
+ this.regionCursorCol = 0;
693
+ this.regionWidths = [];
653
694
  this.render();
654
695
  }
655
696
 
656
697
  layout() {
657
698
  const c = this.c;
658
- const cols = Math.max(30, this.out.columns || 100);
699
+ const cols = Math.max(30, (this.out.columns || 100) - 1);
659
700
  const lines = [];
660
701
  if (this.spinner) {
661
702
  const s = this.spinner;
@@ -687,7 +728,7 @@ export class FancyUI extends UI {
687
728
  const placeholder = !this.buffer && !this.pending?.label ? (this.busy ? 'Type to queue a message…' : 'Ask arrakis anything, / for commands, @ for files') : '';
688
729
  rows.forEach((r, i) => {
689
730
  const prefix = i === 0 ? c.accent('> ') : ' ';
690
- const body = placeholder && i === 0 ? c.gray(placeholder) : r.text;
731
+ const body = placeholder && i === 0 ? c.gray(truncate(placeholder, width)) : r.text;
691
732
  const pad = Math.max(0, width - visibleLength(body));
692
733
  lines.push(`${border('│')} ${prefix}${body}${' '.repeat(pad)} ${border('│')}`);
693
734
  if (this.cursor >= r.start && this.cursor <= r.end && cursorRow === null) {
@@ -771,7 +812,7 @@ export class FancyUI extends UI {
771
812
  if (resumed) rows.push(c.gray(` session: ${resumed}`));
772
813
  if (mcp.length) rows.push(c.gray(` mcp: ${mcp.map((s) => (s.status === 'connected' ? `${s.name} (${s.tools.length} tools)` : `${s.name} (failed)`)).join(', ')}`));
773
814
  if (hooks) rows.push(c.gray(` hooks: ${hooks} active`));
774
- const cols = Math.max(30, this.out.columns || 100);
815
+ const cols = Math.max(30, (this.out.columns || 100) - 1);
775
816
  const inner = Math.min(cols - 4, Math.max(46, ...rows.map(visibleLength)));
776
817
  const line = (s) => `${c.accent('│')} ${truncate(s, inner)}${' '.repeat(Math.max(0, inner - visibleLength(truncate(s, inner))))} ${c.accent('│')}`;
777
818
  let out = `\n${c.accent(`╭${'─'.repeat(inner + 2)}╮`)}\n${rows.map(line).join('\n')}\n${c.accent(`╰${'─'.repeat(inner + 2)}╯`)}\n`;