@lewishowles/cli-style 0.8.1 → 0.10.0

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/src/index.js CHANGED
@@ -1,16 +1,25 @@
1
1
  export { createCliStyle, profiles } from "./create-cli-style.js";
2
2
 
3
3
  export { resolveProfile } from "./capability/resolve-profile.js";
4
- export { resolveTerminalCapabilities } from "./capability/terminal.js";
4
+ export { resolveTerminalCapabilities, resolveTheme } from "./capability/terminal.js";
5
5
  export { isProfile } from "./profiles/profiles.js";
6
6
 
7
7
  export { renderHelp } from "./cli/help.js";
8
+ export { parseCatalogueRequest, renderCatalogue } from "./cli/catalogue-command.js";
9
+ export {
10
+ getRendererMetadata,
11
+ rendererCatalogue,
12
+ rendererNames,
13
+ } from "./catalogue/renderer-catalogue.js";
14
+
8
15
  export {
9
16
  adapterNames,
10
17
  parseAdapterPathRequest,
11
18
  renderAdapterPath,
12
19
  } from "./cli/adapter-path-command.js";
13
- export { parseRenderRequest, rendererNames, renderJsonInput } from "./cli/render-command.js";
20
+
21
+ export { parseRenderRequest, renderJsonInput } from "./cli/render-command.js";
22
+
14
23
  export {
15
24
  galleryFixtures,
16
25
  gallerySections,
@@ -26,6 +35,7 @@ export { commandResult } from "./patterns/command-result.js";
26
35
  export { compactDataTable } from "./patterns/compact-data-table.js";
27
36
  export { confirmationResult } from "./patterns/confirmation-result.js";
28
37
  export { diagnosticReport } from "./patterns/diagnostic-report.js";
38
+ export { diffBlock } from "./patterns/diff-block.js";
29
39
  export { nextStepBlock } from "./patterns/next-step-block.js";
30
40
  export { taskSummary } from "./patterns/task-summary.js";
31
41
  export { barChart } from "./primitives/bar-chart.js";
@@ -35,10 +45,13 @@ export { emptyState, errorBlock, hint } from "./primitives/feedback.js";
35
45
  export { panel } from "./primitives/panel.js";
36
46
  export { progressBar } from "./primitives/progress-bar.js";
37
47
  export { row } from "./primitives/row.js";
48
+ export { rowGroup } from "./primitives/row-group.js";
49
+ export { sparkline } from "./primitives/sparkline.js";
38
50
  export { span } from "./primitives/span.js";
39
51
  export { status } from "./primitives/status.js";
40
52
  export { step, stepProgress, stepStates } from "./primitives/step-progress.js";
41
53
  export { table } from "./primitives/table.js";
54
+
42
55
  export {
43
56
  createReporter,
44
57
  renderGroup,
@@ -46,10 +59,12 @@ export {
46
59
  renderReporterStatus,
47
60
  renderSection,
48
61
  } from "./reporters/create-reporter.js";
62
+
49
63
  export {
50
64
  chartColours,
51
65
  chipColours,
52
66
  colourTokens,
67
+ diffColours,
53
68
  getColourToken,
54
69
  getToneColour,
55
70
  panelColours,
@@ -57,8 +72,10 @@ export {
57
72
  resolveColourValue,
58
73
  tableColours,
59
74
  terminalColours,
75
+ themes,
60
76
  toneColours,
61
77
  } from "./theme/colours.js";
78
+
62
79
  export {
63
80
  getHighestSeverityResult,
64
81
  getResultSymbol,
@@ -0,0 +1,333 @@
1
+ import { background, foreground, stripAnsi } from "../formatters/ansi.js";
2
+ import { profiles } from "../profiles/profiles.js";
3
+ import { diffColours } from "../theme/colours.js";
4
+ import { isNonEmptyString, isRecord, renderTitle } from "./helpers.js";
5
+
6
+ // Diff line types map stable prefixes and semantic tones for text profiles.
7
+ const diffLineTypes = {
8
+ added: {
9
+ prefix: "+ ",
10
+ tone: "success",
11
+ },
12
+ context: {
13
+ prefix: " ",
14
+ tone: "muted",
15
+ },
16
+ header: {
17
+ prefix: "@@ ",
18
+ tone: "info",
19
+ },
20
+ removed: {
21
+ prefix: "- ",
22
+ tone: "danger",
23
+ },
24
+ };
25
+
26
+ // Full-row fills apply only to colour-enabled terminal text profiles.
27
+ const backgroundProfiles = new Set([profiles.DIAGNOSTIC, profiles.HUMAN]);
28
+
29
+ /**
30
+ * Render a structured diff block for human and agent output.
31
+ *
32
+ * @param {object} diff
33
+ * Structured diff input.
34
+ * @param {object[]} diff.lines
35
+ * Structured diff lines.
36
+ * @param {number} diff.lines[].oldLineNumber
37
+ * Optional caller-supplied old line number.
38
+ * @param {number} diff.lines[].newLineNumber
39
+ * Optional caller-supplied new line number.
40
+ * @param {string} diff.path
41
+ * Optional affected path.
42
+ * @param {string} diff.title
43
+ * Optional block title.
44
+ * @param {object} options
45
+ * Rendering options.
46
+ * @returns {object|string}
47
+ * Structured JSON input or rendered diff block.
48
+ */
49
+ export function diffBlock(diff, options = {}) {
50
+ if (options.profile === profiles.JSON) {
51
+ return diff;
52
+ }
53
+
54
+ if (!isRecord(diff)) {
55
+ return "";
56
+ }
57
+
58
+ const lines = normaliseLines(diff.lines);
59
+
60
+ if (lines.length === 0) {
61
+ return "";
62
+ }
63
+
64
+ const gutterWidth = getGutterWidth(lines);
65
+ const renderedLines = lines.map((line) => renderLine(line, options, gutterWidth));
66
+ const summary = gutterWidth === undefined ? "" : renderChangeSummary(lines, options);
67
+ const metadata = renderMetadata(diff, options, summary);
68
+
69
+ if (options.profile === profiles.AGENT) {
70
+ const codeFence = ["```diff", renderedLines.join("\n"), "```"].join("\n");
71
+
72
+ return [...metadata, codeFence].join("\n\n");
73
+ }
74
+
75
+ return [...metadata, renderedLines.join("\n")].filter((section) => section !== "").join("\n\n");
76
+ }
77
+
78
+ /**
79
+ * Return structured lines with supported types and text.
80
+ *
81
+ * @param {*} lines
82
+ * Candidate diff lines.
83
+ * @returns {object[]}
84
+ * Valid diff lines.
85
+ */
86
+ function normaliseLines(lines) {
87
+ if (!Array.isArray(lines)) {
88
+ return [];
89
+ }
90
+
91
+ return lines.filter(isValidLine).map((line) => ({
92
+ newLineNumber: normaliseLineNumber(line.newLineNumber),
93
+ oldLineNumber: normaliseLineNumber(line.oldLineNumber),
94
+ text: stripAnsi(line.text),
95
+ type: line.type,
96
+ }));
97
+ }
98
+
99
+ /**
100
+ * Return a valid positive line number or undefined.
101
+ *
102
+ * @param {*} value
103
+ * Candidate line number.
104
+ * @returns {number|undefined}
105
+ * Normalised line number.
106
+ */
107
+ function normaliseLineNumber(value) {
108
+ return Number.isInteger(value) && value > 0 ? value : undefined;
109
+ }
110
+
111
+ /**
112
+ * Return the stable gutter width for a fully numbered block.
113
+ *
114
+ * @param {object[]} lines
115
+ * Normalised diff lines.
116
+ * @returns {number|undefined}
117
+ * Widest line-number width, or undefined when gutters are unavailable.
118
+ */
119
+ function getGutterWidth(lines) {
120
+ const codeLines = lines.filter((line) => line.type !== "header");
121
+
122
+ if (codeLines.length === 0 || !codeLines.every(hasRequiredLineNumbers)) {
123
+ return undefined;
124
+ }
125
+
126
+ return Math.max(
127
+ ...codeLines
128
+ .flatMap(getVisibleLineNumbers)
129
+ .filter((lineNumber) => lineNumber !== undefined)
130
+ .map((lineNumber) => String(lineNumber).length),
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Return line numbers visible for one diff type.
136
+ *
137
+ * @param {object} line
138
+ * Normalised diff line.
139
+ * @returns {number[]}
140
+ * Visible old and new line numbers.
141
+ */
142
+ function getVisibleLineNumbers(line) {
143
+ if (line.type === "added") {
144
+ return [line.newLineNumber];
145
+ }
146
+
147
+ if (line.type === "removed") {
148
+ return [line.oldLineNumber];
149
+ }
150
+
151
+ return [line.oldLineNumber, line.newLineNumber];
152
+ }
153
+
154
+ /**
155
+ * Check whether a line has the numbers required by its diff type.
156
+ *
157
+ * @param {object} line
158
+ * Normalised diff line.
159
+ * @returns {boolean}
160
+ * Whether the line can participate in a numbered gutter.
161
+ */
162
+ function hasRequiredLineNumbers(line) {
163
+ if (line.type === "added") {
164
+ return line.newLineNumber !== undefined;
165
+ }
166
+
167
+ if (line.type === "removed") {
168
+ return line.oldLineNumber !== undefined;
169
+ }
170
+
171
+ return line.oldLineNumber !== undefined && line.newLineNumber !== undefined;
172
+ }
173
+
174
+ /**
175
+ * Check whether a candidate is a supported diff line.
176
+ *
177
+ * @param {*} line
178
+ * Candidate diff line.
179
+ * @returns {boolean}
180
+ * Whether the line can be rendered safely.
181
+ */
182
+ function isValidLine(line) {
183
+ return isRecord(line) && Object.hasOwn(diffLineTypes, line.type) && typeof line.text === "string";
184
+ }
185
+
186
+ /**
187
+ * Render optional diff metadata for the active profile.
188
+ *
189
+ * @param {object} diff
190
+ * Structured diff input.
191
+ * @param {object} options
192
+ * Rendering options.
193
+ * @param {string} summary
194
+ * Optional added and removed line count.
195
+ * @returns {string[]}
196
+ * Rendered metadata sections.
197
+ */
198
+ function renderMetadata(diff, options, summary) {
199
+ const metadata = [];
200
+
201
+ if (isNonEmptyString(diff.title)) {
202
+ const title = stripAnsi(diff.title);
203
+
204
+ metadata.push(renderTitle(formatMetadataValue(title, summary), options));
205
+ }
206
+
207
+ if (isNonEmptyString(diff.path)) {
208
+ const path = stripAnsi(diff.path);
209
+
210
+ metadata.push(`Path: ${formatMetadataValue(path, summary, diff.title)}`);
211
+ } else if (!isNonEmptyString(diff.title) && summary !== "") {
212
+ metadata.push(summary);
213
+ }
214
+
215
+ return metadata;
216
+ }
217
+
218
+ /**
219
+ * Append a change summary to the first available metadata value.
220
+ *
221
+ * @param {string} value
222
+ * Metadata value.
223
+ * @param {string} summary
224
+ * Optional change summary.
225
+ * @param {*} title
226
+ * Title value used to avoid duplicating a summary on the path.
227
+ * @returns {string}
228
+ * Metadata value with optional summary.
229
+ */
230
+ function formatMetadataValue(value, summary, title) {
231
+ return summary !== "" && !isNonEmptyString(title) ? `${value} ${summary}` : value;
232
+ }
233
+
234
+ /**
235
+ * Count added and removed lines already present in the block.
236
+ *
237
+ * @param {object[]} lines
238
+ * Normalised diff lines.
239
+ * @param {object} options
240
+ * Rendering options.
241
+ * @returns {string}
242
+ * Optional change summary.
243
+ */
244
+ function renderChangeSummary(lines, options) {
245
+ const added = lines.filter((line) => line.type === "added").length;
246
+ const removed = lines.filter((line) => line.type === "removed").length;
247
+
248
+ if (added === 0 && removed === 0) {
249
+ return "";
250
+ }
251
+
252
+ const profile = options.profile ?? profiles.HUMAN;
253
+ const addedValue = `+${added}`;
254
+ const removedValue = `-${removed}`;
255
+
256
+ if (options.colour !== true || !backgroundProfiles.has(profile)) {
257
+ return `(${addedValue} ${removedValue})`;
258
+ }
259
+
260
+ return `(${foreground(addedValue, "success", options)} ${foreground(removedValue, "danger", options)})`;
261
+ }
262
+
263
+ /**
264
+ * Render one diff line with a stable semantic prefix.
265
+ *
266
+ * @param {object} line
267
+ * Normalised diff line.
268
+ * @param {object} options
269
+ * Rendering options.
270
+ * @param {number|undefined} gutterWidth
271
+ * Stable gutter width for the block.
272
+ * @returns {string}
273
+ * Rendered diff line.
274
+ */
275
+ function renderLine(line, options, gutterWidth) {
276
+ const lineType = diffLineTypes[line.type];
277
+ const gutter = gutterWidth === undefined ? "" : renderGutter(line, gutterWidth);
278
+ const lineValue = `${gutter}${lineType.prefix}${line.text}`;
279
+ const profile = options.profile ?? profiles.HUMAN;
280
+ const fill = diffColours[line.type];
281
+
282
+ if (options.colour === true && backgroundProfiles.has(profile) && fill !== undefined) {
283
+ return background(foreground(lineValue, fill.foreground, options), fill.background, options);
284
+ }
285
+
286
+ const prefix = foreground(lineType.prefix, lineType.tone, options);
287
+
288
+ return `${gutter}${prefix}${line.text}`;
289
+ }
290
+
291
+ /**
292
+ * Render one stable old/new line-number gutter.
293
+ *
294
+ * @param {object} line
295
+ * Normalised diff line.
296
+ * @param {number} width
297
+ * Widest line-number width in the block.
298
+ * @returns {string}
299
+ * Rendered gutter.
300
+ */
301
+ function renderGutter(line, width) {
302
+ const blank = " ".repeat(width);
303
+ const oldNumber = formatLineNumber(line.oldLineNumber, width);
304
+ const newNumber = formatLineNumber(line.newLineNumber, width);
305
+
306
+ if (line.type === "added") {
307
+ return `${blank} ${newNumber} `;
308
+ }
309
+
310
+ if (line.type === "removed") {
311
+ return `${oldNumber} ${blank} `;
312
+ }
313
+
314
+ if (line.type === "context" && line.oldLineNumber === line.newLineNumber) {
315
+ return `${oldNumber} ${blank} `;
316
+ }
317
+
318
+ return `${oldNumber} ${newNumber} `;
319
+ }
320
+
321
+ /**
322
+ * Pad one line number to the block gutter width.
323
+ *
324
+ * @param {number|undefined} value
325
+ * Line number to format.
326
+ * @param {number} width
327
+ * Gutter width.
328
+ * @returns {string}
329
+ * Right-aligned number or blank placeholder.
330
+ */
331
+ function formatLineNumber(value, width) {
332
+ return value === undefined ? " ".repeat(width) : String(value).padStart(width, " ");
333
+ }
@@ -21,7 +21,7 @@ const fallbackTone = "neutral";
21
21
  export function chip(label, tone = fallbackTone, options = {}) {
22
22
  const token = chipColours[tone] ?? chipColours[fallbackTone];
23
23
 
24
- if (options.colour !== true) {
24
+ if (options.colour !== true || options.theme === "auto") {
25
25
  return `[${label}]`;
26
26
  }
27
27
 
@@ -72,7 +72,12 @@ function renderLine(line, width, isTitle, options) {
72
72
  }
73
73
 
74
74
  const renderedAccent = renderPanelColour(accent, options);
75
- const renderedContent = background(content, panelColours.background, options);
75
+
76
+ const renderedContent = foreground(
77
+ background(content, panelColours.background, options),
78
+ panelColours.body,
79
+ options,
80
+ );
76
81
 
77
82
  return `${renderedAccent}${isTitle ? style(renderedContent, "bold", options) : renderedContent}`;
78
83
  }
@@ -0,0 +1,84 @@
1
+ import { getResultSymbol } from "../theme/results.js";
2
+ import { row } from "./row.js";
3
+
4
+ /**
5
+ * Render related labelled values against a shared label width.
6
+ *
7
+ * @param {object} options
8
+ * Rendering options.
9
+ * @param {number} options.labelWidth
10
+ * Optional minimum label width.
11
+ * @param {object[]} options.rows
12
+ * Labelled values to render.
13
+ * @returns {string}
14
+ * Rendered row group.
15
+ */
16
+ export function rowGroup(options = {}) {
17
+ const rows = normaliseRows(options.rows);
18
+
19
+ if (rows.length === 0) {
20
+ return "";
21
+ }
22
+
23
+ const labelWidth = Math.max(
24
+ ...rows.map((entry) => displayedLabelLength(entry, options)),
25
+ options.labelWidth ?? 0,
26
+ );
27
+
28
+ return rows
29
+ .map((entry) =>
30
+ row(entry.label, entry.value ?? "", {
31
+ ...options,
32
+ ...entry,
33
+ labelWidth,
34
+ }),
35
+ )
36
+ .join("\n");
37
+ }
38
+
39
+ /**
40
+ * Return rows with usable labels.
41
+ *
42
+ * @param {object[]} rows
43
+ * Requested row entries.
44
+ * @returns {object[]}
45
+ * Valid row entries.
46
+ */
47
+ function normaliseRows(rows) {
48
+ if (!Array.isArray(rows)) {
49
+ return [];
50
+ }
51
+
52
+ return rows.filter(
53
+ (entry) =>
54
+ entry !== null &&
55
+ typeof entry === "object" &&
56
+ typeof entry.label === "string" &&
57
+ entry.label !== "",
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Return the visible width of a row label.
63
+ *
64
+ * @param {object} entry
65
+ * Row entry.
66
+ * @param {object} options
67
+ * Group rendering options.
68
+ * @returns {number}
69
+ * Displayed label length.
70
+ */
71
+ function displayedLabelLength(entry, options) {
72
+ if (typeof entry.result !== "string" || entry.result === "") {
73
+ return entry.label.length;
74
+ }
75
+
76
+ const entryOptions = {
77
+ ...options,
78
+ ...entry,
79
+ };
80
+
81
+ const symbol = getResultSymbol(entry.result, entryOptions);
82
+
83
+ return [symbol, entry.label].join(" ").length;
84
+ }