@lewishowles/cli-style 0.1.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/LICENSE +21 -0
- package/README.md +153 -0
- package/adapters/bash/cli-style.sh +50 -0
- package/adapters/python/cli_style.py +104 -0
- package/bin/cli-style.js +82 -0
- package/examples/diagnostic.mjs +30 -0
- package/examples/diagnostic.py +38 -0
- package/examples/diagnostic.sh +29 -0
- package/examples/gallery.mjs +18 -0
- package/package.json +42 -0
- package/src/capability/resolve-profile.js +69 -0
- package/src/capability/terminal.js +105 -0
- package/src/cli/gallery-command.js +238 -0
- package/src/cli/help.js +35 -0
- package/src/cli/render-command.js +283 -0
- package/src/create-cli-style.js +53 -0
- package/src/formatters/ansi.js +154 -0
- package/src/gallery/render-gallery.js +758 -0
- package/src/index.js +52 -0
- package/src/patterns/agent-transcript.js +180 -0
- package/src/patterns/audit-finding.js +107 -0
- package/src/patterns/command-result.js +91 -0
- package/src/patterns/compact-data-table.js +67 -0
- package/src/patterns/confirmation-result.js +88 -0
- package/src/patterns/diagnostic-report.js +144 -0
- package/src/patterns/helpers.js +106 -0
- package/src/patterns/next-step-block.js +118 -0
- package/src/patterns/task-summary.js +59 -0
- package/src/primitives/bar-chart.js +131 -0
- package/src/primitives/chip.js +33 -0
- package/src/primitives/divider.js +64 -0
- package/src/primitives/feedback.js +83 -0
- package/src/primitives/panel.js +100 -0
- package/src/primitives/progress-bar.js +121 -0
- package/src/primitives/row.js +80 -0
- package/src/primitives/status.js +39 -0
- package/src/primitives/step-progress.js +184 -0
- package/src/primitives/table.js +239 -0
- package/src/profiles/profiles.js +24 -0
- package/src/theme/colours.js +141 -0
- package/src/theme/results.js +98 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { resolveColourValue } from "../theme/colours.js";
|
|
2
|
+
|
|
3
|
+
// ANSI reset sequence used after coloured output.
|
|
4
|
+
const reset = "\u001b[0m";
|
|
5
|
+
|
|
6
|
+
// Matches simple SGR ANSI sequences used by this package.
|
|
7
|
+
const escapeCharacter = String.fromCharCode(27);
|
|
8
|
+
const ansiPattern = new RegExp(`${escapeCharacter}\\[[\\d;]*m`, "gu");
|
|
9
|
+
|
|
10
|
+
// ANSI style sequences supported before primitive renderers exist.
|
|
11
|
+
const styleCodes = {
|
|
12
|
+
bold: "\u001b[1m",
|
|
13
|
+
dim: "\u001b[2m",
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Wrap text in a foreground colour when colour output is enabled.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} value
|
|
20
|
+
* Text to wrap.
|
|
21
|
+
* @param {string} colour
|
|
22
|
+
* Colour token name or hex colour.
|
|
23
|
+
* @param {object} options
|
|
24
|
+
* Formatting options.
|
|
25
|
+
* @param {boolean} options.colour
|
|
26
|
+
* Whether ANSI colour should be applied.
|
|
27
|
+
* @returns {string}
|
|
28
|
+
* Coloured text, or the original text when colour is disabled.
|
|
29
|
+
*/
|
|
30
|
+
export function foreground(value, colour, options = {}) {
|
|
31
|
+
return wrapColour(value, colour, options, createForegroundCode);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Wrap text in a background colour when colour output is enabled.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} value
|
|
38
|
+
* Text to wrap.
|
|
39
|
+
* @param {string} colour
|
|
40
|
+
* Colour token name or hex colour.
|
|
41
|
+
* @param {object} options
|
|
42
|
+
* Formatting options.
|
|
43
|
+
* @param {boolean} options.colour
|
|
44
|
+
* Whether ANSI colour should be applied.
|
|
45
|
+
* @returns {string}
|
|
46
|
+
* Coloured text, or the original text when colour is disabled.
|
|
47
|
+
*/
|
|
48
|
+
export function background(value, colour, options = {}) {
|
|
49
|
+
return wrapColour(value, colour, options, createBackgroundCode);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Wrap text in a basic ANSI style when colour output is enabled.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} value
|
|
56
|
+
* Text to wrap.
|
|
57
|
+
* @param {string} style
|
|
58
|
+
* Style name to apply.
|
|
59
|
+
* @param {object} options
|
|
60
|
+
* Formatting options.
|
|
61
|
+
* @param {boolean} options.colour
|
|
62
|
+
* Whether ANSI style should be applied.
|
|
63
|
+
* @returns {string}
|
|
64
|
+
* Styled text, or the original text when styling is disabled.
|
|
65
|
+
*/
|
|
66
|
+
export function style(value, style, options = {}) {
|
|
67
|
+
const styleCode = styleCodes[style];
|
|
68
|
+
|
|
69
|
+
if (options.colour !== true || styleCode === undefined) {
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return `${styleCode}${value}${reset}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Remove ANSI escape sequences from text.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} value
|
|
80
|
+
* Text to clean.
|
|
81
|
+
* @returns {string}
|
|
82
|
+
* Text without ANSI escape sequences.
|
|
83
|
+
*/
|
|
84
|
+
export function stripAnsi(value) {
|
|
85
|
+
return value.replace(ansiPattern, "");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Wrap text in ANSI colour codes.
|
|
90
|
+
*
|
|
91
|
+
* @param {string} value
|
|
92
|
+
* Text to wrap.
|
|
93
|
+
* @param {string} colour
|
|
94
|
+
* Colour token name or hex colour.
|
|
95
|
+
* @param {object} options
|
|
96
|
+
* Formatting options.
|
|
97
|
+
* @param {Function} createCode
|
|
98
|
+
* Function that creates the ANSI opening sequence.
|
|
99
|
+
* @returns {string}
|
|
100
|
+
* Coloured text, or the original text when colour is disabled.
|
|
101
|
+
*/
|
|
102
|
+
function wrapColour(value, colour, options, createCode) {
|
|
103
|
+
const resolvedColour = resolveColourValue(colour);
|
|
104
|
+
|
|
105
|
+
if (options.colour !== true || resolvedColour === null) {
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return `${createCode(resolvedColour)}${value}${reset}`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Create a foreground ANSI sequence from a hex colour.
|
|
114
|
+
*
|
|
115
|
+
* @param {string} colour
|
|
116
|
+
* Hex colour value.
|
|
117
|
+
* @returns {string}
|
|
118
|
+
* ANSI foreground sequence.
|
|
119
|
+
*/
|
|
120
|
+
function createForegroundCode(colour) {
|
|
121
|
+
const { blue, green, red } = parseHexColour(colour);
|
|
122
|
+
|
|
123
|
+
return `\u001b[38;2;${red};${green};${blue}m`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Create a background ANSI sequence from a hex colour.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} colour
|
|
130
|
+
* Hex colour value.
|
|
131
|
+
* @returns {string}
|
|
132
|
+
* ANSI background sequence.
|
|
133
|
+
*/
|
|
134
|
+
function createBackgroundCode(colour) {
|
|
135
|
+
const { blue, green, red } = parseHexColour(colour);
|
|
136
|
+
|
|
137
|
+
return `\u001b[48;2;${red};${green};${blue}m`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Parse a hex colour into RGB values.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} colour
|
|
144
|
+
* Hex colour value.
|
|
145
|
+
* @returns {object}
|
|
146
|
+
* Red, green, and blue values.
|
|
147
|
+
*/
|
|
148
|
+
function parseHexColour(colour) {
|
|
149
|
+
return {
|
|
150
|
+
blue: Number.parseInt(colour.slice(5, 7), 16),
|
|
151
|
+
green: Number.parseInt(colour.slice(3, 5), 16),
|
|
152
|
+
red: Number.parseInt(colour.slice(1, 3), 16),
|
|
153
|
+
};
|
|
154
|
+
}
|