@nbtca/prompt 1.5.15 → 1.5.17

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/dist/app/app.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { ansi, ensureCursorRestored } from '../core/canvas.js';
2
2
  import { composeFrameLines, computeBodyRows, diffFrame } from './frame.js';
3
- import { isPrintableKey, KeyStreamDecoder, routeGlobalKey } from './keys.js';
3
+ import { frameWidth } from '../core/theme.js';
4
+ import { isPrintableKey, KeyStreamDecoder, routeGlobalKey, } from './keys.js';
4
5
  import { renderHeader, renderFooter, resolveChromeLayout } from './chrome.js';
5
6
  import { homeView } from './views/home.js';
6
7
  import { scheduleView } from './views/schedule.js';
@@ -46,9 +47,13 @@ export async function runApp() {
46
47
  .finally(() => pendingLoads.delete(id));
47
48
  pendingLoads.set(id, pending);
48
49
  }
49
- function size() {
50
+ function terminalSize() {
50
51
  return { rows: process.stdout.rows || 24, cols: process.stdout.columns || 80 };
51
52
  }
53
+ function size() {
54
+ const terminal = terminalSize();
55
+ return { rows: terminal.rows, cols: frameWidth(terminal.cols) };
56
+ }
52
57
  const ctx = {
53
58
  signal: lifecycle.signal,
54
59
  get size() {
@@ -76,6 +81,7 @@ export async function runApp() {
76
81
  if (suspended || !running)
77
82
  return;
78
83
  const { rows, cols } = size();
84
+ const terminalCols = terminalSize().cols;
79
85
  const active = nativeViews[view];
80
86
  const tabs = getAppTabs();
81
87
  const chrome = resolveChromeLayout(rows);
@@ -87,10 +93,10 @@ export async function runApp() {
87
93
  const bodyScroll = !helpOpen && active?.capturesInput?.() ? Number.MAX_SAFE_INTEGER : scroll;
88
94
  const height = computeBodyRows(rows, chrome.headerLines, chrome.footerLines);
89
95
  lastBody = { length: body.length, height };
90
- const footer = renderFooter(view, cols, tabs.length, helpOpen ? t().help.close : active?.footerHint?.(tabs.length, cols), chrome.footerLines, !helpOpen && active?.scrollsBody?.() === true ? scrollPercent() : undefined);
91
- const lines = composeFrameLines(header, body, footer, rows, cols, bodyScroll);
92
- const patch = diffFrame(painted?.cols === cols ? painted.lines : undefined, lines);
93
- painted = { cols, lines };
96
+ const footer = renderFooter(view, cols, tabs.length, helpOpen ? t().help.close : active?.footerHint?.(tabs.length, cols), chrome.footerLines, helpOpen || active?.scrollsBody?.() === true ? scrollPercent() : undefined);
97
+ const lines = composeFrameLines(header, body, footer, rows, terminalCols, bodyScroll);
98
+ const patch = diffFrame(painted?.cols === terminalCols ? painted.lines : undefined, lines);
99
+ painted = { cols: terminalCols, lines };
94
100
  if (patch)
95
101
  process.stdout.write(patch);
96
102
  scheduleBusyTick(active?.isBusy?.() === true);
@@ -133,15 +139,8 @@ export async function runApp() {
133
139
  render();
134
140
  return;
135
141
  }
136
- if (helpOpen) {
137
- if (key === '?' || key === '\x1b') {
138
- helpOpen = false;
139
- render();
140
- }
141
- return;
142
- }
143
- if (key === '?') {
144
- helpOpen = true;
142
+ if (key === '?' || (helpOpen && key === '\x1b')) {
143
+ helpOpen = !helpOpen;
145
144
  scroll = 0;
146
145
  render();
147
146
  return;
@@ -151,6 +150,10 @@ export async function runApp() {
151
150
  quit();
152
151
  return;
153
152
  }
153
+ if (helpOpen) {
154
+ scrollBody(g);
155
+ return;
156
+ }
154
157
  if (g.back) {
155
158
  if (active?.handleBack?.(ctx)) {
156
159
  scroll = 0; // the new sub-view's content height has nothing to do with the old one's
@@ -166,29 +169,30 @@ export async function runApp() {
166
169
  switchTo(g.switchTo);
167
170
  return;
168
171
  }
169
- if (g.scrollBy) {
170
- if (active?.capturesPageKeys?.()) {
171
- active.handleKey?.(key, ctx);
172
- scroll = 0;
173
- render();
174
- return;
175
- }
176
- const page = Math.max(1, ctx.bodyRows - 2);
177
- scrollTo(scroll + g.scrollBy * page);
172
+ if (g.scrollBy && active?.capturesPageKeys?.()) {
173
+ active.handleKey?.(key, ctx);
174
+ scroll = 0;
175
+ render();
178
176
  return;
179
177
  }
180
- if ((g.scrollLines !== undefined || g.scrollTo !== undefined) && active?.scrollsBody?.()) {
181
- if (g.scrollTo === 'top')
182
- scrollTo(0);
183
- else if (g.scrollTo === 'end')
184
- scrollTo(maxScroll());
185
- else
186
- scrollTo(scroll + (g.scrollLines ?? 0));
178
+ if ((g.scrollBy || active?.scrollsBody?.()) && scrollBody(g))
187
179
  return;
188
- }
189
180
  active?.handleKey?.(key, ctx);
190
181
  render();
191
182
  }
183
+ function scrollBody(g) {
184
+ if (g.scrollBy)
185
+ scrollTo(scroll + g.scrollBy * Math.max(1, ctx.bodyRows - 2));
186
+ else if (g.scrollTo === 'top')
187
+ scrollTo(0);
188
+ else if (g.scrollTo === 'end')
189
+ scrollTo(maxScroll());
190
+ else if (g.scrollLines !== undefined)
191
+ scrollTo(scroll + g.scrollLines);
192
+ else
193
+ return false;
194
+ return true;
195
+ }
192
196
  function dispatchKeys(keys) {
193
197
  for (let index = 0; index < keys.length && running && !suspended; index += 1) {
194
198
  let key = keys[index];
@@ -1,7 +1,7 @@
1
- import { type, space, glyph, brandMark, MAX_FRAME_COLS } from '../core/theme.js';
1
+ import { c, type, space, glyph, brandMark, bodyEdge } from '../core/theme.js';
2
2
  import { pickIcon } from '../core/icons.js';
3
3
  import { t } from '../i18n/index.js';
4
- import { clipAnsiToVisualWidth, visualWidth } from '../core/text.js';
4
+ import { clipAnsiToVisualWidth, visualWidth, wrapAnsiWithIndent } from '../core/text.js';
5
5
  export const HEADER_LINES = 3;
6
6
  export const FOOTER_LINES = 2;
7
7
  export function resolveChromeLayout(rows) {
@@ -19,10 +19,9 @@ export function resolveChromeLayout(rows) {
19
19
  return { headerLines: 0, footerLines: 0 };
20
20
  }
21
21
  function renderRule(cols) {
22
- const terminal = Number.isFinite(cols) ? Math.max(1, Math.floor(cols)) : 80;
23
- const width = Math.min(terminal, MAX_FRAME_COLS);
22
+ const width = Number.isFinite(cols) ? Math.max(1, Math.floor(cols)) : 80;
24
23
  const indent = visualWidth(space.indent) < width ? space.indent : '';
25
- const ruleWidth = Math.max(1, width - visualWidth(indent) * 2);
24
+ const ruleWidth = Math.max(1, bodyEdge(width) - visualWidth(indent));
26
25
  return indent + type.hint(glyph.rule().repeat(ruleWidth));
27
26
  }
28
27
  function renderTabs(views, active, cols) {
@@ -62,7 +61,9 @@ function renderTabs(views, active, cols) {
62
61
  export function renderContextPath(segments, cols) {
63
62
  const chevron = pickIcon('›', '>');
64
63
  const ellipsis = pickIcon('…', '...');
65
- const width = Number.isFinite(cols) ? Math.max(1, Math.floor(cols)) : Number.POSITIVE_INFINITY;
64
+ const width = Number.isFinite(cols)
65
+ ? bodyEdge(Math.max(1, Math.floor(cols)))
66
+ : Number.POSITIVE_INFINITY;
66
67
  for (let start = 0; start < segments.length; start += 1) {
67
68
  const shown = segments.slice(start);
68
69
  const last = shown.length - 1;
@@ -94,46 +95,41 @@ export function renderHeader(views, active, cols, lineCount = HEADER_LINES, cont
94
95
  return [brand, tabs];
95
96
  return [brand, tabs, rule];
96
97
  }
98
+ export function offlineNotice(cols) {
99
+ const notice = `${c.warn(pickIcon('⚠', '!'))} ${t().common.offline}`;
100
+ return [...wrapAnsiWithIndent(notice, cols, space.indent), ''];
101
+ }
97
102
  export function fitFooterHint(cols, ...candidates) {
98
- return (candidates.find((candidate) => visualWidth(space.indent + candidate) <= cols) ??
103
+ return (candidates.find((candidate) => visualWidth(space.indent + candidate) <= bodyEdge(cols)) ??
99
104
  candidates[candidates.length - 1] ??
100
105
  '');
101
106
  }
102
107
  export function captureFooterHint(cols = Number.POSITIVE_INFINITY) {
103
108
  const trans = t();
104
109
  const dot = pickIcon('·', '-');
105
- return fitFooterHint(cols, `Ctrl+C ${trans.common.exit} ${dot} Esc ${trans.common.back} ${dot} Enter ${trans.common.confirm}`, 'Ctrl+C Esc Enter', 'Ctrl+C Esc', 'Ctrl+C');
110
+ return fitFooterHint(cols, `${glyph.enter()} ${trans.common.confirm} ${dot} Esc ${trans.common.back} ${dot} Ctrl+C ${trans.common.exit}`, `${glyph.enter()} Esc Ctrl+C`, 'Ctrl+C Esc', 'Ctrl+C');
106
111
  }
107
112
  export function digitTabHint(tabCount) {
108
113
  const dot = pickIcon('·', '-');
109
114
  return tabCount > 1 ? `1-${tabCount} / Tab ${t().menu.hintTabs} ${dot} ` : '';
110
115
  }
111
- export function passiveFooterHint(tabCount, cols = Number.POSITIVE_INFINITY) {
116
+ export function passiveFooterHint(tabCount, cols = Number.POSITIVE_INFINITY, canGoBack = true) {
112
117
  const trans = t();
113
118
  const dot = pickIcon('·', '-');
114
119
  const compactTabs = tabCount > 1 ? `1-${tabCount}/Tab ${dot} ` : '';
115
- return fitFooterHint(cols, `${digitTabHint(tabCount)}Esc ${trans.menu.hintBack} ${dot} q ${trans.menu.hintQuit} ${dot} ${trans.help.hint}`, `${digitTabHint(tabCount)}Esc ${trans.menu.hintBack} ${dot} q ${trans.menu.hintQuit}`, `${compactTabs}Esc ${dot} q ${dot} ?`, `Esc ${dot} q ${dot} ?`, `Esc ${dot} q`, 'q');
120
+ const back = canGoBack ? `Esc ${trans.menu.hintBack} ${dot} ` : '';
121
+ const backKey = canGoBack ? `Esc ${dot} ` : '';
122
+ return fitFooterHint(cols, `${digitTabHint(tabCount)}${back}q ${trans.menu.hintQuit} ${dot} ${trans.help.hint}`, `${digitTabHint(tabCount)}${back}q ${trans.menu.hintQuit}`, `${compactTabs}${backKey}q ${dot} ?`, `${backKey}q ${dot} ?`, `${backKey}q`, 'q');
116
123
  }
117
124
  function interactiveFooterHint(tabCount, cols) {
118
125
  const trans = t();
119
126
  const dot = pickIcon('·', '-');
120
- const fullTabs = digitTabHint(tabCount);
121
127
  const compactTabs = tabCount > 1 ? `1-${tabCount}/Tab ${dot} ` : '';
122
- const localLabelled = `${trans.menu.hintMove} ${dot} ${trans.menu.hintOpen} ${dot} Esc ${trans.menu.hintBack} ${dot} q ${trans.menu.hintQuit}`;
123
- const localFull = `${trans.menu.hintMove} ${dot} ${trans.menu.hintOpen} ${dot} Esc ${dot} q ${trans.menu.hintQuit}`;
124
- const localCompact = `${trans.menu.hintMove} ${trans.menu.hintOpen} Esc q`;
125
- const candidates = [
126
- `${fullTabs}${localLabelled} ${dot} ${trans.help.hint}`,
127
- `${fullTabs}${localLabelled}`,
128
- `${compactTabs}${localFull}`,
129
- localFull,
130
- `${compactTabs}${localCompact}`,
131
- localCompact,
132
- `${trans.menu.hintOpen} Esc q`,
133
- `Esc ${dot} q`,
134
- 'q',
135
- ];
136
- return fitFooterHint(cols, ...candidates);
128
+ const move = `${glyph.updown()} ${trans.menu.hintMove} ${dot} ${glyph.enter()} ${trans.menu.hintOpen}`;
129
+ const quit = `q ${trans.menu.hintQuit}`;
130
+ const local = `${move} ${dot} Esc ${trans.menu.hintBack} ${dot} ${quit}`;
131
+ const keys = `${glyph.updown()} ${glyph.enter()} Esc q`;
132
+ return fitFooterHint(cols, `${digitTabHint(tabCount)}${local} ${dot} ${trans.help.hint}`, `${digitTabHint(tabCount)}${local}`, `${compactTabs}${local}`, local, `${move} ${dot} Esc ${dot} ${quit}`, `${compactTabs}${keys}`, keys, `Esc ${dot} q`, 'q');
137
133
  }
138
134
  export function renderFooter(_active, cols, tabCount, overrideHint, lineCount = FOOTER_LINES, position) {
139
135
  if (lineCount === 0)
@@ -149,10 +145,9 @@ export function renderFooter(_active, cols, tabCount, overrideHint, lineCount =
149
145
  function withPosition(hint, hintText, indent, position, cols) {
150
146
  if (position === undefined)
151
147
  return hint;
152
- const frame = Math.min(cols, MAX_FRAME_COLS);
153
- const margin = visualWidth(space.indent) < frame ? space.indent : '';
154
- const gap = frame - visualWidth(indent + hintText) - visualWidth(position) - visualWidth(margin);
148
+ const edge = bodyEdge(cols);
149
+ const gap = edge - visualWidth(indent + hintText) - visualWidth(position);
155
150
  if (gap < 2)
156
151
  return hint;
157
- return hint + ' '.repeat(gap) + type.hint(position) + margin;
152
+ return hint + ' '.repeat(gap) + type.hint(position) + ' '.repeat(cols - edge);
158
153
  }
@@ -1,5 +1,5 @@
1
1
  import { renderMenu, renderMenuOption, menuColumns, nextIndex, parseKey, } from '../../core/components/menu.js';
2
- import { glyph, space, type } from '../../core/theme.js';
2
+ import { bodyEdge, glyph, space, type } from '../../core/theme.js';
3
3
  import { pickIcon } from '../../core/icons.js';
4
4
  import { t, fmt } from '../../i18n/index.js';
5
5
  import { visualWidth, wrapAnsiToVisualWidth } from '../../core/text.js';
@@ -9,7 +9,7 @@ export function computeMaxVisible(bodyRows) {
9
9
  function renderIndentedOutput(value, cols, preferredIndent = space.indent) {
10
10
  const width = Number.isFinite(cols) ? Math.max(1, Math.floor(cols)) : Number.POSITIVE_INFINITY;
11
11
  const indent = [preferredIndent, space.indent].find((i) => visualWidth(i) < width) ?? '';
12
- const contentWidth = Math.max(1, width - visualWidth(indent));
12
+ const contentWidth = Math.max(1, bodyEdge(width) - visualWidth(indent));
13
13
  return wrapAnsiToVisualWidth(value, contentWidth).map((line) => `${indent}${line}`);
14
14
  }
15
15
  export class ListField {
package/dist/app/frame.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ansi } from '../core/canvas.js';
2
+ import { frameLeft, frameWidth } from '../core/theme.js';
2
3
  import { clipAnsiToVisualWidth, visualWidth } from '../core/text.js';
3
4
  export function clipToWidth(line, cols) {
4
5
  if (visualWidth(line) <= cols)
@@ -19,11 +20,14 @@ export function fitBody(lines, height, scroll, cols) {
19
20
  return out;
20
21
  }
21
22
  export function composeFrameLines(header, body, footer, rows, cols, scroll) {
22
- const h = header.map((l) => fitLine(l, cols));
23
- const f = footer.map((l) => fitLine(l, cols));
23
+ const width = frameWidth(cols);
24
+ const left = ' '.repeat(frameLeft(cols));
25
+ const right = ' '.repeat(cols - width - left.length);
26
+ const h = header.map((l) => fitLine(l, width));
27
+ const f = footer.map((l) => fitLine(l, width));
24
28
  const bodyH = Math.max(0, rows - h.length - f.length);
25
- const b = fitBody(body, bodyH, scroll, cols);
26
- return [...h, ...b, ...f].slice(0, rows);
29
+ const b = fitBody(body, bodyH, scroll, width);
30
+ return [...h, ...b, ...f].slice(0, rows).map((line) => left + line + right);
27
31
  }
28
32
  export function diffFrame(prev, next) {
29
33
  if (prev?.length !== next.length)
package/dist/app/help.js CHANGED
@@ -1,11 +1,11 @@
1
- import { type, space, glyph } from '../core/theme.js';
1
+ import { type, space, glyph, bodyEdge } from '../core/theme.js';
2
2
  import { pickIcon } from '../core/icons.js';
3
3
  import { t } from '../i18n/index.js';
4
4
  import { padEndV, visualWidth, wrapAnsiWithIndent } from '../core/text.js';
5
5
  function row(shortcut, keyWidth, cols) {
6
6
  const key = padEndV(type.label(shortcut.key), keyWidth);
7
7
  const line = `${space.indent}${space.indent}${key} ${type.hint(shortcut.label)}`;
8
- if (visualWidth(line) <= cols)
8
+ if (visualWidth(line) <= bodyEdge(cols))
9
9
  return [line];
10
10
  return wrapAnsiWithIndent(`${type.label(shortcut.key)} ${type.hint(shortcut.label)}`, cols, space.indent + space.indent);
11
11
  }
@@ -1,13 +1,14 @@
1
- import { type, space } from '../../core/theme.js';
2
- import { t } from '../../i18n/index.js';
1
+ import { type, space, bodyEdge } from '../../core/theme.js';
2
+ import { fmt, t } from '../../i18n/index.js';
3
3
  import { renderListFieldWithContext } from '../fields/list-field.js';
4
+ import { offlineNotice } from '../chrome.js';
4
5
  import { visualWidth, wrapAnsiWithIndent } from '../../core/text.js';
5
6
  import { loadingLines } from '../../core/components/spinner.js';
6
7
  function hintLines(label, cols) {
7
8
  return wrapAnsiWithIndent(type.hint(label), cols, space.indent);
8
9
  }
9
10
  function renderReader(render, cols) {
10
- const contentWidth = Math.max(1, Math.min(80, cols - visualWidth(space.indent)));
11
+ const contentWidth = Math.max(1, Math.min(80, bodyEdge(cols) - visualWidth(space.indent)));
11
12
  return render(contentWidth).map((line) => (line ? `${space.indent}${line}` : ''));
12
13
  }
13
14
  function listFieldForState(state) {
@@ -25,6 +26,7 @@ function listFieldForState(state) {
25
26
  case 'reader':
26
27
  return state.readerLinksField;
27
28
  case 'error':
29
+ return state.errorField;
28
30
  case 'loading':
29
31
  case 'readerLoading':
30
32
  case 'search':
@@ -39,25 +41,34 @@ export function renderDocs(state, cols = 80, bodyRows = Number.POSITIVE_INFINITY
39
41
  case 'loading':
40
42
  lines = loadingLines(trans.common.loading, cols);
41
43
  break;
42
- case 'sections':
44
+ case 'sections': {
45
+ const context = state.stale ? offlineNotice(cols) : [];
46
+ if (Number.isFinite(bodyRows)) {
47
+ state.sectionsField?.setMaxVisible(Math.max(1, Math.floor(bodyRows) - context.length - 1));
48
+ }
43
49
  lines = state.sectionsField
44
- ? renderListFieldWithContext(state.stale ? [...hintLines(trans.docs.stale, cols), ''] : [], state.sectionsField, bodyRows, cols)
50
+ ? renderListFieldWithContext(context, state.sectionsField, bodyRows, cols)
45
51
  : [];
46
52
  break;
53
+ }
47
54
  case 'files':
48
- lines = state.filesField?.render(bodyRows, cols) ?? [];
55
+ lines = state.filesField?.render(bodyRows, cols) ?? loadingLines(trans.common.loading, cols);
49
56
  break;
50
57
  case 'archivedGroups':
51
58
  lines = state.archivedGroupsField?.render(bodyRows, cols) ?? [];
52
59
  break;
53
60
  case 'archivedFiles':
54
- lines = state.archivedFilesField?.render(bodyRows, cols) ?? [];
61
+ lines =
62
+ state.archivedFilesField?.render(bodyRows, cols) ??
63
+ loadingLines(trans.common.loading, cols);
55
64
  break;
56
65
  case 'search':
57
66
  lines = state.searchField?.render(cols) ?? [];
58
67
  break;
59
68
  case 'searchLoading':
60
- lines = loadingLines(trans.docs.searching, cols);
69
+ lines = loadingLines(state.searchProgress
70
+ ? fmt(trans.docs.searchProgress, state.searchProgress)
71
+ : trans.docs.searching, cols);
61
72
  break;
62
73
  case 'searchResults':
63
74
  lines = state.searchResultsField
@@ -76,8 +87,16 @@ export function renderDocs(state, cols = 80, bodyRows = Number.POSITIVE_INFINITY
76
87
  ? state.readerLinksField.render(bodyRows, cols)
77
88
  : renderReader(state.readerRender ?? (() => []), cols);
78
89
  break;
79
- case 'error':
80
- return hintLines(state.errorMessage ?? trans.docs.loadError, cols);
90
+ case 'error': {
91
+ const context = [
92
+ ...wrapAnsiWithIndent(type.body(state.errorMessage ?? trans.docs.loadError), cols, space.indent),
93
+ ...hintLines(trans.docs.offlineHint, cols),
94
+ '',
95
+ ];
96
+ return state.errorField
97
+ ? renderListFieldWithContext(context, state.errorField, bodyRows, cols)
98
+ : context;
99
+ }
81
100
  }
82
101
  if (!state.errorMessage)
83
102
  return lines;