@raidiant/notifai 0.2.0 → 0.3.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/dist/commands.d.ts +33 -3
- package/dist/commands.js +283 -59
- package/dist/commands.js.map +1 -1
- package/dist/config-schema.d.ts +62 -0
- package/dist/config-schema.js +274 -0
- package/dist/config-schema.js.map +1 -0
- package/dist/hooks.d.ts +31 -19
- package/dist/hooks.js +277 -189
- package/dist/hooks.js.map +1 -1
- package/dist/interactive.d.ts +4 -0
- package/dist/interactive.js +604 -0
- package/dist/interactive.js.map +1 -0
- package/dist/main.js +106 -17
- package/dist/main.js.map +1 -1
- package/dist/ui/banner.d.ts +22 -0
- package/dist/ui/banner.js +87 -0
- package/dist/ui/banner.js.map +1 -0
- package/dist/ui/config-view.d.ts +32 -0
- package/dist/ui/config-view.js +105 -0
- package/dist/ui/config-view.js.map +1 -0
- package/dist/ui/help.d.ts +46 -0
- package/dist/ui/help.js +105 -0
- package/dist/ui/help.js.map +1 -0
- package/dist/ui/theme.d.ts +108 -0
- package/dist/ui/theme.js +261 -0
- package/dist/ui/theme.js.map +1 -0
- package/package.json +2 -1
- package/src/commands.test.ts +153 -11
- package/src/commands.ts +328 -60
- package/src/config-schema.test.ts +152 -0
- package/src/config-schema.ts +321 -0
- package/src/hooks.test.ts +214 -81
- package/src/hooks.ts +335 -224
- package/src/interactive.ts +692 -0
- package/src/main.ts +107 -16
- package/src/ui/banner.ts +95 -0
- package/src/ui/config-view.test.ts +86 -0
- package/src/ui/config-view.ts +135 -0
- package/src/ui/help.ts +124 -0
- package/src/ui/theme.test.ts +92 -0
- package/src/ui/theme.ts +279 -0
package/src/ui/help.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Help that answers "what can I do here", not "what strings does argv accept".
|
|
3
|
+
*
|
|
4
|
+
* The old help listed seventeen commands in one flat column, with the two a new
|
|
5
|
+
* user needs (`init`, `login`) sitting between a platform capability dump and
|
|
6
|
+
* an entry point marked "Internal:". Everything was equally prominent, which is
|
|
7
|
+
* the same as nothing being prominent.
|
|
8
|
+
*
|
|
9
|
+
* Grouping and colour are Commander's own (`commandsGroup`, `optionsGroup`, and
|
|
10
|
+
* the `styleX` hooks it calls when the output has colours); this module only
|
|
11
|
+
* supplies the palette and the group vocabulary. Commander turns its styling
|
|
12
|
+
* off by itself when the output is not a terminal, so piped help stays plain
|
|
13
|
+
* without a second code path.
|
|
14
|
+
*/
|
|
15
|
+
import { Help, type Command, type HelpConfiguration, type Option } from 'commander'
|
|
16
|
+
import { glyph, style } from './theme.js'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Group headings, in the order a reader meets them.
|
|
20
|
+
*
|
|
21
|
+
* Ordering is by who needs it and when: first run, then daily use, then the
|
|
22
|
+
* commands agents drive, then the things touched once or never. Commander
|
|
23
|
+
* renders groups in first-use order, so these constants are also the layout.
|
|
24
|
+
*/
|
|
25
|
+
export const GROUP = {
|
|
26
|
+
start: 'Getting started:',
|
|
27
|
+
daily: 'Everyday:',
|
|
28
|
+
agent: 'For agents and scripts:',
|
|
29
|
+
advanced: 'Account and wiring:',
|
|
30
|
+
help: 'Help:',
|
|
31
|
+
} as const
|
|
32
|
+
|
|
33
|
+
/** Option groups for `send`, which carries thirty flags. */
|
|
34
|
+
export const SEND_GROUP = {
|
|
35
|
+
content: 'What it says:',
|
|
36
|
+
routing: 'Where it goes:',
|
|
37
|
+
presentation: 'How it arrives:',
|
|
38
|
+
reply: 'Asking for an answer:',
|
|
39
|
+
advanced: 'Advanced:',
|
|
40
|
+
} as const
|
|
41
|
+
|
|
42
|
+
/** Group order, independent of the order commands happen to be registered in. */
|
|
43
|
+
const GROUP_ORDER: readonly string[] = [
|
|
44
|
+
GROUP.start,
|
|
45
|
+
GROUP.daily,
|
|
46
|
+
GROUP.agent,
|
|
47
|
+
GROUP.advanced,
|
|
48
|
+
GROUP.help,
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
function groupRank(heading: string): number {
|
|
52
|
+
const index = GROUP_ORDER.indexOf(heading)
|
|
53
|
+
// Anything not named above keeps its position relative to its peers, because
|
|
54
|
+
// the sort is stable — which is what lets the same override run over option
|
|
55
|
+
// groups without needing to know their headings.
|
|
56
|
+
return index === -1 ? GROUP_ORDER.length : index
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const helpConfiguration: HelpConfiguration = {
|
|
60
|
+
// Groups already carry the intended order; alphabetising would scatter the
|
|
61
|
+
// dependency between `login` and everything that needs a credential.
|
|
62
|
+
sortSubcommands: false,
|
|
63
|
+
sortOptions: false,
|
|
64
|
+
showGlobalOptions: false,
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Commander seeds group order from the raw registration order, which ties
|
|
68
|
+
* how the help reads to where a `program.command(…)` block happens to sit in
|
|
69
|
+
* the file — move one and the help silently reorders. Sorting the finished
|
|
70
|
+
* grouping makes `GROUP_ORDER` the single place that decision is made.
|
|
71
|
+
*
|
|
72
|
+
* The same override runs over option groups, whose headings are not in
|
|
73
|
+
* `GROUP_ORDER` at all. They all rank equal, and `sort` is stable, so they
|
|
74
|
+
* keep the order they were declared in.
|
|
75
|
+
*/
|
|
76
|
+
groupItems<T extends Command | Option>(
|
|
77
|
+
this: Help,
|
|
78
|
+
unsortedItems: T[],
|
|
79
|
+
visibleItems: T[],
|
|
80
|
+
getGroup: (item: T) => string,
|
|
81
|
+
): Map<string, T[]> {
|
|
82
|
+
// `.call` cannot carry the method's own generic through, so the signature
|
|
83
|
+
// is restated once here instead of widening every caller to
|
|
84
|
+
// `Command | Option`.
|
|
85
|
+
const base = Help.prototype.groupItems as unknown as (
|
|
86
|
+
this: Help,
|
|
87
|
+
unsorted: T[],
|
|
88
|
+
visible: T[],
|
|
89
|
+
group: (item: T) => string,
|
|
90
|
+
) => Map<string, T[]>
|
|
91
|
+
const grouped = base.call(this, unsortedItems, visibleItems, getGroup)
|
|
92
|
+
return new Map([...grouped.entries()].sort((a, b) => groupRank(a[0]) - groupRank(b[0])))
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
styleTitle: (title: string): string => style.heading(title),
|
|
96
|
+
styleCommandText: (text: string): string => style.accent(text),
|
|
97
|
+
styleSubcommandTerm: (text: string): string => style.code(text),
|
|
98
|
+
styleOptionTerm: (text: string): string => style.code(text),
|
|
99
|
+
styleArgumentTerm: (text: string): string => style.code(text),
|
|
100
|
+
styleDescriptionText: (text: string): string => text,
|
|
101
|
+
styleUsage: (text: string): string => text,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Footer for the root help.
|
|
106
|
+
*
|
|
107
|
+
* Its whole job is to make the interactive app discoverable, because someone
|
|
108
|
+
* reading a wall of flags has already demonstrated they did not know it was
|
|
109
|
+
* there.
|
|
110
|
+
*/
|
|
111
|
+
export function rootHelpFooter(): string {
|
|
112
|
+
const lines = [
|
|
113
|
+
'',
|
|
114
|
+
style.heading('Start here'),
|
|
115
|
+
` ${style.code('notifai')} ${style.dim(
|
|
116
|
+
`${glyph.arrow} the interactive app: status, settings, devices, test sends`,
|
|
117
|
+
)}`,
|
|
118
|
+
` ${style.code('notifai init')} ${style.dim(`${glyph.arrow} set this project up, step by step`)}`,
|
|
119
|
+
'',
|
|
120
|
+
style.dim(`Every command takes ${style.code('--help')}. Machine-readable output: ${style.code('--json')}.`),
|
|
121
|
+
style.dim(`Settings are explained by ${style.code('notifai config show')} and ${style.code('notifai config explain <key>')}.`),
|
|
122
|
+
]
|
|
123
|
+
return lines.join('\n')
|
|
124
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { card, clip, pad, width, wrap } from './theme.js'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* These run with colour off — vitest's stdout is not a TTY, so `picocolors`
|
|
6
|
+
* resolves to identity functions. That is the interesting case: it is the one
|
|
7
|
+
* every agent, pipe and CI log gets, and the one where a layout bug is
|
|
8
|
+
* invisible to a human developer reading a colourful terminal.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const RED = '[31m'
|
|
12
|
+
const RESET = '[39m'
|
|
13
|
+
|
|
14
|
+
describe('width', () => {
|
|
15
|
+
it('counts printable characters, not escape bytes', () => {
|
|
16
|
+
expect(width('hello')).toBe(5)
|
|
17
|
+
expect(width(`${RED}hello${RESET}`)).toBe(5)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('pad', () => {
|
|
22
|
+
it('pads by visible width so colour cannot break a column', () => {
|
|
23
|
+
expect(width(pad(`${RED}ab${RESET}`, 6))).toBe(6)
|
|
24
|
+
expect(pad('ab', 4)).toBe('ab ')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('never truncates when the text already exceeds the column', () => {
|
|
28
|
+
expect(pad('abcdef', 3)).toBe('abcdef')
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('wrap', () => {
|
|
33
|
+
it('breaks on whitespace within the limit', () => {
|
|
34
|
+
expect(wrap('one two three four', 9)).toEqual(['one two', 'three', 'four'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('returns bare lines, leaving indentation to the caller', () => {
|
|
38
|
+
// The regression this guards: wrap used to indent continuation lines
|
|
39
|
+
// itself while callers indented every line, so each wrapped paragraph
|
|
40
|
+
// stepped further right down the page.
|
|
41
|
+
for (const line of wrap('alpha beta gamma delta', 11)) {
|
|
42
|
+
expect(line.startsWith(' ')).toBe(false)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('keeps a word that is longer than the limit rather than losing it', () => {
|
|
47
|
+
expect(wrap('supercalifragilistic', 5)).toEqual(['supercalifragilistic'])
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe('clip', () => {
|
|
52
|
+
it('leaves text that already fits', () => {
|
|
53
|
+
expect(clip('short', 10)).toBe('short')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('truncates by visible width and never mid-escape', () => {
|
|
57
|
+
const clipped = clip(`${RED}abcdefghij${RESET}`, 5)
|
|
58
|
+
expect(width(clipped)).toBeLessThanOrEqual(5)
|
|
59
|
+
// A slice by String.length would cut through `[31m` and spill the
|
|
60
|
+
// raw bytes into the terminal.
|
|
61
|
+
expect(clipped).toContain(RED)
|
|
62
|
+
expect(clipped.endsWith(RESET)).toBe(true)
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
describe('card', () => {
|
|
67
|
+
it('draws a closed box whose rows all share one width', () => {
|
|
68
|
+
const lines = card('Status', ['a', 'bbbbbbbbbb'], 40)
|
|
69
|
+
const widths = new Set(lines.map((line) => width(line)))
|
|
70
|
+
expect(widths.size).toBe(1)
|
|
71
|
+
expect(lines[0]).toContain('Status')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('measures rows by visible width, so colour does not skew the border', () => {
|
|
75
|
+
const plain = card('T', ['abcdef'], 40)
|
|
76
|
+
const coloured = card('T', [`${RED}abcdef${RESET}`], 40)
|
|
77
|
+
expect(plain.map(width)).toEqual(coloured.map(width))
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('survives a terminal that reports no usable width', () => {
|
|
81
|
+
// A pty reporting zero columns made the obvious library call compute a
|
|
82
|
+
// negative pad and throw, taking the whole interactive app with it. A
|
|
83
|
+
// status card may degrade; it must never be the thing that crashes.
|
|
84
|
+
expect(() => card('Status', ['something reasonably long here'], 0)).not.toThrow()
|
|
85
|
+
expect(card('Status', ['x'], 0).length).toBe(3)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('clips a row too wide for the terminal instead of wrapping it', () => {
|
|
89
|
+
const lines = card('T', ['x'.repeat(200)], 40)
|
|
90
|
+
for (const line of lines) expect(width(line)).toBeLessThanOrEqual(40)
|
|
91
|
+
})
|
|
92
|
+
})
|
package/src/ui/theme.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One visual language for every human-facing surface: help, `config show`,
|
|
3
|
+
* `doctor`, and the interactive app.
|
|
4
|
+
*
|
|
5
|
+
* Two rules hold everything here together.
|
|
6
|
+
*
|
|
7
|
+
* Colour is decoration, never information. Every state that colour marks also
|
|
8
|
+
* carries a glyph and a word, because the output is read through pipes, on
|
|
9
|
+
* monochrome terminals, by screen readers, and by people who cannot separate
|
|
10
|
+
* red from green. `picocolors` resolves to identity functions when stdout is
|
|
11
|
+
* not a TTY or `NO_COLOR` is set, so the plain path is the same code, not a
|
|
12
|
+
* second implementation that can drift.
|
|
13
|
+
*
|
|
14
|
+
* And an agent's bytes are not ours to restyle. Machine-readable output
|
|
15
|
+
* (`--json`) never routes through this module, and every non-TTY invocation
|
|
16
|
+
* lands on the uncoloured branch by construction.
|
|
17
|
+
*/
|
|
18
|
+
import pc from 'picocolors'
|
|
19
|
+
|
|
20
|
+
/** True when the terminal can render the box-drawing and status glyphs below. */
|
|
21
|
+
export const unicode: boolean =
|
|
22
|
+
process.platform !== 'win32' ||
|
|
23
|
+
process.env['WT_SESSION'] !== undefined ||
|
|
24
|
+
process.env['TERM_PROGRAM'] === 'vscode' ||
|
|
25
|
+
process.env['ConEmuTask'] === '{cmd::Cmder}'
|
|
26
|
+
|
|
27
|
+
/** True when 24-bit colour is available, which the banner gradient needs. */
|
|
28
|
+
export function supportsTrueColor(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
29
|
+
if (!pc.isColorSupported) return false
|
|
30
|
+
const colorterm = env['COLORTERM'] ?? ''
|
|
31
|
+
if (/^(truecolor|24bit)$/i.test(colorterm)) return true
|
|
32
|
+
return env['TERM_PROGRAM'] === 'iTerm.app' || env['TERM_PROGRAM'] === 'vscode'
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const color = pc
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Brand ramp, shared with the companion app icon: pink → coral → rose →
|
|
39
|
+
* magenta. Used for the wordmark and for accenting the surfaces a human sees.
|
|
40
|
+
*/
|
|
41
|
+
export const BRAND_RAMP = ['#FF9EC0', '#FF816B', '#EC5A73', '#B84E93'] as const
|
|
42
|
+
|
|
43
|
+
interface Rgb {
|
|
44
|
+
r: number
|
|
45
|
+
g: number
|
|
46
|
+
b: number
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function hexToRgb(hex: string): Rgb {
|
|
50
|
+
const value = Number.parseInt(hex.slice(1), 16)
|
|
51
|
+
return { r: (value >> 16) & 255, g: (value >> 8) & 255, b: value & 255 }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function mix(a: Rgb, b: Rgb, t: number): Rgb {
|
|
55
|
+
return {
|
|
56
|
+
r: Math.round(a.r + (b.r - a.r) * t),
|
|
57
|
+
g: Math.round(a.g + (b.g - a.g) * t),
|
|
58
|
+
b: Math.round(a.b + (b.b - a.b) * t),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sample the brand ramp at `t` in [0, 1]. Multi-stop so the gradient keeps the
|
|
64
|
+
* icon's coral midtones instead of washing straight from pink to magenta.
|
|
65
|
+
*/
|
|
66
|
+
export function rampAt(t: number, ramp: readonly string[] = BRAND_RAMP): string {
|
|
67
|
+
const clamped = Math.min(1, Math.max(0, t))
|
|
68
|
+
const span = ramp.length - 1
|
|
69
|
+
const scaled = clamped * span
|
|
70
|
+
const index = Math.min(span - 1, Math.floor(scaled))
|
|
71
|
+
const { r, g, b } = mix(hexToRgb(ramp[index]!), hexToRgb(ramp[index + 1]!), scaled - index)
|
|
72
|
+
return `[38;2;${r};${g};${b}m`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const RESET = '[39m'
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* SGR escape sequences, in the three shapes the helpers below need: stripping
|
|
79
|
+
* them to measure, splitting around them to slice, and recognising one on its
|
|
80
|
+
* own. Kept together so a change to what counts as an escape lands once.
|
|
81
|
+
*/
|
|
82
|
+
const ANSI_GLOBAL = /\[[0-9;]*m/g
|
|
83
|
+
const ANSI_SPLIT = /(\[[0-9;]*m)/
|
|
84
|
+
const ANSI_ONLY = /^\[[0-9;]*m$/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Paint `text` across the brand ramp, one escape per character.
|
|
88
|
+
*
|
|
89
|
+
* Falls back to a single flat brand colour without truecolor, and to the bare
|
|
90
|
+
* string with colour off — a gradient is the most decorative thing this CLI
|
|
91
|
+
* does, so it must also be the first thing to degrade quietly.
|
|
92
|
+
*/
|
|
93
|
+
export function gradient(text: string, ramp: readonly string[] = BRAND_RAMP): string {
|
|
94
|
+
if (!pc.isColorSupported) return text
|
|
95
|
+
if (!supportsTrueColor()) return pc.magenta(text)
|
|
96
|
+
const chars = [...text]
|
|
97
|
+
const last = Math.max(1, chars.length - 1)
|
|
98
|
+
return chars.map((char, i) => (char === ' ' ? char : `${rampAt(i / last, ramp)}${char}`)).join('') + RESET
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Paint each line of a block so the gradient runs left-to-right across the
|
|
103
|
+
* whole block rather than restarting per line. The wordmark is one image, and
|
|
104
|
+
* per-line gradients make it read as several.
|
|
105
|
+
*/
|
|
106
|
+
export function gradientBlock(lines: readonly string[], ramp: readonly string[] = BRAND_RAMP): string[] {
|
|
107
|
+
if (!pc.isColorSupported) return [...lines]
|
|
108
|
+
const width = Math.max(1, ...lines.map((line) => [...line].length))
|
|
109
|
+
if (!supportsTrueColor()) return lines.map((line) => pc.magenta(line))
|
|
110
|
+
return lines.map((line) =>
|
|
111
|
+
[...line]
|
|
112
|
+
.map((char, i) => (char === ' ' ? char : `${rampAt(i / Math.max(1, width - 1), ramp)}${char}`))
|
|
113
|
+
.join('') + RESET,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
// Semantic styles
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
export const style = {
|
|
122
|
+
/** A heading that opens a section. */
|
|
123
|
+
heading: (text: string): string => pc.bold(text),
|
|
124
|
+
/** The brand voice — command names, the product name, the selected thing. */
|
|
125
|
+
accent: (text: string): string => pc.magenta(text),
|
|
126
|
+
/** Secondary text: hints, provenance, units, defaults. */
|
|
127
|
+
dim: (text: string): string => pc.dim(text),
|
|
128
|
+
/** Something the reader can type. */
|
|
129
|
+
code: (text: string): string => pc.cyan(text),
|
|
130
|
+
ok: (text: string): string => pc.green(text),
|
|
131
|
+
warn: (text: string): string => pc.yellow(text),
|
|
132
|
+
bad: (text: string): string => pc.red(text),
|
|
133
|
+
/** A value as it currently stands. */
|
|
134
|
+
value: (text: string): string => pc.bold(pc.white(text)),
|
|
135
|
+
/** A value that is not set, or is inherited rather than chosen. */
|
|
136
|
+
unset: (text: string): string => pc.dim(pc.italic(text)),
|
|
137
|
+
} as const
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Status marks. The glyph and the colour say the same thing twice on purpose;
|
|
141
|
+
* either alone is enough to read the line.
|
|
142
|
+
*/
|
|
143
|
+
export const glyph = {
|
|
144
|
+
ok: unicode ? '●' : '*',
|
|
145
|
+
warn: unicode ? '▲' : '!',
|
|
146
|
+
bad: unicode ? '✕' : 'x',
|
|
147
|
+
pending: unicode ? '○' : 'o',
|
|
148
|
+
bullet: unicode ? '•' : '-',
|
|
149
|
+
arrow: unicode ? '→' : '->',
|
|
150
|
+
chevron: unicode ? '›' : '>',
|
|
151
|
+
bar: unicode ? '│' : '|',
|
|
152
|
+
} as const
|
|
153
|
+
|
|
154
|
+
export type Tone = 'ok' | 'warn' | 'bad' | 'pending'
|
|
155
|
+
|
|
156
|
+
/** A coloured glyph for a tone; the caller supplies the words. */
|
|
157
|
+
export function mark(tone: Tone): string {
|
|
158
|
+
switch (tone) {
|
|
159
|
+
case 'ok':
|
|
160
|
+
return style.ok(glyph.ok)
|
|
161
|
+
case 'warn':
|
|
162
|
+
return style.warn(glyph.warn)
|
|
163
|
+
case 'bad':
|
|
164
|
+
return style.bad(glyph.bad)
|
|
165
|
+
case 'pending':
|
|
166
|
+
return style.dim(glyph.pending)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Visible width, ignoring ANSI escapes, for alignment maths. */
|
|
171
|
+
export function width(text: string): number {
|
|
172
|
+
return [...text.replace(ANSI_GLOBAL, '')].length
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Pad to `size` using visible width, so colour never breaks a column. */
|
|
176
|
+
export function pad(text: string, size: number): string {
|
|
177
|
+
return text + ' '.repeat(Math.max(0, size - width(text)))
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Usable terminal width: the real one, capped so paragraphs do not run to 200
|
|
182
|
+
* characters on a maximised window.
|
|
183
|
+
*
|
|
184
|
+
* No lower clamp. Raising a narrow terminal to a comfortable minimum sounds
|
|
185
|
+
* harmless and is not — every box drawn to that invented width overflows the
|
|
186
|
+
* real one and wraps, which looks far worse than a cramped box that fits.
|
|
187
|
+
*/
|
|
188
|
+
export function terminalWidth(fallback = 80): number {
|
|
189
|
+
const columns = process.stdout.columns
|
|
190
|
+
if (typeof columns !== 'number' || columns <= 0) return fallback
|
|
191
|
+
return Math.min(100, columns)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A titled box around `rows`.
|
|
196
|
+
*
|
|
197
|
+
* Hand-drawn rather than delegated, because the obvious library call sizes
|
|
198
|
+
* itself from `process.stdout.columns` and divides by it — a pty that reports
|
|
199
|
+
* zero columns (some CI runners, some `script` invocations) makes the padding
|
|
200
|
+
* negative and throws, taking the whole screen down with it. A status card is
|
|
201
|
+
* decoration; it may degrade, but it must never be what crashes the app.
|
|
202
|
+
*
|
|
203
|
+
* Rows are measured with `width`, so colour inside them does not skew the
|
|
204
|
+
* border, and anything too wide for the terminal is clipped rather than
|
|
205
|
+
* wrapped — a status line that wraps stops being a status line.
|
|
206
|
+
*/
|
|
207
|
+
export function card(title: string, rows: readonly string[], columns = terminalWidth()): string[] {
|
|
208
|
+
const [tl, tr, bl, br, h, v] = unicode
|
|
209
|
+
? ['╭', '╮', '╰', '╯', '─', '│']
|
|
210
|
+
: ['+', '+', '+', '+', '-', '|']
|
|
211
|
+
const inner = Math.max(20, columns - 4)
|
|
212
|
+
const content = rows.map((row) => (width(row) > inner ? clip(row, inner) : row))
|
|
213
|
+
const bodyWidth = Math.max(width(title) + 4, ...content.map((row) => width(row)))
|
|
214
|
+
const boxWidth = Math.min(inner, bodyWidth)
|
|
215
|
+
const heading = `${h} ${style.heading(title)} `
|
|
216
|
+
const lines: string[] = []
|
|
217
|
+
lines.push(style.dim(tl) + heading + style.dim(h!.repeat(Math.max(0, boxWidth - width(heading) + 2))) + style.dim(tr))
|
|
218
|
+
for (const row of content) {
|
|
219
|
+
lines.push(`${style.dim(v)} ${pad(row, boxWidth)} ${style.dim(v)}`)
|
|
220
|
+
}
|
|
221
|
+
lines.push(style.dim(bl! + h!.repeat(boxWidth + 2) + br))
|
|
222
|
+
return lines
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Truncate to a visible width, carrying escape sequences through untouched.
|
|
227
|
+
*
|
|
228
|
+
* Counting only printable characters is the whole point: slicing a coloured
|
|
229
|
+
* string by `String.length` cuts through an escape sequence and spills raw
|
|
230
|
+
* `[38;2` into the terminal.
|
|
231
|
+
*/
|
|
232
|
+
export function clip(text: string, limit: number): string {
|
|
233
|
+
if (width(text) <= limit) return text
|
|
234
|
+
const parts = text.split(ANSI_SPLIT)
|
|
235
|
+
let out = ''
|
|
236
|
+
let visible = 0
|
|
237
|
+
for (const part of parts) {
|
|
238
|
+
if (part === undefined || part === '') continue
|
|
239
|
+
if (ANSI_ONLY.test(part)) {
|
|
240
|
+
out += part
|
|
241
|
+
continue
|
|
242
|
+
}
|
|
243
|
+
for (const char of part) {
|
|
244
|
+
if (visible >= limit - 1) return `${out}\u2026${RESET}`
|
|
245
|
+
out += char
|
|
246
|
+
visible += 1
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return `${out}${RESET}`
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Wrap `text` to `columns`, returning bare lines.
|
|
254
|
+
*
|
|
255
|
+
* Indentation is the caller's, deliberately: an earlier version indented
|
|
256
|
+
* continuation lines itself, callers indented every line as well, and each
|
|
257
|
+
* wrapped paragraph stepped further right down the page.
|
|
258
|
+
*/
|
|
259
|
+
export function wrap(text: string, columns: number): string[] {
|
|
260
|
+
// Honour the width asked for. A floor here would silently ignore small
|
|
261
|
+
// values and hand back lines wider than the caller reserved room for; a
|
|
262
|
+
// width of zero or less is the only nonsense, and that falls back.
|
|
263
|
+
const limit = columns > 0 ? columns : 80
|
|
264
|
+
const words = text.split(/\s+/).filter(Boolean)
|
|
265
|
+
if (words.length === 0) return ['']
|
|
266
|
+
const lines: string[] = []
|
|
267
|
+
let line = ''
|
|
268
|
+
for (const word of words) {
|
|
269
|
+
const candidate = line === '' ? word : `${line} ${word}`
|
|
270
|
+
if (width(candidate) > limit && line !== '') {
|
|
271
|
+
lines.push(line)
|
|
272
|
+
line = word
|
|
273
|
+
} else {
|
|
274
|
+
line = candidate
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (line !== '') lines.push(line)
|
|
278
|
+
return lines
|
|
279
|
+
}
|