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