@bhooai/nexus-cli 2.0.11 → 2.0.12

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": "@bhooai/nexus-cli",
3
- "version": "2.0.11",
3
+ "version": "2.0.12",
4
4
  "description": "BhooAI Nexus v2 CLI — init, dev, build, test, scaffolding, plugins, queue, db.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/devPanel.ts CHANGED
@@ -35,6 +35,8 @@ export interface DevPanelOptions {
35
35
 
36
36
  type Tab = 'services' | 'logs' | 'commands' | 'features' | 'settings';
37
37
 
38
+ let isCapturing = false;
39
+
38
40
  const TABS: Array<{ id: Tab; label: string }> = [
39
41
  { id: 'services', label: 'Services' },
40
42
  { id: 'logs', label: 'Logs' },
@@ -121,16 +123,27 @@ export async function startDevPanel(opts: DevPanelOptions): Promise<void> {
121
123
  };
122
124
 
123
125
  let lastRender = 0;
126
+ let throttleTimer: ReturnType<typeof setTimeout> | null = null;
124
127
 
125
128
  function throttledRender(): void {
129
+ if (isCapturing || state.quit) return;
126
130
  const now = Date.now();
127
- if (now - lastRender < 80) return;
131
+ if (now - lastRender < 80) {
132
+ if (!throttleTimer) {
133
+ throttleTimer = setTimeout(() => {
134
+ throttleTimer = null;
135
+ lastRender = Date.now();
136
+ render();
137
+ }, 80);
138
+ }
139
+ return;
140
+ }
128
141
  lastRender = now;
129
142
  render();
130
143
  }
131
144
 
132
145
  function render(): void {
133
- if (state.quit) return;
146
+ if (state.quit || isCapturing) return;
134
147
  tui.draw(buildRows(state, services, palette, projectRoot), helpText(state));
135
148
  }
136
149
 
@@ -146,6 +159,7 @@ export async function startDevPanel(opts: DevPanelOptions): Promise<void> {
146
159
  render();
147
160
 
148
161
  await tui.wait();
162
+ if (throttleTimer) { clearTimeout(throttleTimer); throttleTimer = null; }
149
163
  tui.exit();
150
164
  services.onLog = null;
151
165
  opts.onQuit?.();
@@ -206,12 +220,6 @@ function onKey(state: PanelState, str: string, key: KeyInfo, ctx: Ctx): void {
206
220
  ctx.redraw();
207
221
  return;
208
222
  }
209
- if (key.name === 'q') {
210
- state.confirmQuit = true;
211
- state.quitIndex = 0;
212
- ctx.redraw();
213
- return;
214
- }
215
223
  if (key.name === 'escape') {
216
224
  if (state.tab === 'commands' && state.cmdFilter) {
217
225
  state.cmdFilter = '';
@@ -231,7 +239,8 @@ function onKey(state: PanelState, str: string, key: KeyInfo, ctx: Ctx): void {
231
239
  }
232
240
  if (state.showHelp) return;
233
241
 
234
- // Type-to-filter in the Commands tab.
242
+ // Type-to-filter in the Commands tab — must be before single-letter shortcuts
243
+ // so 'q','s','t','r','l','a','d' act as filter characters instead of hotkeys.
235
244
  if (state.tab === 'commands' && str && !key.ctrl && !key.meta && str.length === 1 && str !== '?') {
236
245
  state.cmdFilter += str;
237
246
  const fp = filteredPalette(ctx.palette, state.cmdFilter);
@@ -255,6 +264,12 @@ function onKey(state: PanelState, str: string, key: KeyInfo, ctx: Ctx): void {
255
264
  ctx.redraw();
256
265
  return;
257
266
  }
267
+ if (key.name === 'q') {
268
+ state.confirmQuit = true;
269
+ state.quitIndex = 0;
270
+ ctx.redraw();
271
+ return;
272
+ }
258
273
 
259
274
  switch (key.name) {
260
275
  case 'tab': {
@@ -286,13 +301,13 @@ function onKey(state: PanelState, str: string, key: KeyInfo, ctx: Ctx): void {
286
301
  void activate(state, ctx);
287
302
  break;
288
303
  case 's':
289
- ctx.services.start(ctx.services.services[state.svcIndex]?.name ?? '');
304
+ if (state.tab === 'services' || state.tab === 'logs') ctx.services.start(ctx.services.services[state.svcIndex]?.name ?? '');
290
305
  break;
291
306
  case 't':
292
- ctx.services.stop(ctx.services.services[state.svcIndex]?.name ?? '');
307
+ if (state.tab === 'services' || state.tab === 'logs') ctx.services.stop(ctx.services.services[state.svcIndex]?.name ?? '');
293
308
  break;
294
309
  case 'r':
295
- ctx.services.restart(ctx.services.services[state.svcIndex]?.name ?? '');
310
+ if (state.tab === 'services' || state.tab === 'logs') ctx.services.restart(ctx.services.services[state.svcIndex]?.name ?? '');
296
311
  break;
297
312
  case 'l':
298
313
  state.tab = 'logs';
@@ -394,7 +409,10 @@ async function runSelectedCommand(state: PanelState, cmd: CommandItem, inlineArg
394
409
  ctx.redraw();
395
410
 
396
411
  if (cmd.interactive) {
397
- // Suspend panel → real TTY → resume.
412
+ // Suspend panel → real TTY → resume. Suppress log redraws while suspended
413
+ const prevLog = ctx.services.onLog;
414
+ ctx.services.onLog = null;
415
+ isCapturing = true;
398
416
  ctx.tui.suspend();
399
417
  try {
400
418
  const bin = resolveCliBin();
@@ -404,6 +422,8 @@ async function runSelectedCommand(state: PanelState, cmd: CommandItem, inlineArg
404
422
  push(state, `✗ ${cmd.name} failed: ${(err as Error).message}`);
405
423
  }
406
424
  ctx.tui.resume();
425
+ isCapturing = false;
426
+ ctx.services.onLog = prevLog;
407
427
  ctx.redraw();
408
428
  return;
409
429
  }
@@ -411,6 +431,9 @@ async function runSelectedCommand(state: PanelState, cmd: CommandItem, inlineArg
411
431
  const argv = inlineArgs ? inlineArgs.trim().split(/\s+/) : [];
412
432
  const origOut = process.stdout.write.bind(process.stdout);
413
433
  const origErr = process.stderr.write.bind(process.stderr);
434
+ const prevLog2 = ctx.services.onLog;
435
+ isCapturing = true;
436
+ ctx.services.onLog = null;
414
437
  const sink = (chunk: string | Buffer) => {
415
438
  push(state, String(chunk));
416
439
  return true;
@@ -425,6 +448,8 @@ async function runSelectedCommand(state: PanelState, cmd: CommandItem, inlineArg
425
448
  } finally {
426
449
  (process.stdout as unknown as { write: Function }).write = origOut;
427
450
  (process.stderr as unknown as { write: Function }).write = origErr;
451
+ isCapturing = false;
452
+ ctx.services.onLog = prevLog2;
428
453
  }
429
454
  ctx.redraw();
430
455
  }
@@ -437,12 +462,17 @@ function push(state: PanelState, text: string): void {
437
462
  }
438
463
 
439
464
  async function runSpawned(cmd: string, args: string[], ctx: Ctx): Promise<void> {
465
+ const prevLog = ctx.services.onLog;
466
+ ctx.services.onLog = null;
467
+ isCapturing = true;
440
468
  ctx.tui.suspend();
441
469
  try {
442
470
  const bin = resolveCliBin();
443
471
  await spawnNode(bin, [cmd, ...args]);
444
472
  } catch { /* ignore */ }
445
473
  ctx.tui.resume();
474
+ isCapturing = false;
475
+ ctx.services.onLog = prevLog;
446
476
  ctx.redraw();
447
477
  }
448
478
 
@@ -530,23 +560,6 @@ function prevSelectable(list: PaletteRow[], current: number, dir: number): numbe
530
560
  function filteredPalette(palette: PaletteRow[], filter: string): PaletteRow[] {
531
561
  if (!filter) return palette;
532
562
  const q = filter.toLowerCase();
533
- const result: PaletteRow[] = [];
534
- let currentHeader: PaletteRow | null = null;
535
- let headerHasMatch = false;
536
- for (const row of palette) {
537
- if (row.kind === 'header') {
538
- if (currentHeader && headerHasMatch) result.push(currentHeader);
539
- currentHeader = row;
540
- headerHasMatch = false;
541
- continue;
542
- }
543
- if (row.cmd.name.toLowerCase().includes(q) || row.cmd.desc.toLowerCase().includes(q)) {
544
- headerHasMatch = true;
545
- result.push(row);
546
- }
547
- }
548
- if (currentHeader && headerHasMatch) result.push(currentHeader);
549
- // Re-headers got pushed to the end; rebuild with headers in the right place.
550
563
  const ordered: PaletteRow[] = [];
551
564
  let pendingHeader: PaletteRow | null = null;
552
565
  for (const row of palette) {
@@ -819,5 +832,5 @@ function helpText(state: PanelState): string {
819
832
  }
820
833
 
821
834
  function clamp(n: number, lo: number, hi: number): number {
822
- return Math.min(Math.max(n, lo), Math.max(lo, hi));
835
+ return Math.min(Math.max(n, lo), hi);
823
836
  }
@@ -219,12 +219,23 @@ export class ServiceManager {
219
219
  }
220
220
 
221
221
  private push(svc: ManagedService, text: string, source: 'stdout' | 'stderr' | 'system' = 'stdout'): void {
222
- const lines = text.split(/\r?\n/);
222
+ // Sanitize terminal control sequences that would otherwise move the cursor
223
+ // when rendered in the TUI panel (e.g. \r progress bars, clear lines, alt buffer)
224
+ const sanitized = text
225
+ .replace(/\r/g, '')
226
+ .replace(/\x1b\[\?25[hl]/g, '')
227
+ .replace(/\x1b\[\?1049[hl]/g, '')
228
+ .replace(/\x1b\[2[JK]/g, '')
229
+ .replace(/\x1b\[[0-9;]*[ABCDGKHf]/g, '');
230
+ const lines = sanitized.split(/\n/);
223
231
  for (const line of lines) {
224
232
  if (!line.trim()) continue;
233
+ // Keep color codes for panel rendering, but strip for level detection
234
+ const stripped = line.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '');
235
+ if (!stripped.trim()) continue;
225
236
  svc.logBuffer.push(line);
226
237
  if (svc.logBuffer.length > MAX_LOG_LINES) svc.logBuffer.shift();
227
- this.allLogs.push({ service: svc.name, ts: Date.now(), source, level: detectLevel(line), line });
238
+ this.allLogs.push({ service: svc.name, ts: Date.now(), source, level: detectLevel(stripped), line });
228
239
  if (this.allLogs.length > MAX_ALL_LOGS) this.allLogs.shift();
229
240
  }
230
241
  this.onLog?.(svc.name);
package/src/launcher.ts CHANGED
@@ -95,10 +95,22 @@ function handleMenuKey(state: MenuState, items: MenuItem[], tui: Tui, _str: stri
95
95
  return;
96
96
  }
97
97
  if (key.name === 'up') {
98
- state.index = Math.max(0, state.index - 1);
98
+ let next = state.index;
99
+ for (let i = 0; i < items.length; i++) {
100
+ next = Math.max(0, next - 1);
101
+ if (items[next]?.enabled) break;
102
+ if (next === 0) break;
103
+ }
104
+ state.index = next;
99
105
  renderMenu(state, items, tui);
100
106
  } else if (key.name === 'down') {
101
- state.index = Math.min(items.length - 1, state.index + 1);
107
+ let next = state.index;
108
+ for (let i = 0; i < items.length; i++) {
109
+ next = Math.min(items.length - 1, next + 1);
110
+ if (items[next]?.enabled) break;
111
+ if (next === items.length - 1) break;
112
+ }
113
+ state.index = next;
102
114
  renderMenu(state, items, tui);
103
115
  } else if (key.name === 'return' || key.name === 'enter') {
104
116
  const item = items[state.index];
package/src/tui.ts CHANGED
@@ -91,27 +91,40 @@ export class Tui {
91
91
  readline.emitKeypressEvents(input);
92
92
  input.on('keypress', this.keypress);
93
93
  }
94
- output.write(ANSI.altOn + ANSI.hideCursor);
94
+ if (output.isTTY) output.write(ANSI.altOn + ANSI.hideCursor + ANSI.clear);
95
95
  process.on('SIGINT', this.onSignal);
96
96
  process.on('SIGTERM', this.onSignal);
97
97
  }
98
98
 
99
99
  /** Leave the screen so a child process can own the TTY. */
100
100
  suspend(): void {
101
- input.setRawMode?.(false);
102
- output.write(ANSI.altOff + ANSI.showCursor);
101
+ input.removeListener('keypress', this.keypress);
102
+ process.removeListener('SIGINT', this.onSignal);
103
+ process.removeListener('SIGTERM', this.onSignal);
104
+ if (input.isTTY) input.setRawMode?.(false);
105
+ if (output.isTTY) output.write(ANSI.altOff + ANSI.showCursor);
103
106
  }
104
107
 
105
108
  resume(): void {
106
- input.setRawMode?.(true);
107
- input.resume?.();
108
- output.write(ANSI.altOn + ANSI.hideCursor);
109
+ // Ensure idempotent — remove stale handlers before re-adding.
110
+ input.removeListener('keypress', this.keypress);
111
+ process.removeListener('SIGINT', this.onSignal);
112
+ process.removeListener('SIGTERM', this.onSignal);
113
+ if (input.isTTY) {
114
+ input.setRawMode?.(true);
115
+ input.resume?.();
116
+ readline.emitKeypressEvents(input);
117
+ input.on('keypress', this.keypress);
118
+ }
119
+ process.on('SIGINT', this.onSignal);
120
+ process.on('SIGTERM', this.onSignal);
121
+ if (output.isTTY) output.write(ANSI.altOn + ANSI.hideCursor + ANSI.clear);
109
122
  }
110
123
 
111
124
  exit(): void {
112
125
  input.removeListener('keypress', this.keypress);
113
- input.setRawMode?.(false);
114
- output.write(ANSI.altOff + ANSI.showCursor);
126
+ if (input.isTTY) input.setRawMode?.(false);
127
+ if (output.isTTY) output.write(ANSI.altOff + ANSI.showCursor);
115
128
  process.removeListener('SIGINT', this.onSignal);
116
129
  process.removeListener('SIGTERM', this.onSignal);
117
130
  }
@@ -119,25 +132,71 @@ export class Tui {
119
132
  /** Paint a frame. `rows` are the body; `footer` is the final status line. */
120
133
  draw(rows: string[], footer = ''): void {
121
134
  const H = output.rows || 24;
135
+ const W = output.columns || 80;
122
136
  const body = fitLines(rows);
123
- let out = ANSI.clear;
137
+ const clippedFooter = footer ? clipAnsi(footer, W) : '';
138
+ let out = '';
139
+ if (output.isTTY) out += ANSI.clear;
124
140
  out += body.join('\n');
125
- out += ANSI.move(H, 1);
126
- out += footer;
127
- output.write(out);
141
+ // Pad body to H-1 lines so previous longer frames don't leave stale lines.
142
+ const bodyLines = body.length;
143
+ if (bodyLines < H - 1) out += '\n'.repeat(H - 1 - bodyLines);
144
+ out += ANSI.move(H, 1) + '\x1b[2K';
145
+ if (clippedFooter) out += clippedFooter;
146
+ if (output.isTTY) output.write(out);
147
+ else output.write(body.join('\n') + (clippedFooter ? '\n' + stripAnsi(clippedFooter) : '') + '\n');
128
148
  }
129
149
 
130
150
  /** Resolve once `quit` becomes true. */
131
151
  async wait(): Promise<void> {
132
152
  while (!this._quit) {
133
- await new Promise((r) => setTimeout(r, 100));
153
+ await new Promise((r) => setTimeout(r, 50));
134
154
  }
135
155
  }
136
156
  }
137
157
 
138
- /** Strip ANSI color codes from a string. */
158
+ /** Strip ANSI escape codes from a string (SGR + CSI + ESC sequences). */
139
159
  export function stripAnsi(text: string): string {
140
- return text.replace(/\x1b\[[0-9;]*m/g, '');
160
+ return text
161
+ .replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '')
162
+ .replace(/\x1b\][^\x07]*\x07/g, '')
163
+ .replace(/\x1b\(B/g, '')
164
+ .replace(/\x1b\[[0-9;]*m/g, '');
165
+ }
166
+
167
+ /** Clip an ANSI-colored string to `width` visible columns, preserving leading color. */
168
+ function clipAnsi(text: string, width: number): string {
169
+ if (stripAnsi(text).length <= width) return text;
170
+ let out = '';
171
+ let used = 0;
172
+ let i = 0;
173
+ let sawColor = false;
174
+ while (i < text.length && used < width) {
175
+ const ch = text[i]!;
176
+ if (ch === '\x1b') {
177
+ let j = i + 1;
178
+ if (text.charCodeAt(j) === 0x5b) {
179
+ j++;
180
+ while (j < text.length) {
181
+ const c = text.charCodeAt(j);
182
+ if (c >= 0x20 && c <= 0x3f) { j++; continue; }
183
+ break;
184
+ }
185
+ if (j < text.length) j++;
186
+ } else if (j < text.length) {
187
+ j++;
188
+ }
189
+ out += text.slice(i, j);
190
+ sawColor = true;
191
+ i = j;
192
+ } else {
193
+ out += ch;
194
+ used++;
195
+ i++;
196
+ }
197
+ }
198
+ if (sawColor && !out.endsWith(ANSI.reset)) out += ANSI.reset;
199
+ return out;
141
200
  }
142
201
 
143
202
  /** Spawn `node <bin> <args>` with inherited stdio (used for interactive CLI commands). */
package/src/wizard.ts CHANGED
@@ -300,10 +300,9 @@ function pageExamples(state: WizardState, key: KeyInfo, tui: Tui): void {
300
300
  }
301
301
 
302
302
  function pageAdmin(state: WizardState, str: string, key: KeyInfo): void {
303
- const isYes = state.features.has('admin');
304
303
  if (key.name === 'left' || key.name === 'right' || key.name === 'up' || key.name === 'down') {
305
- state.features.add('admin');
306
- if (isYes) state.features.delete('admin');
304
+ if (state.features.has('admin')) state.features.delete('admin');
305
+ else state.features.add('admin');
307
306
  } else if (key.name === 'return' || key.name === 'enter') {
308
307
  state.page = 'features';
309
308
  } else if (str && /y/i.test(str)) {
@@ -501,7 +500,7 @@ function titleBar(state: WizardState, W: number): string {
501
500
  const n = stepIndex(state.page) + 1;
502
501
  const right = `${ANSI.dim}Step ${n} of ${STEPS.length}${ANSI.reset}`;
503
502
  const gap = Math.max(1, W - visibleWidth(left) - visibleWidth(right));
504
- return `${left}${' '.repeat(gap)}${right}`;
503
+ return clipVisible(`${left}${' '.repeat(gap)}${right}`, W);
505
504
  }
506
505
 
507
506
  function progressDots(state: WizardState): string {