toolcraft-openapi 0.0.232 → 0.0.233

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 (84) hide show
  1. package/dist/composition.json +2 -2
  2. package/dist/define-client.js +4 -64
  3. package/dist/generate.d.ts +10 -0
  4. package/dist/generate.js +5 -5
  5. package/dist/http.js +132 -61
  6. package/dist/network-error.d.ts +1 -0
  7. package/dist/network-error.js +11 -10
  8. package/dist/redaction.js +2 -1
  9. package/dist/runtime.d.ts +1 -0
  10. package/dist/runtime.js +73 -5
  11. package/dist/spec-cache.d.ts +1 -0
  12. package/dist/spec-cache.js +14 -2
  13. package/dist/spec-source.d.ts +2 -0
  14. package/dist/spec-source.js +15 -10
  15. package/node_modules/toolcraft-design/dist/command-registry.d.ts +11 -0
  16. package/node_modules/toolcraft-design/dist/command-registry.js +24 -0
  17. package/node_modules/toolcraft-design/dist/dashboard/ansi.d.ts +2 -0
  18. package/node_modules/toolcraft-design/dist/dashboard/ansi.js +81 -27
  19. package/node_modules/toolcraft-design/dist/dashboard/buffer.js +39 -25
  20. package/node_modules/toolcraft-design/dist/dashboard/components/border.js +6 -5
  21. package/node_modules/toolcraft-design/dist/dashboard/components/footer.js +26 -21
  22. package/node_modules/toolcraft-design/dist/dashboard/components/output-pane.d.ts +1 -1
  23. package/node_modules/toolcraft-design/dist/dashboard/components/output-pane.js +12 -6
  24. package/node_modules/toolcraft-design/dist/dashboard/components/stats-pane.d.ts +2 -1
  25. package/node_modules/toolcraft-design/dist/dashboard/components/stats-pane.js +51 -10
  26. package/node_modules/toolcraft-design/dist/dashboard/dashboard.d.ts +4 -0
  27. package/node_modules/toolcraft-design/dist/dashboard/dashboard.js +77 -4
  28. package/node_modules/toolcraft-design/dist/dashboard/index.d.ts +2 -0
  29. package/node_modules/toolcraft-design/dist/dashboard/index.js +2 -0
  30. package/node_modules/toolcraft-design/dist/dashboard/keymap.js +26 -6
  31. package/node_modules/toolcraft-design/dist/dashboard/layout.d.ts +1 -0
  32. package/node_modules/toolcraft-design/dist/dashboard/layout.js +19 -3
  33. package/node_modules/toolcraft-design/dist/dashboard/line-buffer.d.ts +9 -0
  34. package/node_modules/toolcraft-design/dist/dashboard/line-buffer.js +63 -0
  35. package/node_modules/toolcraft-design/dist/dashboard/output-preview.d.ts +9 -0
  36. package/node_modules/toolcraft-design/dist/dashboard/output-preview.js +70 -0
  37. package/node_modules/toolcraft-design/dist/dashboard/snapshot.js +3 -1
  38. package/node_modules/toolcraft-design/dist/dashboard/store.js +9 -3
  39. package/node_modules/toolcraft-design/dist/dashboard/terminal-strings.d.ts +6 -0
  40. package/node_modules/toolcraft-design/dist/dashboard/terminal-strings.js +108 -0
  41. package/node_modules/toolcraft-design/dist/dashboard/terminal-width.js +3 -1
  42. package/node_modules/toolcraft-design/dist/dashboard/terminal.js +11 -1
  43. package/node_modules/toolcraft-design/dist/dashboard/testing/pipeline-scenario.d.ts +1 -0
  44. package/node_modules/toolcraft-design/dist/dashboard/testing/pipeline-scenario.js +126 -0
  45. package/node_modules/toolcraft-design/dist/dashboard/types.d.ts +3 -1
  46. package/node_modules/toolcraft-design/dist/event-groups.d.ts +19 -0
  47. package/node_modules/toolcraft-design/dist/event-groups.js +49 -0
  48. package/node_modules/toolcraft-design/dist/index.d.ts +6 -1
  49. package/node_modules/toolcraft-design/dist/index.js +5 -1
  50. package/node_modules/toolcraft-design/dist/inline-notice.d.ts +14 -0
  51. package/node_modules/toolcraft-design/dist/inline-notice.js +28 -0
  52. package/node_modules/toolcraft-design/dist/metric.d.ts +8 -0
  53. package/node_modules/toolcraft-design/dist/metric.js +21 -0
  54. package/node_modules/toolcraft-design/dist/overlay-manager.d.ts +6 -0
  55. package/node_modules/toolcraft-design/dist/overlay-manager.js +10 -0
  56. package/node_modules/toolcraft-design/dist/primitives.d.ts +12 -0
  57. package/node_modules/toolcraft-design/dist/primitives.js +7 -0
  58. package/node_modules/toolcraft-design/dist/progress-group.d.ts +7 -0
  59. package/node_modules/toolcraft-design/dist/progress-group.js +10 -0
  60. package/node_modules/toolcraft-design/dist/render-performance.d.ts +33 -0
  61. package/node_modules/toolcraft-design/dist/render-performance.js +71 -0
  62. package/node_modules/toolcraft-design/dist/task-tree.d.ts +22 -0
  63. package/node_modules/toolcraft-design/dist/task-tree.js +79 -0
  64. package/node_modules/toolcraft-design/dist/viewport.d.ts +18 -0
  65. package/node_modules/toolcraft-design/dist/viewport.js +56 -0
  66. package/node_modules/toolcraft-schema/dist/clone-default.d.ts +1 -0
  67. package/node_modules/toolcraft-schema/dist/clone-default.js +26 -0
  68. package/node_modules/toolcraft-schema/dist/index.compile-check.js +1 -0
  69. package/node_modules/toolcraft-schema/dist/index.d.ts +18 -9
  70. package/node_modules/toolcraft-schema/dist/index.js +46 -8
  71. package/node_modules/toolcraft-schema/dist/json-schema/compiler.d.ts +1 -0
  72. package/node_modules/toolcraft-schema/dist/json-schema/compiler.js +6 -0
  73. package/node_modules/toolcraft-schema/dist/json-schema/evaluate.js +6 -3
  74. package/node_modules/toolcraft-schema/dist/json-schema/normalize-nullability.d.ts +1 -0
  75. package/node_modules/toolcraft-schema/dist/json-schema/normalize-nullability.js +69 -0
  76. package/node_modules/toolcraft-schema/dist/json-schema/types.d.ts +1 -0
  77. package/node_modules/toolcraft-schema/dist/json.d.ts +4 -1
  78. package/node_modules/toolcraft-schema/dist/json.js +57 -1
  79. package/node_modules/toolcraft-schema/dist/native-json-schema.d.ts +10 -0
  80. package/node_modules/toolcraft-schema/dist/native-json-schema.js +11 -0
  81. package/node_modules/toolcraft-schema/dist/validate.d.ts +5 -1
  82. package/node_modules/toolcraft-schema/dist/validate.js +61 -47
  83. package/node_modules/toolcraft-schema/package.json +1 -1
  84. package/package.json +4 -4
@@ -25,6 +25,7 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
25
25
  }
26
26
  return {
27
27
  sourceText: result.sourceText,
28
+ sourceUrl: result.sourceUrl,
28
29
  document: parseOpenApiDocument(result.sourceText, inputUrl)
29
30
  };
30
31
  }
@@ -40,6 +41,7 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
40
41
  now - cached.entry.validatedAt < freshnessMs) {
41
42
  return {
42
43
  sourceText: cached.entry.sourceText,
44
+ sourceUrl: cached.entry.sourceUrl,
43
45
  document: cached.document
44
46
  };
45
47
  }
@@ -58,6 +60,7 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
58
60
  notifyFallback(options.cache, inputUrl, error);
59
61
  return {
60
62
  sourceText: cached.entry.sourceText,
63
+ sourceUrl: cached.entry.sourceUrl,
61
64
  document: cached.document
62
65
  };
63
66
  }
@@ -69,6 +72,7 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
69
72
  if (cached.entry.etag === undefined) {
70
73
  throw new UserError(`Failed to fetch ${JSON.stringify(inputUrl.toString())}: received 304 without a cached validator.`);
71
74
  }
75
+ const sourceUrl = result.sourceUrl ?? cached.entry.sourceUrl;
72
76
  const policy = result.cacheControl === undefined
73
77
  ? {
74
78
  store: true,
@@ -78,18 +82,21 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
78
82
  if (!policy.store) {
79
83
  return {
80
84
  sourceText: cached.entry.sourceText,
85
+ sourceUrl,
81
86
  document: cached.document,
82
87
  ...(writableFs === null ? {} : { commit: () => removeCacheEntry(cachePath, writableFs) })
83
88
  };
84
89
  }
85
90
  const entry = {
86
91
  ...cached.entry,
92
+ sourceUrl,
87
93
  validatedAt: now,
88
94
  maxAgeMs: policy.maxAgeMs,
89
95
  ...(result.etag === undefined ? {} : { etag: result.etag })
90
96
  };
91
97
  return {
92
98
  sourceText: cached.entry.sourceText,
99
+ sourceUrl,
93
100
  document: cached.document,
94
101
  ...(writableFs === null
95
102
  ? {}
@@ -101,19 +108,22 @@ export async function loadCachedOpenApiSource(inputUrl, options) {
101
108
  if (!policy.store) {
102
109
  return {
103
110
  sourceText: result.sourceText,
111
+ sourceUrl: result.sourceUrl,
104
112
  document,
105
113
  ...(writableFs === null ? {} : { commit: () => removeCacheEntry(cachePath, writableFs) })
106
114
  };
107
115
  }
108
116
  const entry = {
109
- version: 1,
117
+ version: 2,
110
118
  sourceText: result.sourceText,
119
+ sourceUrl: result.sourceUrl,
111
120
  validatedAt: now,
112
121
  maxAgeMs: policy.maxAgeMs,
113
122
  ...(result.etag === undefined ? {} : { etag: result.etag })
114
123
  };
115
124
  return {
116
125
  sourceText: result.sourceText,
126
+ sourceUrl: result.sourceUrl,
117
127
  document,
118
128
  ...(writableFs === null
119
129
  ? {}
@@ -333,9 +343,11 @@ function isCacheEntry(value) {
333
343
  }
334
344
  const entry = value;
335
345
  return (Object.hasOwn(entry, "version") &&
336
- entry.version === 1 &&
346
+ entry.version === 2 &&
337
347
  Object.hasOwn(entry, "sourceText") &&
338
348
  typeof entry.sourceText === "string" &&
349
+ Object.hasOwn(entry, "sourceUrl") &&
350
+ typeof entry.sourceUrl === "string" &&
339
351
  Object.hasOwn(entry, "validatedAt") &&
340
352
  typeof entry.validatedAt === "number" &&
341
353
  Number.isFinite(entry.validatedAt) &&
@@ -15,11 +15,13 @@ export interface OpenApiHttpSourceOptions {
15
15
  export type OpenApiHttpSourceResult = {
16
16
  status: "modified";
17
17
  sourceText: string;
18
+ sourceUrl: string;
18
19
  etag?: string;
19
20
  cacheControl?: string;
20
21
  age?: string;
21
22
  } | {
22
23
  status: "not-modified";
24
+ sourceUrl?: string;
23
25
  etag?: string;
24
26
  cacheControl?: string;
25
27
  age?: string;
@@ -1,7 +1,7 @@
1
1
  import path from "node:path";
2
2
  import { fileURLToPath } from "node:url";
3
3
  import { parse as parseYaml } from "yaml";
4
- import { UserError } from "toolcraft";
4
+ import { UserError, redactHttpBody } from "toolcraft";
5
5
  import { renderSourceSnippet } from "toolcraft/source-snippet";
6
6
  import { classifyNetworkError } from "./network-error.js";
7
7
  import { redactSensitiveQueryValues } from "./redaction.js";
@@ -34,7 +34,7 @@ export async function readOpenApiSourceText(input, services) {
34
34
  }
35
35
  const result = await fetchOpenApiHttpSource(inputUrl, services.fetch);
36
36
  if (result.status === "not-modified") {
37
- throw new UserError(`Failed to fetch ${JSON.stringify(inputUrl.toString())}: received 304 without a cached document.`);
37
+ throw new UserError(`Failed to fetch ${JSON.stringify(sourceLabel)}: received 304 without a cached document.`);
38
38
  }
39
39
  return result.sourceText;
40
40
  }
@@ -62,14 +62,17 @@ export async function fetchOpenApiHttpSource(inputUrl, fetch, options = {}) {
62
62
  if (response.status === 304) {
63
63
  return {
64
64
  status: "not-modified",
65
+ ...(response.url === "" ? {} : { sourceUrl: response.url }),
65
66
  ...readResponseCacheHeaders(response)
66
67
  };
67
68
  }
68
69
  if (!response.ok) {
69
70
  const contentType = response.headers.get("content-type") ?? "";
70
71
  const text = await response.text().catch(() => "");
71
- const snippet = text.length === 0 ? "" : `\n body: ${truncate(text, 500)}`;
72
- throw new OpenApiHttpStatusError(`Failed to fetch ${JSON.stringify(url)}: ` +
72
+ const body = redactHttpBody(text);
73
+ const bodyText = typeof body === "string" ? body : JSON.stringify(body);
74
+ const snippet = text.length === 0 ? "" : `\n body: ${truncate(bodyText, 500)}`;
75
+ throw new OpenApiHttpStatusError(`Failed to fetch ${JSON.stringify(redactSensitiveQueryValues(url))}: ` +
73
76
  `${response.status} ${response.statusText}` +
74
77
  (contentType ? ` (content-type: ${contentType})` : "") +
75
78
  snippet);
@@ -78,6 +81,7 @@ export async function fetchOpenApiHttpSource(inputUrl, fetch, options = {}) {
78
81
  return {
79
82
  status: "modified",
80
83
  sourceText: await response.text(),
84
+ sourceUrl: response.url || url,
81
85
  ...readResponseCacheHeaders(response)
82
86
  };
83
87
  }
@@ -153,7 +157,7 @@ export function parseOpenApiDocument(sourceText, input) {
153
157
  }
154
158
  catch {
155
159
  try {
156
- parsed = parseYaml(sourceText);
160
+ parsed = parseYaml(sourceText, { prettyErrors: false });
157
161
  }
158
162
  catch (error) {
159
163
  throw new UserError(`Failed to parse OpenAPI document ${JSON.stringify(formatSourceLabel(input))}: ${formatParseErrorMessage(error, sourceText, formatSourceLabel(input))}`);
@@ -165,6 +169,9 @@ export function parseOpenApiDocument(sourceText, input) {
165
169
  return parsed;
166
170
  }
167
171
  function tryParseUrl(value) {
172
+ if (path.parse(value).root.length > 0) {
173
+ return null;
174
+ }
168
175
  try {
169
176
  return new URL(value);
170
177
  }
@@ -173,7 +180,8 @@ function tryParseUrl(value) {
173
180
  }
174
181
  }
175
182
  function formatSourceLabel(source) {
176
- return source instanceof URL ? source.toString() : source;
183
+ const url = source instanceof URL ? source : tryParseUrl(source);
184
+ return url === null ? String(source) : redactSensitiveQueryValues(url.toString());
177
185
  }
178
186
  function getErrorMessage(error) {
179
187
  if (error instanceof Error) {
@@ -189,10 +197,7 @@ function formatParseErrorMessage(error, sourceText, filePath) {
189
197
  return message;
190
198
  }
191
199
  const positionText = `at line ${linePosition.line} column ${linePosition.column}`;
192
- const messageWithPosition = message.includes(positionText)
193
- ? message
194
- : `${message} (${positionText})`;
195
- return `${messageWithPosition}\n${renderSourceSnippet({
200
+ return `${message} (${positionText})\n${renderSourceSnippet({
196
201
  source: sourceText,
197
202
  line: linePosition.line,
198
203
  column: linePosition.column,
@@ -0,0 +1,11 @@
1
+ export interface DesignCommand {
2
+ id: string;
3
+ label: string;
4
+ keys: readonly string[];
5
+ enabled?: () => boolean;
6
+ run: () => void;
7
+ }
8
+ export declare function createCommandRegistry(commands: readonly DesignCommand[]): {
9
+ dispatch(key: string): boolean;
10
+ list(): DesignCommand[];
11
+ };
@@ -0,0 +1,24 @@
1
+ export function createCommandRegistry(commands) {
2
+ const bindings = new Map();
3
+ const ids = new Set();
4
+ for (const command of commands) {
5
+ if (ids.has(command.id))
6
+ throw new Error(`Duplicate command: ${command.id}`);
7
+ ids.add(command.id);
8
+ for (const key of command.keys) {
9
+ if (bindings.has(key))
10
+ throw new Error(`Duplicate binding: ${key}`);
11
+ bindings.set(key, command);
12
+ }
13
+ }
14
+ return {
15
+ dispatch(key) {
16
+ const command = bindings.get(key);
17
+ if (!command || command.enabled?.() === false)
18
+ return false;
19
+ command.run();
20
+ return true;
21
+ },
22
+ list() { return commands.filter(command => command.enabled?.() !== false); }
23
+ };
24
+ }
@@ -6,6 +6,8 @@ export interface StyledSegment {
6
6
  export interface StyledLine {
7
7
  segments: StyledSegment[];
8
8
  }
9
+ /** Visible text for a plain screen row; styles are supplied by the caller. */
10
+ export declare function plainTerminalText(text: string): string;
9
11
  export declare function hasAnsi(text: string): boolean;
10
12
  /**
11
13
  * Parse a block of text that may contain ANSI SGR escape codes into a sequence of
@@ -1,4 +1,16 @@
1
+ import { graphemes, graphemeWidth } from "./terminal-width.js";
2
+ import { createTerminalStringFilter } from "./terminal-strings.js";
1
3
  const ESC = "\u001b";
4
+ /** Visible text for a plain screen row; styles are supplied by the caller. */
5
+ export function plainTerminalText(text) {
6
+ for (let index = 0; index < text.length; index++) {
7
+ const code = text.charCodeAt(index);
8
+ if ((code < 0x20 && code !== 0x09) || (code >= 0x7f && code <= 0x9f)) {
9
+ return parseAnsi(text).map(line => line.segments.map(segment => segment.text).join("")).join(" ");
10
+ }
11
+ }
12
+ return text;
13
+ }
2
14
  export function hasAnsi(text) {
3
15
  return text.includes(ESC);
4
16
  }
@@ -11,12 +23,38 @@ export function hasAnsi(text) {
11
23
  * `baseStyle` is used as the initial style and as the restore target for SGR reset / default color.
12
24
  */
13
25
  export function parseAnsi(text, baseStyle) {
26
+ text = createTerminalStringFilter().push(text);
14
27
  const base = normalizeStyle(baseStyle);
15
28
  let style = { ...base };
16
29
  let concealed = false;
17
30
  const lines = [];
18
31
  let cells = [];
19
32
  let column = 0;
33
+ const clearCell = (position) => {
34
+ if (cells[position]?.ch === "" && position > 0) {
35
+ cells[position - 1] = { ch: " ", style: { ...style } };
36
+ }
37
+ if (cells[position + 1]?.ch === "") {
38
+ cells[position + 1] = { ch: " ", style: { ...style } };
39
+ }
40
+ cells[position] = undefined;
41
+ };
42
+ const writeGrapheme = (ch) => {
43
+ const width = graphemeWidth(ch);
44
+ if (width === 0) {
45
+ const previous = cells[column - 1]?.ch === "" ? column - 2 : column - 1;
46
+ if (!concealed && cells[previous])
47
+ cells[previous].ch += ch;
48
+ return;
49
+ }
50
+ clearCell(column);
51
+ if (width === 2)
52
+ clearCell(column + 1);
53
+ cells[column] = { ch: concealed ? " " : ch, style: { ...style } };
54
+ if (width === 2)
55
+ cells[column + 1] = { ch: concealed ? " " : "", style: { ...style } };
56
+ column += width;
57
+ };
20
58
  const finishLine = () => {
21
59
  lines.push({ segments: cellsToSegments(cells) });
22
60
  cells = [];
@@ -25,8 +63,8 @@ export function parseAnsi(text, baseStyle) {
25
63
  let index = 0;
26
64
  while (index < text.length) {
27
65
  const ch = text[index];
28
- if (ch === ESC && text[index + 1] === "[") {
29
- const paramsStart = index + 2;
66
+ if ((ch === ESC && text[index + 1] === "[") || ch === "\u009b") {
67
+ const paramsStart = index + (ch === ESC ? 2 : 1);
30
68
  let cursor = paramsStart;
31
69
  while (cursor < text.length && !isCsiFinalByte(text[cursor])) {
32
70
  cursor += 1;
@@ -42,18 +80,30 @@ export function parseAnsi(text, baseStyle) {
42
80
  style = sgr.style;
43
81
  concealed = sgr.concealed;
44
82
  }
45
- else if (finalByte === "K" && parseParams(params)[0] === 2) {
46
- cells = [];
83
+ else if (finalByte === "K") {
84
+ const mode = parseParams(params)[0];
85
+ if (mode === 0) {
86
+ clearCell(column);
87
+ cells.length = Math.min(cells.length, column);
88
+ }
89
+ else if (mode === 1) {
90
+ const end = Math.min(column, cells.length - 1);
91
+ for (let position = 0; position <= end; position += 1)
92
+ clearCell(position);
93
+ }
94
+ else if (mode === 2) {
95
+ cells = [];
96
+ }
47
97
  }
48
98
  index = cursor + 1;
49
99
  continue;
50
100
  }
101
+ if (ch === "\u0085") {
102
+ finishLine();
103
+ index += 1;
104
+ continue;
105
+ }
51
106
  if (ch === ESC) {
52
- const next = text[index + 1];
53
- if (next === "]" || next === "P" || next === "X" || next === "^" || next === "_") {
54
- index = skipStringTerminated(text, index + 2);
55
- continue;
56
- }
57
107
  index += 2;
58
108
  continue;
59
109
  }
@@ -72,14 +122,32 @@ export function parseAnsi(text, baseStyle) {
72
122
  index += 1;
73
123
  continue;
74
124
  }
125
+ if (ch === "\t") {
126
+ const spaces = 8 - (column % 8);
127
+ for (let offset = 0; offset < spaces; offset++) {
128
+ if (cells[column] === undefined)
129
+ writeGrapheme(" ");
130
+ else
131
+ column += 1;
132
+ }
133
+ index += 1;
134
+ continue;
135
+ }
75
136
  const code = ch.charCodeAt(0);
76
- if (code < 0x20 && ch !== "\t") {
137
+ if (code < 0x20 || (code >= 0x7f && code <= 0x9f)) {
77
138
  index += 1;
78
139
  continue;
79
140
  }
80
- cells[column] = { ch: concealed ? " " : ch, style: { ...style } };
81
- column += 1;
82
- index += 1;
141
+ let end = index + 1;
142
+ while (end < text.length) {
143
+ const next = text.charCodeAt(end);
144
+ if (next < 0x20 || (next >= 0x7f && next <= 0x9f))
145
+ break;
146
+ end += 1;
147
+ }
148
+ for (const grapheme of graphemes(text.slice(index, end)))
149
+ writeGrapheme(grapheme);
150
+ index = end;
83
151
  }
84
152
  finishLine();
85
153
  return lines;
@@ -102,20 +170,6 @@ function isCsiFinalByte(ch) {
102
170
  const code = ch.charCodeAt(0);
103
171
  return code >= 0x40 && code <= 0x7e;
104
172
  }
105
- function skipStringTerminated(text, start) {
106
- let index = start;
107
- while (index < text.length) {
108
- const ch = text[index];
109
- if (ch === "\u0007") {
110
- return index + 1;
111
- }
112
- if (ch === ESC && text[index + 1] === "\\") {
113
- return index + 2;
114
- }
115
- index += 1;
116
- }
117
- return index;
118
- }
119
173
  function parseParams(params) {
120
174
  if (params.length === 0) {
121
175
  return [0];
@@ -1,5 +1,6 @@
1
1
  import { color } from "../components/color.js";
2
2
  import { expandTabs, graphemes, graphemeWidth } from "./terminal-width.js";
3
+ import { parseAnsi } from "./ansi.js";
3
4
  const EMPTY_CELL = { ch: " ", style: {} };
4
5
  export class ScreenBuffer {
5
6
  _width;
@@ -22,17 +23,19 @@ export class ScreenBuffer {
22
23
  }
23
24
  const normalizedStyle = normalizeStyle(style);
24
25
  let offset = 0;
25
- for (const ch of graphemes(expandTabs(text, Math.max(0, x)))) {
26
- const targetX = x + offset;
27
- const width = graphemeWidth(ch);
28
- offset += width;
29
- if (!this.isInBoundsX(targetX)) {
30
- continue;
31
- }
32
- this._cells[this.index(targetX, y)] = { ch, style: normalizedStyle };
33
- for (let continuation = 1; continuation < width; continuation += 1) {
34
- if (this.isInBoundsX(targetX + continuation)) {
35
- this._cells[this.index(targetX + continuation, y)] = { ch: "", style: normalizedStyle };
26
+ for (const segment of screenSegments(text, normalizedStyle)) {
27
+ for (const ch of graphemes(expandTabs(segment.text, Math.max(0, x + offset)))) {
28
+ const targetX = x + offset;
29
+ const width = graphemeWidth(ch);
30
+ offset += width;
31
+ if (!this.isInBoundsX(targetX)) {
32
+ continue;
33
+ }
34
+ this._cells[this.index(targetX, y)] = { ch, style: segment.style };
35
+ for (let continuation = 1; continuation < width; continuation += 1) {
36
+ if (this.isInBoundsX(targetX + continuation)) {
37
+ this._cells[this.index(targetX + continuation, y)] = { ch: "", style: segment.style };
38
+ }
36
39
  }
37
40
  }
38
41
  }
@@ -84,20 +87,22 @@ export class ScreenBuffer {
84
87
  const normalizedStyle = normalizeStyle(style);
85
88
  const rectEndX = rect.x + rect.width;
86
89
  let offset = 0;
87
- for (const ch of graphemes(expandTabs(text))) {
88
- const targetX = rect.x + offset;
89
- const width = graphemeWidth(ch);
90
- offset += width;
91
- if (targetX + width > rectEndX) {
92
- break;
93
- }
94
- if (!this.isInBoundsX(targetX)) {
95
- continue;
96
- }
97
- this._cells[this.index(targetX, y)] = { ch, style: normalizedStyle };
98
- for (let continuation = 1; continuation < width; continuation += 1) {
99
- if (this.isInBoundsX(targetX + continuation)) {
100
- this._cells[this.index(targetX + continuation, y)] = { ch: "", style: normalizedStyle };
90
+ for (const segment of screenSegments(text, normalizedStyle)) {
91
+ for (const ch of graphemes(expandTabs(segment.text, offset))) {
92
+ const targetX = rect.x + offset;
93
+ const width = graphemeWidth(ch);
94
+ offset += width;
95
+ if (targetX + width > rectEndX) {
96
+ break;
97
+ }
98
+ if (!this.isInBoundsX(targetX)) {
99
+ continue;
100
+ }
101
+ this._cells[this.index(targetX, y)] = { ch, style: segment.style };
102
+ for (let continuation = 1; continuation < width; continuation += 1) {
103
+ if (this.isInBoundsX(targetX + continuation)) {
104
+ this._cells[this.index(targetX + continuation, y)] = { ch: "", style: segment.style };
105
+ }
101
106
  }
102
107
  }
103
108
  }
@@ -115,6 +120,15 @@ export class ScreenBuffer {
115
120
  return y >= 0 && y < this._height;
116
121
  }
117
122
  }
123
+ function screenSegments(text, style) {
124
+ for (let index = 0; index < text.length; index++) {
125
+ const code = text.charCodeAt(index);
126
+ if ((code < 0x20 && code !== 0x09) || (code >= 0x7f && code <= 0x9f)) {
127
+ return parseAnsi(text, style).flatMap((line, row) => row === 0 ? line.segments : [{ text: " ", style }, ...line.segments]);
128
+ }
129
+ }
130
+ return [{ text, style }];
131
+ }
118
132
  export function diff(prev, next) {
119
133
  const changes = [];
120
134
  const width = Math.max(prev.width, next.width);
@@ -1,3 +1,5 @@
1
+ import { plainTerminalText } from "../ansi.js";
2
+ import { displayWidth, truncateToWidth } from "../terminal-width.js";
1
3
  export function renderBorder(buffer, layout, opts) {
2
4
  const outer = layout.outerBorder;
3
5
  if (outer.width <= 0 || outer.height <= 0) {
@@ -100,17 +102,16 @@ function renderFooterDivider(buffer, left, right, bottom, layout, style) {
100
102
  }
101
103
  }
102
104
  function renderTopSegment(width, title) {
105
+ if (title !== undefined)
106
+ title = plainTerminalText(title);
103
107
  if (width <= 0) {
104
108
  return "";
105
109
  }
106
110
  if (!title) {
107
111
  return "─".repeat(width);
108
112
  }
109
- const content = `─ ${title} `;
110
- if (content.length >= width) {
111
- return content.slice(0, width);
112
- }
113
- return `${content}${"─".repeat(width - content.length)}`;
113
+ const content = truncateToWidth(`─ ${title} `, width);
114
+ return `${content}${"─".repeat(width - displayWidth(content))}`;
114
115
  }
115
116
  function hasInteriorRows(top, bottom) {
116
117
  return (bottom - top) > 1;
@@ -1,14 +1,32 @@
1
1
  import { getTheme } from "../../internal/theme-detect.js";
2
+ import { displayWidth, graphemes, graphemeWidth, truncateToWidth } from "../terminal-width.js";
3
+ import { plainTerminalText } from "../ansi.js";
2
4
  export function renderFooter(buffer, rect, hints) {
3
5
  buffer.clearRect(rect);
4
6
  if (rect.width <= 0 || rect.height <= 0 || hints.length === 0) {
5
7
  return;
6
8
  }
7
- const cells = truncateCells(hintsToCells(hints), rect.width);
8
- const startX = rect.x + Math.floor((rect.width - cells.length) / 2);
9
+ const fitted = [];
10
+ hints = hints.map(hint => ({ key: plainTerminalText(hint.key), label: plainTerminalText(hint.label) }));
11
+ let width = 0;
12
+ for (const hint of hints) {
13
+ const nextWidth = displayWidth(`${hint.key} ${hint.label}`) + (fitted.length > 0 ? 2 : 0);
14
+ if (width + nextWidth > rect.width)
15
+ break;
16
+ fitted.push(hint);
17
+ width += nextWidth;
18
+ }
19
+ if (fitted.length === 0) {
20
+ const key = truncateToWidth(hints[0].key, rect.width);
21
+ fitted.push({ key, label: "" });
22
+ width = displayWidth(key);
23
+ }
24
+ const cells = hintsToCells(fitted);
25
+ let x = rect.x + Math.floor((rect.width - width) / 2);
9
26
  const y = rect.y + Math.floor(rect.height / 2);
10
- cells.forEach((cell, index) => {
11
- buffer.put(startX + index, y, cell.ch, cell.style);
27
+ cells.forEach((cell) => {
28
+ buffer.put(x, y, cell.ch, cell.style);
29
+ x += graphemeWidth(cell.ch);
12
30
  });
13
31
  }
14
32
  export function defaultHints() {
@@ -27,30 +45,17 @@ function hintsToCells(hints) {
27
45
  if (hintIndex > 0) {
28
46
  cells.push({ ch: " ", style: {} }, { ch: " ", style: {} });
29
47
  }
30
- for (const ch of hint.key) {
48
+ for (const ch of graphemes(hint.key)) {
31
49
  cells.push({ ch, style: accentStyle });
32
50
  }
33
- cells.push({ ch: " ", style: {} });
34
- for (const ch of hint.label) {
51
+ if (hint.label.length > 0)
52
+ cells.push({ ch: " ", style: {} });
53
+ for (const ch of graphemes(hint.label)) {
35
54
  cells.push({ ch, style: {} });
36
55
  }
37
56
  });
38
57
  return cells;
39
58
  }
40
- function truncateCells(cells, width) {
41
- if (cells.length <= width) {
42
- return cells;
43
- }
44
- if (width <= 3) {
45
- return Array.from({ length: Math.max(0, width) }, () => ({ ch: ".", style: {} }));
46
- }
47
- return [
48
- ...cells.slice(0, width - 3),
49
- { ch: ".", style: {} },
50
- { ch: ".", style: {} },
51
- { ch: ".", style: {} }
52
- ];
53
- }
54
59
  function getAccentStyle() {
55
60
  return getTheme().styles.accent;
56
61
  }
@@ -8,5 +8,5 @@ export type VisualLine = {
8
8
  prefixStyle: CellStyle;
9
9
  segments?: StyledSegment[];
10
10
  };
11
- export declare function renderOutputPane(buffer: ScreenBuffer, rect: Rect, items: OutputItem[]): void;
11
+ export declare function renderOutputPane(buffer: ScreenBuffer, rect: Rect, items: OutputItem[], scrollOffset?: number): number;
12
12
  export declare function computeVisualLines(items: OutputItem[], width: number): VisualLine[];
@@ -1,15 +1,15 @@
1
+ import { selectViewportTail } from "../../viewport.js";
1
2
  import { getTheme } from "../../internal/theme-detect.js";
2
3
  import { hasAnsi, parseAnsi } from "../ansi.js";
3
4
  import { displayWidth, expandTabs, graphemes, graphemeWidth } from "../terminal-width.js";
4
5
  const TEXT_OFFSET = 3;
5
6
  const CONTINUATION_PREFIX = "│";
6
- export function renderOutputPane(buffer, rect, items) {
7
+ export function renderOutputPane(buffer, rect, items, scrollOffset = 0) {
7
8
  buffer.clearRect(rect);
8
9
  if (rect.width <= 0 || rect.height <= 0) {
9
- return;
10
+ return 0;
10
11
  }
11
- const visualLines = computeVisualLines(items, rect.width);
12
- const startLine = Math.max(visualLines.length - rect.height, 0);
12
+ const { rows: visualLines, offset: actualOffset } = selectViewportTail(items, rect.height, scrollOffset, item => computeVisualLines([item], rect.width));
13
13
  const textRect = {
14
14
  x: rect.x + TEXT_OFFSET,
15
15
  y: rect.y,
@@ -17,7 +17,7 @@ export function renderOutputPane(buffer, rect, items) {
17
17
  height: rect.height
18
18
  };
19
19
  for (let row = 0; row < rect.height; row += 1) {
20
- const line = visualLines[startLine + row];
20
+ const line = visualLines[row];
21
21
  if (line === undefined) {
22
22
  continue;
23
23
  }
@@ -44,6 +44,7 @@ export function renderOutputPane(buffer, rect, items) {
44
44
  }
45
45
  buffer.putInRect(textRect, row, line.text, line.style);
46
46
  }
47
+ return actualOffset;
47
48
  }
48
49
  export function computeVisualLines(items, width) {
49
50
  if (width <= 0) {
@@ -85,7 +86,12 @@ export function computeVisualLines(items, width) {
85
86
  return visualLines;
86
87
  }
87
88
  function hasCursorControls(text) {
88
- return text.includes("\r") || text.includes("\b");
89
+ for (let index = 0; index < text.length; index += 1) {
90
+ const code = text.charCodeAt(index);
91
+ if ((code < 0x20 && code !== 0x0a && code !== 0x09) || (code >= 0x7f && code <= 0x9f))
92
+ return true;
93
+ }
94
+ return false;
89
95
  }
90
96
  function hardWrapSegments(segments, width) {
91
97
  if (width <= 0) {
@@ -1,6 +1,7 @@
1
1
  import { ScreenBuffer } from "../buffer.js";
2
2
  import type { DashboardStats, Rect } from "../types.js";
3
- import type { VisualLine } from "./output-pane.js";
3
+ import { type VisualLine } from "./output-pane.js";
4
+ export declare function renderCompactStatsPane(buffer: ScreenBuffer, rect: Rect, stats: DashboardStats): void;
4
5
  export declare function renderStatsPane(buffer: ScreenBuffer, rect: Rect, stats: DashboardStats): void;
5
6
  export declare function formatElapsed(ms: number): string;
6
7
  export declare function formatNumber(n: number): string;