@tensorspark/dravix-code 1.5.0 → 1.5.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.
@@ -1,82 +1,236 @@
1
1
  import chalk from 'chalk';
2
+ import { colors, clipVis, padVis, visWidth } from './display.js';
3
+ import { fmtNum } from '../services/usage.js';
4
+ import { basename } from 'path';
2
5
  export class ChatLayout {
3
6
  rows;
4
7
  cols;
8
+ chatRow = 1;
9
+ didCleanup = false;
5
10
  constructor() {
6
11
  this.rows = process.stdout.rows ?? 24;
7
12
  this.cols = process.stdout.columns ?? 80;
8
13
  }
9
- // Scroll region occupies rows 1 … scrollEnd
10
- get scrollEnd() { return Math.max(this.rows - 3, 5); }
11
- get divRow() { return this.rows - 2; } // ────── divider
12
- get inputRow() { return this.rows - 1; } // › user input
13
- get hintRow() { return this.rows; } // Tab / @ / … hints
14
- makeSep() {
15
- return chalk.hex('#374151')(' ' + '─'.repeat(Math.max(this.cols - 4, 40)));
16
- }
17
- /** Full-screen mode: hide everything outside the app */
18
- enterFullScreen() {
14
+ refreshSize() {
19
15
  this.rows = process.stdout.rows ?? 24;
20
16
  this.cols = process.stdout.columns ?? 80;
21
- // Clear entire screen first
22
- process.stdout.write('\x1b[2J');
23
- // Set scroll region to ENTIRE terminal (rows 1 to rows)
24
- // This hides everything outside the app
25
- process.stdout.write(`\x1b[1;${this.rows}r`);
26
- // Move to top-left
27
- process.stdout.write('\x1b[1;1H');
28
17
  }
29
- /** Call once at the start of startRepl — sets scroll region + draws divider. */
30
- init() {
31
- this.rows = process.stdout.rows ?? 24;
32
- this.cols = process.stdout.columns ?? 80;
33
- // Set scroll region for chat (excluding bottom 3 rows for divider/input/hints)
34
- process.stdout.write(`\x1b[1;${this.scrollEnd}r`);
35
- this.redrawDivider();
18
+ /** Bottom 4 rows: top border, input line, bottom border, status bar (3 rows on tiny terminals). */
19
+ get bottomHeight() {
20
+ return this.rows >= 14 ? 4 : 3;
21
+ }
22
+ // Scroll region occupies rows 1 … scrollEnd
23
+ get scrollEnd() { return Math.max(this.rows - this.bottomHeight, 4); }
24
+ get topBorderRow() { return this.rows - this.bottomHeight + 1; }
25
+ get inputRow() { return this.rows - this.bottomHeight + 2; }
26
+ get botBorderRow() { return this.bottomHeight >= 4 ? this.rows - 1 : 0; }
27
+ get statusRow() { return this.rows; }
28
+ /** Compatibility getters */
29
+ get divRow() { return this.topBorderRow; }
30
+ get hintRow() { return this.statusRow; }
31
+ get boxOuterW() {
32
+ return Math.max(this.cols - 4, 24);
36
33
  }
37
- /** Redraw the divider line at divRow (outside the scroll region). */
38
- redrawDivider() {
39
- process.stdout.write(`\x1b[${this.divRow};1H\x1b[2K${this.makeSep()}`);
34
+ /** Inner editable width (between " │ › " and " │") */
35
+ get innerTextWidth() {
36
+ return Math.max(this.boxOuterW - 8, 10);
40
37
  }
41
- /** Place cursor at inputRow, optionally with text. */
42
- posInput(prompt, text = '') {
43
- process.stdout.write(`\x1b[${this.inputRow};1H\x1b[2K${prompt}${text}`);
38
+ /** Full-screen setup at app launch */
39
+ enterFullScreen() {
40
+ this.refreshSize();
41
+ process.stdout.write('\x1b[2J\x1b[3J');
42
+ process.stdout.write(`\x1b[1;${this.scrollEnd}r`);
43
+ process.stdout.write('\x1b[1;1H');
44
+ this.chatRow = 1;
44
45
  }
45
- /** Clear the input and hint rows. */
46
- clearInput() {
47
- process.stdout.write(`\x1b[${this.inputRow};1H\x1b[2K`);
48
- process.stdout.write(`\x1b[${this.hintRow};1H\x1b[2K`);
46
+ /** Set DECSTBM scroll region and draw the bottom composer frame. */
47
+ init(opts) {
48
+ this.refreshSize();
49
+ this.drawComposer({ focused: false, focusCursor: false, ...opts });
49
50
  }
50
- /** Write hint text at hintRow without moving cursor. */
51
- drawHint(hint) {
52
- process.stdout.write('\x1b7');
53
- process.stdout.write(`\x1b[${this.hintRow};1H\x1b[2K${hint}`);
54
- process.stdout.write('\x1b8');
51
+ /** Capture current terminal cursor row (when stdin is in raw mode and cursor is in scroll region). */
52
+ async captureChatRow() {
53
+ if (!process.stdin.isTTY || !process.stdin.isRaw)
54
+ return;
55
+ await new Promise((resolve) => {
56
+ let done = false;
57
+ const timer = setTimeout(() => {
58
+ if (!done) {
59
+ done = true;
60
+ process.stdin.removeListener('data', onDsr);
61
+ resolve();
62
+ }
63
+ }, 35);
64
+ const onDsr = (chunk) => {
65
+ const m = chunk.match(/\x1b\[(\d+);(\d+)R/);
66
+ if (m) {
67
+ const r = parseInt(m[1], 10);
68
+ if (r >= 1 && r <= this.scrollEnd) {
69
+ this.chatRow = r;
70
+ }
71
+ if (!done) {
72
+ done = true;
73
+ clearTimeout(timer);
74
+ process.stdin.removeListener('data', onDsr);
75
+ resolve();
76
+ }
77
+ }
78
+ };
79
+ process.stdin.on('data', onDsr);
80
+ process.stdout.write('\x1b[6n');
81
+ });
55
82
  }
56
- /** Move cursor to the bottom of the scroll region (ready for chat output). */
83
+ /** Move cursor back to the active chat line inside the scroll region. */
57
84
  enterChat() {
58
- process.stdout.write(`\x1b[${this.scrollEnd};1H`);
85
+ const r = Math.max(1, Math.min(this.chatRow, this.scrollEnd));
86
+ process.stdout.write(`\x1b[${r};1H`);
59
87
  }
60
- /** Handle terminal resize: recalculate dimensions and redraw fixed rows. */
61
- resize() {
62
- this.rows = process.stdout.rows ?? 24;
63
- this.cols = process.stdout.columns ?? 80;
88
+ /** Update scroll region without wiping chat, redraw composer. */
89
+ resize(opts) {
90
+ this.refreshSize();
64
91
  process.stdout.write(`\x1b[1;${this.scrollEnd}r`);
65
- this.redrawDivider();
66
- process.stdout.write(`\x1b[${this.inputRow};1H\x1b[2K`);
67
- process.stdout.write(`\x1b[${this.hintRow};1H\x1b[2K`);
68
- process.stdout.write(`\x1b[${this.inputRow};1H`);
92
+ if (opts?.focusCursor === false) {
93
+ const targetRow = Math.max(1, Math.min(this.chatRow, this.scrollEnd));
94
+ process.stdout.write(`\x1b[${targetRow};1H`);
95
+ }
96
+ this.drawComposer(opts);
97
+ }
98
+ /** Draw the framed composer box + status bar at the bottom rows. */
99
+ drawComposer(opts = {}) {
100
+ this.refreshSize();
101
+ const focused = opts.focused ?? true;
102
+ const streaming = opts.streaming ?? false;
103
+ const focusCursor = opts.focusCursor ?? focused;
104
+ const boxW = this.boxOuterW;
105
+ const innerW = this.innerTextWidth;
106
+ const borderPaint = streaming
107
+ ? chalk.hex('#4c1d95')
108
+ : focused
109
+ ? colors.brand
110
+ : colors.dim;
111
+ const topLeftBadge = streaming
112
+ ? borderPaint('╭─ ') + colors.accent('● streaming') + borderPaint(' ')
113
+ : borderPaint('╭');
114
+ const topLeftVis = visWidth(topLeftBadge);
115
+ const topFill = Math.max(boxW - topLeftVis - 3, 1);
116
+ const topLine = ' ' + topLeftBadge + borderPaint('─'.repeat(topFill) + '╮');
117
+ const promptSym = streaming
118
+ ? colors.muted('›')
119
+ : focused
120
+ ? colors.primary.bold('›')
121
+ : colors.muted('›');
122
+ const leftMark = opts.leftOverflow ? colors.primary('‹') : ' ';
123
+ const rightMark = opts.rightOverflow ? colors.primary('›') : ' ';
124
+ const rawText = opts.displayText ?? '';
125
+ let bodyContent;
126
+ if (rawText.length > 0) {
127
+ bodyContent = padVis(rawText, innerW);
128
+ }
129
+ else if (opts.placeholder) {
130
+ bodyContent = padVis(colors.muted(clipVis(opts.placeholder, innerW)), innerW);
131
+ }
132
+ else {
133
+ bodyContent = ' '.repeat(innerW);
134
+ }
135
+ const inputLine = ' ' + borderPaint('│') + ' ' + promptSym + leftMark + bodyContent + rightMark + borderPaint('│');
136
+ const botLine = ' ' + borderPaint('╰' + '─'.repeat(Math.max(boxW - 4, 1)) + '╯');
137
+ // Status / hints bar on the bottom row
138
+ let statusLine = '';
139
+ if (opts.notice) {
140
+ statusLine = ' ' + colors.warn(clipVis(opts.notice, Math.max(this.cols - 6, 16)));
141
+ }
142
+ else {
143
+ const leftParts = [];
144
+ if (opts.cwd) {
145
+ leftParts.push(colors.subtext(basename(opts.cwd) || opts.cwd));
146
+ }
147
+ if (opts.meta?.plan) {
148
+ leftParts.push(opts.meta.plan === 'Plus'
149
+ ? colors.accent('Plus')
150
+ : colors.muted('Free'));
151
+ }
152
+ if (opts.meta?.usage) {
153
+ const u = opts.meta.usage;
154
+ leftParts.push(colors.muted(`${fmtNum(u.tokens_used)}/${fmtNum(u.limit)}`));
155
+ }
156
+ const leftStr = leftParts.join(colors.dim(' · '));
157
+ const rightStr = streaming
158
+ ? colors.muted('Ctrl+C') + colors.dim(' cancel')
159
+ : colors.muted('@') + colors.dim(' file ') +
160
+ colors.muted('/') + colors.dim(' cmd ') +
161
+ colors.muted('Tab') + colors.dim(' path ') +
162
+ colors.muted('↑↓') + colors.dim(' history');
163
+ const avail = Math.max(this.cols - 8, 20);
164
+ const lw = visWidth(leftStr);
165
+ const rw = visWidth(rightStr);
166
+ if (lw + rw + 4 <= avail) {
167
+ const gap = ' '.repeat(avail - lw - rw);
168
+ statusLine = ' ' + leftStr + gap + rightStr;
169
+ }
170
+ else if (rw <= avail) {
171
+ statusLine = ' ' + rightStr;
172
+ }
173
+ else {
174
+ statusLine = ' ' + colors.dim('@ file · / cmd · Tab path');
175
+ }
176
+ }
177
+ if (!focusCursor) {
178
+ process.stdout.write('\x1b7');
179
+ }
180
+ process.stdout.write(`\x1b[${this.topBorderRow};1H\x1b[2K${topLine}`);
181
+ process.stdout.write(`\x1b[${this.inputRow};1H\x1b[2K${inputLine}`);
182
+ if (this.botBorderRow > 0) {
183
+ process.stdout.write(`\x1b[${this.botBorderRow};1H\x1b[2K${botLine}`);
184
+ }
185
+ process.stdout.write(`\x1b[${this.statusRow};1H\x1b[2K${statusLine}`);
186
+ if (focusCursor) {
187
+ // " │ › " = 6 columns (1-indexed col 7 is text start)
188
+ const colOffset = Math.max(0, Math.min(opts.cursorOffset ?? 0, innerW));
189
+ process.stdout.write(`\x1b[${this.inputRow};${7 + colOffset}H`);
190
+ }
191
+ else {
192
+ process.stdout.write('\x1b8');
193
+ }
194
+ }
195
+ /** Clear input & status rows (e.g., when preparing to exit). */
196
+ clearBottom() {
197
+ this.refreshSize();
198
+ for (let r = this.topBorderRow; r <= this.rows; r++) {
199
+ process.stdout.write(`\x1b[${r};1H\x1b[2K`);
200
+ }
69
201
  }
70
- /** Clear all chat lines in the scroll region. */
71
- clearScrollRegion() {
202
+ /** Clear only chat rows (1 .. scrollEnd), keeping the bottom composer intact. */
203
+ clearChat(opts) {
204
+ this.refreshSize();
205
+ process.stdout.write(`\x1b[1;${this.scrollEnd}r`);
72
206
  for (let r = 1; r <= this.scrollEnd; r++) {
73
207
  process.stdout.write(`\x1b[${r};1H\x1b[2K`);
74
208
  }
75
- process.stdout.write(`\x1b[1;1H`);
209
+ this.chatRow = 1;
210
+ this.drawComposer({ focused: false, focusCursor: false, ...opts });
211
+ process.stdout.write('\x1b[1;1H');
76
212
  }
77
- /** Reset scroll region and clean up on process exit. */
78
- cleanup() {
79
- process.stdout.write('\x1b[r');
80
- process.stdout.write(`\x1b[${this.rows};1H\n`);
213
+ /** Reset scroll region and leave terminal clean on exit while preserving chat history. */
214
+ cleanup(preserveChat = true, atChatCursor = false) {
215
+ if (this.didCleanup)
216
+ return;
217
+ this.didCleanup = true;
218
+ this.refreshSize();
219
+ if (preserveChat) {
220
+ if (atChatCursor)
221
+ process.stdout.write('\x1b7');
222
+ process.stdout.write('\x1b[r');
223
+ this.clearBottom();
224
+ if (atChatCursor) {
225
+ process.stdout.write('\x1b8');
226
+ }
227
+ else {
228
+ this.enterChat();
229
+ }
230
+ }
231
+ else {
232
+ process.stdout.write('\x1b[r');
233
+ process.stdout.write('\x1b[2J\x1b[1;1H');
234
+ }
81
235
  }
82
236
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tensorspark/dravix-code",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "AI-powered coding assistant CLI — Dravix Code",
5
5
  "type": "module",
6
6
  "bin": {