flagstaff 1.1.1 → 1.2.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/README.md +13 -9
- package/dist/boxen.d.ts +26 -18
- package/dist/boxen.js +244 -170
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
</p>
|
|
27
27
|
|
|
28
28
|
<p align="center">
|
|
29
|
-
<a href="https://burgee.interlace.tools/docs/compatibility"><img src="https://img.shields.io/badge/boxen%20suite-
|
|
29
|
+
<a href="https://burgee.interlace.tools/docs/compatibility"><img src="https://img.shields.io/badge/boxen%20suite-213%2F213-0a6b47?style=flat-square" alt="flagstaff/boxen passes 213 of 213 cases of the boxen test suite" /></a>
|
|
30
30
|
<a href="https://burgee.interlace.tools/docs/compatibility"><img src="https://img.shields.io/badge/cli--table3%20suite-29%2F29-0a6b47?style=flat-square" alt="flagstaff/cli-table3 passes 29 of 29 cases of the cli-table3 test suite" /></a>
|
|
31
31
|
<a href="https://burgee.interlace.tools/docs/compatibility"><img src="https://img.shields.io/badge/log--update%20suite-99%2F99-0a6b47?style=flat-square" alt="flagstaff/log-update passes 99 of 99 cases of the log-update test suite" /></a>
|
|
32
32
|
<a href="https://burgee.interlace.tools/docs/compatibility"><img src="https://img.shields.io/badge/ora%20suite-99%2F99-0a6b47?style=flat-square" alt="flagstaff/ora passes 99 of 99 cases of the ora test suite" /></a>
|
|
@@ -392,9 +392,10 @@ guarantee.
|
|
|
392
392
|
|
|
393
393
|
### The boxen path
|
|
394
394
|
|
|
395
|
-
`flagstaff/boxen` is boxen
|
|
396
|
-
|
|
397
|
-
|
|
395
|
+
`flagstaff/boxen` is boxen 9's API, graded **213 / 213 by boxen 9.0.0's own test suite** — whose
|
|
396
|
+
cases assert the exact characters the box comes out as. boxen 8 is no longer graded or claimed:
|
|
397
|
+
9 changed two of its answers (a hex colour must be real hex, and `vertical` / `horizontal` are a
|
|
398
|
+
fallback for the sides rather than an override), and this follows 9.
|
|
398
399
|
|
|
399
400
|
```diff
|
|
400
401
|
-import boxen from 'boxen';
|
|
@@ -402,8 +403,11 @@ scope for 1.0: it is not graded or claimed, and `burgee migrate` leaves a progra
|
|
|
402
403
|
```
|
|
403
404
|
|
|
404
405
|
`borderStyle` (all eight of cli-boxes', a style object, or `none`), `borderColor`,
|
|
405
|
-
`backgroundColor`, `dimBorder`, `title
|
|
406
|
-
`
|
|
406
|
+
`backgroundColor`, `borderBackgroundColor`, `dimBorder`, `title`, `titleColor` and
|
|
407
|
+
`titleAlignment`, `footer` and `footerAlignment`, `textAlignment`, `padding`, `margin`, `width`,
|
|
408
|
+
`maxWidth`, `height`, `float`, `fullscreen`, and `_borderStyles`. A tab, a backspace or a cursor
|
|
409
|
+
move inside the text, a label or a border is written the way a terminal would draw it, as boxen 9
|
|
410
|
+
does, so it cannot break the box.
|
|
407
411
|
|
|
408
412
|
The drawing **is** the contract here, and matching it byte for byte is the compatibility
|
|
409
413
|
claim rather than a way of avoiding one: a user leaving boxen cares about one thing, whether
|
|
@@ -452,8 +456,8 @@ Every subpath is a lock, not a convention, and the numbers below are asserted by
|
|
|
452
456
|
never the plugin registry; `flagstaff/plugin` 8.4 KB, of which 2.4 KB is the schema;
|
|
453
457
|
`flagstaff/spinner` 9.4 KB; `flagstaff/ora` 46.5 KB — 55.9 KB with roundel counted, against
|
|
454
458
|
ora's own 113.6 KB; `flagstaff/log-update` 29.6 KB, against
|
|
455
|
-
log-update's own 113.4 KB across sixteen, reaching only `linegauge/wrap` and `closeout`; `flagstaff/boxen`
|
|
456
|
-
counted, against boxen's own
|
|
459
|
+
log-update's own 113.4 KB across sixteen, reaching only `linegauge/wrap` and `closeout`; `flagstaff/boxen` 17.6 KB — 67.8 KB with linegauge and
|
|
460
|
+
roundel counted, against boxen 9's own 114.9 KB across fourteen; `flagstaff/cli-table3` 32.9 KB —
|
|
457
461
|
42.3 KB with roundel, against cli-table3's own 106.0 KB across seven. The three façades share `wrap.js` and
|
|
458
462
|
`width.js`, and the first two share `closeout`; none reaches another's port, and none
|
|
459
463
|
reaches the core. `sideEffects: false` lets a
|
|
@@ -468,7 +472,7 @@ Graded by the incumbent's own test suite:
|
|
|
468
472
|
|
|
469
473
|
| suite | passing |
|
|
470
474
|
| :-- | --: |
|
|
471
|
-
| `boxen` |
|
|
475
|
+
| `boxen` | 213 / 213 |
|
|
472
476
|
| `cli-table3` | 29 / 29 |
|
|
473
477
|
| `log-update` | 99 / 99 |
|
|
474
478
|
| `ora` | 99 / 99 |
|
package/dist/boxen.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
type Alignment = 'left' | 'center' | 'right';
|
|
1
2
|
export interface BoxenBorderStyle {
|
|
2
3
|
topLeft: string;
|
|
3
4
|
top: string;
|
|
@@ -7,9 +8,9 @@ export interface BoxenBorderStyle {
|
|
|
7
8
|
bottomLeft: string;
|
|
8
9
|
bottom: string;
|
|
9
10
|
bottomRight: string;
|
|
10
|
-
/**
|
|
11
|
+
/** @deprecated boxen's own name for `left` and `right` together; a fallback for either. */
|
|
11
12
|
vertical?: string;
|
|
12
|
-
/**
|
|
13
|
+
/** @deprecated boxen's own name for `top` and `bottom` together; a fallback for either. */
|
|
13
14
|
horizontal?: string;
|
|
14
15
|
}
|
|
15
16
|
export interface Spacing {
|
|
@@ -18,21 +19,29 @@ export interface Spacing {
|
|
|
18
19
|
bottom?: number;
|
|
19
20
|
left?: number;
|
|
20
21
|
}
|
|
22
|
+
/** boxen's `Color`: one of chalk's sixteen foreground names, or a hex. */
|
|
23
|
+
export type Color = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray' | 'grey' | 'blackBright' | 'redBright' | 'greenBright' | 'yellowBright' | 'blueBright' | 'magentaBright' | 'cyanBright' | 'whiteBright' | (string & Record<never, never>);
|
|
21
24
|
export interface BoxenOptions {
|
|
22
25
|
borderStyle?: string | BoxenBorderStyle;
|
|
23
|
-
borderColor?:
|
|
24
|
-
backgroundColor?:
|
|
26
|
+
borderColor?: Color;
|
|
27
|
+
backgroundColor?: Color;
|
|
28
|
+
/** The border's background: a colour, `'inherit'` (the default) to take `backgroundColor`, or `undefined` for none. */
|
|
29
|
+
borderBackgroundColor?: Color | 'inherit' | undefined;
|
|
25
30
|
dimBorder?: boolean;
|
|
26
31
|
title?: string;
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
titleColor?: Color;
|
|
33
|
+
titleAlignment?: Alignment;
|
|
34
|
+
footer?: string;
|
|
35
|
+
footerAlignment?: Alignment;
|
|
36
|
+
textAlignment?: Alignment;
|
|
29
37
|
/** @deprecated boxen's own name for `textAlignment`, still honoured. */
|
|
30
|
-
align?:
|
|
38
|
+
align?: Alignment;
|
|
31
39
|
padding?: number | Spacing;
|
|
32
40
|
margin?: number | Spacing;
|
|
33
|
-
width?: number
|
|
34
|
-
|
|
35
|
-
|
|
41
|
+
width?: number | `${number}`;
|
|
42
|
+
maxWidth?: number | `${number}`;
|
|
43
|
+
height?: number | `${number}`;
|
|
44
|
+
float?: Alignment;
|
|
36
45
|
fullscreen?: boolean | ((columns: number, rows: number) => [number, number]);
|
|
37
46
|
}
|
|
38
47
|
/**
|
|
@@ -40,20 +49,19 @@ export interface BoxenOptions {
|
|
|
40
49
|
* surface a caller can reach and not an implementation detail to be hidden.
|
|
41
50
|
*/
|
|
42
51
|
declare const BOXES: Record<string, BoxenBorderStyle>;
|
|
43
|
-
/**
|
|
44
|
-
* Draw a box around `text`, exactly as boxen 8 draws it.
|
|
45
|
-
*
|
|
46
|
-
* An invalid `borderColor` or `backgroundColor` throws rather than drawing something
|
|
47
|
-
* plausible, because boxen throws: a colour name that is not one is a typo, and a box drawn
|
|
48
|
-
* in the wrong colour is a bug somebody ships.
|
|
49
|
-
*/
|
|
50
52
|
/** boxen re-exports cli-boxes under this name, so it is surface a caller can reach. */
|
|
51
53
|
export { BOXES as _borderStyles };
|
|
52
54
|
export type Options = BoxenOptions;
|
|
53
55
|
export type CustomBorderStyle = BoxenBorderStyle;
|
|
54
56
|
export type Boxes = typeof BOXES;
|
|
55
57
|
/**
|
|
58
|
+
* Draw a box around `text`, exactly as boxen 9 draws it.
|
|
59
|
+
*
|
|
60
|
+
* An invalid colour throws rather than drawing something plausible, because boxen throws: a
|
|
61
|
+
* colour name that is not one is a typo, and a box drawn in the wrong colour is a bug somebody
|
|
62
|
+
* ships.
|
|
63
|
+
*
|
|
56
64
|
* `import boxen from 'boxen'` is the incumbent's surface. A named export here would break
|
|
57
65
|
* every migration this file exists to serve, so the house rule yields to the host.
|
|
58
66
|
*/
|
|
59
|
-
export default function boxen(
|
|
67
|
+
export default function boxen(input: string, options?: BoxenOptions): string;
|
package/dist/boxen.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { width as stringWidth } from 'linegauge';
|
|
1
|
+
import { slice as sliceAnsi, widest, width as stringWidth } from 'linegauge';
|
|
2
2
|
import { wrap as wrapAnsi } from 'linegauge/wrap';
|
|
3
3
|
import chalk from 'roundel/chalk';
|
|
4
4
|
import { processRuntime } from './runtime.js';
|
|
@@ -7,10 +7,12 @@ const NEWLINE = '\n';
|
|
|
7
7
|
const PAD = ' ';
|
|
8
8
|
const NONE = 'none';
|
|
9
9
|
const FALLBACK_COLUMNS = 80;
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const BAR_ROWS = 2;
|
|
11
|
+
const LABEL_FRAME = 2;
|
|
12
12
|
const SIDE_MARGIN_RATIO = 3;
|
|
13
13
|
const HALF = 2;
|
|
14
|
+
const ESC = '\u{1B}';
|
|
15
|
+
const BACKSPACE = '\u{8}';
|
|
14
16
|
const BOXES = {
|
|
15
17
|
single: { topLeft: '┌', top: '─', topRight: '┐', right: '│', bottomRight: '┘', bottom: '─', bottomLeft: '└', left: '│' },
|
|
16
18
|
double: { topLeft: '╔', top: '═', topRight: '╗', right: '║', bottomRight: '╝', bottom: '═', bottomLeft: '╚', left: '║' },
|
|
@@ -21,131 +23,161 @@ const BOXES = {
|
|
|
21
23
|
classic: { topLeft: '+', top: '-', topRight: '+', right: '|', bottomRight: '+', bottom: '-', bottomLeft: '+', left: '|' },
|
|
22
24
|
arrow: { topLeft: '↘', top: '↓', topRight: '↙', right: '←', bottomRight: '↖', bottom: '↑', bottomLeft: '↗', left: '→' },
|
|
23
25
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
const LINE_BREAKS = /\r\n|[\n\v\f\r]/gu;
|
|
27
|
+
const STYLING_ESCAPE = /^(?:\u{1B}\[[0-9:;]*m|\u{1B}\]8;[^\u{0}-\u{1F};\u{7F}]*;[^\u{0}-\u{1F}\u{7F}]*(?:\u{7}|\u{1B}\\))$/u;
|
|
28
|
+
const CONTROL_ESCAPE = /(\u{1B}\].*?(?:\u{7}|\u{1B}\\|$)|\u{1B}\[[\u{20}-\u{3F}]*[\u{40}-\u{7E}]|\u{1B}[^\u{7}\u{5B}\u{5D}]?)/gsu;
|
|
29
|
+
function writeControls(text) {
|
|
30
|
+
const characters = [];
|
|
31
|
+
for (const [index, part] of text.split(CONTROL_ESCAPE).entries()) {
|
|
32
|
+
if (index % HALF === 1) {
|
|
33
|
+
if (STYLING_ESCAPE.test(part))
|
|
34
|
+
characters.push(part);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
for (const character of part.replaceAll('\t', PAD)) {
|
|
38
|
+
if (character !== BACKSPACE) {
|
|
39
|
+
characters.push(character);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
const previous = characters.findLastIndex((c) => !c.startsWith(ESC));
|
|
43
|
+
if (previous !== -1 && characters[previous] !== NEWLINE)
|
|
44
|
+
characters.splice(previous, 1);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return characters.join('');
|
|
33
48
|
}
|
|
49
|
+
const oneRow = (text) => writeControls(text.replaceAll(LINE_BREAKS, PAD)).toWellFormed();
|
|
50
|
+
const columnsOf = (value) => Number(value);
|
|
51
|
+
const terminalColumns = () => rt.stdout?.columns || rt.stderr?.columns || columnsOf(rt.env['COLUMNS']) || FALLBACK_COLUMNS;
|
|
52
|
+
const terminalRows = () => rt.stdout?.rows || rt.stderr?.rows || columnsOf(rt.env['LINES']);
|
|
34
53
|
function getObject(detail) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
54
|
+
const object = typeof detail === 'number'
|
|
55
|
+
? { top: detail, right: detail * SIDE_MARGIN_RATIO, bottom: detail, left: detail * SIDE_MARGIN_RATIO }
|
|
56
|
+
: { top: 0, right: 0, bottom: 0, left: 0, ...detail };
|
|
57
|
+
const side = (name) => {
|
|
58
|
+
const value = Number(object[name]);
|
|
59
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
60
|
+
};
|
|
61
|
+
return { top: side('top'), right: side('right'), bottom: side('bottom'), left: side('left') };
|
|
39
62
|
}
|
|
40
|
-
const getBorderWidth = (borderStyle) => (borderStyle === NONE ? 0 : BORDERED);
|
|
41
63
|
const SIDES = ['topLeft', 'topRight', 'bottomRight', 'bottomLeft', 'left', 'right', 'top', 'bottom'];
|
|
42
|
-
const widestLine = (text) => Math.max(0, ...text.split(NEWLINE).map((line) => stringWidth(line)));
|
|
43
|
-
function ansiAlign(text, align) {
|
|
44
|
-
if (text === '' || align === 'left')
|
|
45
|
-
return text;
|
|
46
|
-
const lines = text.split(NEWLINE).map((line) => ({ line, width: stringWidth(line) }));
|
|
47
|
-
const maxWidth = Math.max(0, ...lines.map((l) => l.width));
|
|
48
|
-
const diff = (w) => (align === 'right' ? maxWidth - w : Math.floor((maxWidth - w) / HALF));
|
|
49
|
-
return lines.map((l) => PAD.repeat(diff(l.width)) + l.line).join(NEWLINE);
|
|
50
|
-
}
|
|
51
64
|
function getBorderChars(borderStyle) {
|
|
65
|
+
let characters;
|
|
52
66
|
if (borderStyle === NONE) {
|
|
53
|
-
|
|
67
|
+
characters = Object.fromEntries(SIDES.map((side) => [side, '']));
|
|
54
68
|
}
|
|
55
|
-
if (typeof borderStyle === 'string') {
|
|
56
|
-
|
|
57
|
-
if (!characters)
|
|
69
|
+
else if (typeof borderStyle === 'string') {
|
|
70
|
+
if (!Object.hasOwn(BOXES, borderStyle))
|
|
58
71
|
throw new TypeError(`Invalid border style: ${borderStyle}`);
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
const style = { ...borderStyle };
|
|
62
|
-
if (typeof style.vertical === 'string') {
|
|
63
|
-
style.left = style.vertical;
|
|
64
|
-
style.right = style.vertical;
|
|
72
|
+
characters = BOXES[borderStyle];
|
|
65
73
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
else {
|
|
75
|
+
const given = borderStyle;
|
|
76
|
+
characters = { ...given, left: given?.left ?? given?.vertical, right: given?.right ?? given?.vertical, top: given?.top ?? given?.horizontal, bottom: given?.bottom ?? given?.horizontal };
|
|
77
|
+
for (const side of SIDES) {
|
|
78
|
+
if (typeof characters[side] !== 'string')
|
|
79
|
+
throw new TypeError(`Invalid border style: ${side}`);
|
|
80
|
+
}
|
|
73
81
|
}
|
|
74
|
-
return
|
|
82
|
+
return Object.fromEntries(SIDES.map((side) => [side, oneRow(characters[side])]));
|
|
75
83
|
}
|
|
76
|
-
function
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
return horizontalRun.slice(textWidth) + text;
|
|
82
|
-
let horizontal = horizontalRun.slice(textWidth);
|
|
83
|
-
if (horizontal.length % HALF === 1) {
|
|
84
|
-
horizontal = horizontal.slice(Math.floor(horizontal.length / HALF));
|
|
85
|
-
return horizontal.slice(1) + text + horizontal;
|
|
86
|
-
}
|
|
87
|
-
horizontal = horizontal.slice(horizontal.length / HALF);
|
|
88
|
-
return horizontal + text + horizontal;
|
|
84
|
+
function getBorderWidth(borderStyle) {
|
|
85
|
+
if (borderStyle === NONE)
|
|
86
|
+
return 0;
|
|
87
|
+
const { left, right } = getBorderChars(borderStyle);
|
|
88
|
+
return stringWidth(left || PAD) + stringWidth(right || PAD);
|
|
89
89
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
return alignedLines.map((l) => PAD.repeat((max - longest) / HALF) + l);
|
|
95
|
-
if (align === 'right')
|
|
96
|
-
return alignedLines.map((l) => PAD.repeat(max - longest) + l);
|
|
97
|
-
return alignedLines;
|
|
90
|
+
const getBorderHeight = (borderStyle) => (borderStyle === NONE ? 0 : BAR_ROWS);
|
|
91
|
+
function getCornerWidths(borderStyle) {
|
|
92
|
+
const { topLeft, topRight, bottomLeft, bottomRight } = getBorderChars(borderStyle);
|
|
93
|
+
return { top: stringWidth(topLeft) + stringWidth(topRight), bottom: stringWidth(bottomLeft) + stringWidth(bottomRight) };
|
|
98
94
|
}
|
|
99
|
-
function
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
95
|
+
function sanitizeSize(size, borderWidth, minimum = 1) {
|
|
96
|
+
const value = Number(size);
|
|
97
|
+
return Number.isFinite(value) && value > 0 ? Math.max(minimum, value - borderWidth) : undefined;
|
|
98
|
+
}
|
|
99
|
+
const isValidSize = (size) => sanitizeSize(size, 0) !== undefined;
|
|
100
|
+
const wrapLine = (line, width) => (stringWidth(line) > width ? wrapAnsi(line, width, { hard: true }) : line);
|
|
101
|
+
const widestLine = (text) => widest(text.split(NEWLINE));
|
|
102
|
+
function alignText(text, alignment, width) {
|
|
103
|
+
if (alignment === 'left')
|
|
104
|
+
return text;
|
|
105
|
+
return text
|
|
106
|
+
.split(NEWLINE)
|
|
107
|
+
.map((line) => {
|
|
108
|
+
const padding = Math.max(0, width - stringWidth(line));
|
|
109
|
+
return PAD.repeat(alignment === 'right' ? padding : Math.floor(padding / HALF)) + line;
|
|
110
|
+
})
|
|
111
|
+
.join(NEWLINE);
|
|
111
112
|
}
|
|
112
|
-
function
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
function makeLabel(text, horizontal, alignment) {
|
|
114
|
+
const textWidth = stringWidth(text);
|
|
115
|
+
if (alignment === 'left')
|
|
116
|
+
return text + sliceAnsi(horizontal, textWidth);
|
|
117
|
+
if (alignment === 'right')
|
|
118
|
+
return sliceAnsi(horizontal, textWidth) + text;
|
|
119
|
+
const spare = Math.max(0, stringWidth(horizontal) - textWidth);
|
|
120
|
+
if (spare % HALF === 1) {
|
|
121
|
+
const rest = sliceAnsi(horizontal, Math.floor(spare / HALF) + textWidth);
|
|
122
|
+
return sliceAnsi(rest, 1) + text + rest;
|
|
123
|
+
}
|
|
124
|
+
const rest = sliceAnsi(horizontal, spare / HALF + textWidth);
|
|
125
|
+
return rest + text + rest;
|
|
118
126
|
}
|
|
119
127
|
function makeContentText(text, { padding, width, textAlignment, height }) {
|
|
120
|
-
const align = textAlignment ?? 'left';
|
|
121
|
-
const aligned = ansiAlign(text, align);
|
|
122
|
-
let lines = aligned.split(NEWLINE);
|
|
123
|
-
const textWidth = widestLine(aligned);
|
|
124
128
|
const max = width - padding.left - padding.right;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
const wrappedText = text
|
|
130
|
+
.split(NEWLINE)
|
|
131
|
+
.map((line) => wrapLine(line, max))
|
|
132
|
+
.join(NEWLINE);
|
|
133
|
+
const textWidth = Math.min(max, widestLine(wrappedText));
|
|
134
|
+
const alignedText = alignText(wrappedText, textAlignment, textWidth);
|
|
135
|
+
let offset = 0;
|
|
136
|
+
if (textAlignment === 'right')
|
|
137
|
+
offset = max - textWidth;
|
|
138
|
+
else if (textAlignment === 'center')
|
|
139
|
+
offset = Math.floor((max - textWidth) / HALF);
|
|
140
|
+
const paddingLeft = PAD.repeat(padding.left);
|
|
141
|
+
const paddingRight = PAD.repeat(padding.right);
|
|
142
|
+
let lines = alignedText.split(NEWLINE).map((line) => {
|
|
143
|
+
const newLine = paddingLeft + PAD.repeat(offset) + line + paddingRight;
|
|
144
|
+
return newLine + PAD.repeat(Math.max(0, width - stringWidth(newLine)));
|
|
145
|
+
});
|
|
146
|
+
const textRows = height === undefined ? undefined : Math.max(0, height - padding.top - padding.bottom);
|
|
147
|
+
if (textRows !== undefined && lines.length > textRows)
|
|
148
|
+
lines = lines.slice(0, textRows);
|
|
149
|
+
const blank = (count) => Array.from({ length: count }, () => PAD.repeat(width));
|
|
150
|
+
lines = [...blank(padding.top), ...lines, ...blank(padding.bottom)];
|
|
151
|
+
if (height !== undefined && lines.length < height)
|
|
152
|
+
lines = [...lines, ...blank(height - lines.length)];
|
|
153
|
+
return lines.join(NEWLINE);
|
|
132
154
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
return
|
|
155
|
+
function fillBar(character, width) {
|
|
156
|
+
const fill = character || PAD;
|
|
157
|
+
const count = Math.ceil(Math.max(0, width) / Math.max(1, stringWidth(fill)));
|
|
158
|
+
return sliceAnsi(fill.repeat(count), 0, Math.max(0, width));
|
|
137
159
|
}
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
const
|
|
160
|
+
const isHex = (color) => /^#(?:[\da-f]{3}){1,2}$/iu.test(color);
|
|
161
|
+
const COLOR_NAMES = new Set(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray', 'grey', 'blackBright', 'redBright', 'greenBright', 'yellowBright', 'blueBright', 'magentaBright', 'cyanBright', 'whiteBright']);
|
|
162
|
+
const named = (name) => Reflect.get(chalk, name);
|
|
163
|
+
const isColorValid = (color) => typeof color === 'string' && (COLOR_NAMES.has(color) || isHex(color));
|
|
164
|
+
const colorFunction = (color) => (isHex(color) ? chalk.hex(color) : named(color));
|
|
165
|
+
const bgColorFunction = (color) => (isHex(color) ? chalk.bgHex(color) : named(`bg${color.charAt(0).toUpperCase()}${color.slice(1)}`));
|
|
143
166
|
function boxContent(content, contentWidth, options) {
|
|
144
167
|
const colorizeBorder = (border) => {
|
|
145
|
-
const
|
|
146
|
-
|
|
168
|
+
const colored = options.borderColor ? colorFunction(options.borderColor)(border) : border;
|
|
169
|
+
let background = colored;
|
|
170
|
+
if (options.borderBackgroundColor === 'inherit') {
|
|
171
|
+
if (options.backgroundColor)
|
|
172
|
+
background = bgColorFunction(options.backgroundColor)(colored);
|
|
173
|
+
}
|
|
174
|
+
else if (options.borderBackgroundColor !== undefined) {
|
|
175
|
+
background = bgColorFunction(options.borderBackgroundColor)(colored);
|
|
176
|
+
}
|
|
177
|
+
return options.dimBorder ? chalk.dim(background) : background;
|
|
147
178
|
};
|
|
148
|
-
const colorizeContent = (
|
|
179
|
+
const colorizeContent = (text) => (options.backgroundColor ? bgColorFunction(options.backgroundColor)(text) : text);
|
|
180
|
+
const colorizeTitle = (title) => (options.titleColor ? colorFunction(options.titleColor)(title) : title);
|
|
149
181
|
const chars = getBorderChars(options.borderStyle);
|
|
150
182
|
const columns = terminalColumns();
|
|
151
183
|
let marginLeft = PAD.repeat(options.margin.left);
|
|
@@ -155,99 +187,141 @@ function boxContent(content, contentWidth, options) {
|
|
|
155
187
|
else if (options.float === 'right') {
|
|
156
188
|
marginLeft = PAD.repeat(Math.max(columns - contentWidth - options.margin.right - getBorderWidth(options.borderStyle), 0));
|
|
157
189
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
190
|
+
const hasBorder = options.borderStyle !== NONE;
|
|
191
|
+
const left = hasBorder ? chars.left || PAD : '';
|
|
192
|
+
const right = hasBorder ? chars.right || PAD : '';
|
|
193
|
+
const rowWidth = contentWidth + stringWidth(left) + stringWidth(right);
|
|
194
|
+
const bar = (character, cornerStart, cornerEnd, label, alignment) => {
|
|
195
|
+
const span = Math.max(0, rowWidth - stringWidth(cornerStart) - stringWidth(cornerEnd));
|
|
196
|
+
const fill = fillBar(character, span);
|
|
197
|
+
const filled = label ? makeLabel(label, fill, alignment) : fill;
|
|
198
|
+
return sliceAnsi(filled + PAD.repeat(Math.max(0, span - stringWidth(filled))), 0, Math.max(0, span));
|
|
199
|
+
};
|
|
200
|
+
const rows = [];
|
|
201
|
+
if (hasBorder || options.title) {
|
|
202
|
+
const topBar = bar(chars.top, chars.topLeft, chars.topRight, options.title ? colorizeTitle(options.title) : '', options.titleAlignment);
|
|
203
|
+
rows.push(marginLeft + colorizeBorder(chars.topLeft + topBar + chars.topRight));
|
|
165
204
|
}
|
|
166
|
-
|
|
167
|
-
.
|
|
168
|
-
.
|
|
169
|
-
|
|
170
|
-
if (options.
|
|
171
|
-
|
|
205
|
+
for (const line of content === '' ? [] : content.split(NEWLINE)) {
|
|
206
|
+
const padded = line + PAD.repeat(Math.max(0, contentWidth - stringWidth(line)));
|
|
207
|
+
rows.push(marginLeft + colorizeBorder(left) + colorizeContent(padded) + colorizeBorder(right));
|
|
208
|
+
}
|
|
209
|
+
if (hasBorder || options.footer) {
|
|
210
|
+
const bottomBar = bar(chars.bottom, chars.bottomLeft, chars.bottomRight, options.footer ?? '', options.footerAlignment);
|
|
211
|
+
rows.push(marginLeft + colorizeBorder(chars.bottomLeft + bottomBar + chars.bottomRight));
|
|
172
212
|
}
|
|
173
|
-
|
|
174
|
-
result += NEWLINE.repeat(options.margin.bottom);
|
|
175
|
-
return result;
|
|
213
|
+
return NEWLINE.repeat(options.margin.top) + rows.join(NEWLINE) + NEWLINE.repeat(options.margin.bottom);
|
|
176
214
|
}
|
|
177
215
|
function sanitizeOptions(options) {
|
|
178
|
-
if (options.fullscreen
|
|
179
|
-
let dimensions = [
|
|
216
|
+
if (options.fullscreen) {
|
|
217
|
+
let dimensions = [terminalColumns(), terminalRows()];
|
|
180
218
|
if (typeof options.fullscreen === 'function')
|
|
181
219
|
dimensions = options.fullscreen(...dimensions);
|
|
182
|
-
options.width
|
|
183
|
-
|
|
220
|
+
if (!isValidSize(options.width))
|
|
221
|
+
options.width = dimensions[0];
|
|
222
|
+
if (!isValidSize(options.height))
|
|
223
|
+
options.height = dimensions[1];
|
|
184
224
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
225
|
+
const borderWidth = getBorderWidth(options.borderStyle);
|
|
226
|
+
options.width = sanitizeSize(options.width, borderWidth);
|
|
227
|
+
options.maxWidth = sanitizeSize(options.maxWidth, borderWidth);
|
|
228
|
+
options.height = sanitizeSize(options.height, getBorderHeight(options.borderStyle), 0);
|
|
189
229
|
return options;
|
|
190
230
|
}
|
|
191
|
-
const
|
|
192
|
-
function
|
|
193
|
-
if (
|
|
194
|
-
return;
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
options.title = formatTitle(options.title, options.borderStyle);
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
options.title = options.title.slice(0, Math.max(0, maxWidth - HALF));
|
|
202
|
-
if (!options.title)
|
|
203
|
-
return;
|
|
204
|
-
options.title = formatTitle(options.title, options.borderStyle);
|
|
205
|
-
if (stringWidth(options.title) > widest)
|
|
206
|
-
options.width = stringWidth(options.title);
|
|
231
|
+
const formatLabel = (label, borderStyle) => (borderStyle === NONE ? label : ` ${label} `);
|
|
232
|
+
function fitLabel(label, width, borderStyle, cornerWidth) {
|
|
233
|
+
if (!label)
|
|
234
|
+
return label;
|
|
235
|
+
const frame = borderStyle === NONE ? 0 : LABEL_FRAME;
|
|
236
|
+
const fitted = sliceAnsi(oneRow(label), 0, Math.max(0, width + getBorderWidth(borderStyle) - cornerWidth - frame));
|
|
237
|
+
return fitted && formatLabel(fitted, borderStyle);
|
|
207
238
|
}
|
|
208
239
|
function determineDimensions(text, input) {
|
|
209
240
|
const options = sanitizeOptions(input);
|
|
210
|
-
const
|
|
241
|
+
const isWidthOverride = options.width !== undefined;
|
|
211
242
|
const columns = terminalColumns();
|
|
212
243
|
const borderWidth = getBorderWidth(options.borderStyle);
|
|
213
|
-
const
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
options.
|
|
217
|
-
|
|
218
|
-
if (options.
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
244
|
+
const cornerWidths = getCornerWidths(options.borderStyle);
|
|
245
|
+
const corners = Math.max(cornerWidths.top, cornerWidths.bottom);
|
|
246
|
+
const terminalWidth = columns - borderWidth;
|
|
247
|
+
const maxContentWidth = Math.min(terminalWidth, options.maxWidth || terminalWidth);
|
|
248
|
+
const marginWidth = () => {
|
|
249
|
+
if (options.float === 'center')
|
|
250
|
+
return 0;
|
|
251
|
+
if (options.float === 'right')
|
|
252
|
+
return options.margin.right;
|
|
253
|
+
return options.margin.left + options.margin.right;
|
|
254
|
+
};
|
|
255
|
+
const availableWidth = isWidthOverride ? columns - borderWidth - (options.float === 'left' ? options.margin.left : 0) : terminalWidth - marginWidth();
|
|
256
|
+
const maxTextWidth = Math.max(1, maxContentWidth - options.padding.left - options.padding.right);
|
|
257
|
+
const wrappedText = text
|
|
258
|
+
.split(NEWLINE)
|
|
259
|
+
.map((line) => wrapLine(line, maxTextWidth))
|
|
260
|
+
.join(NEWLINE);
|
|
261
|
+
let widestRow = Math.min(widestLine(wrappedText) + options.padding.left + options.padding.right, maxContentWidth);
|
|
262
|
+
options.width ||= widestRow;
|
|
263
|
+
if ((options.margin.left || options.margin.right) && Math.max(1, options.width) > availableWidth) {
|
|
264
|
+
const spaceForMargins = columns - Math.max(1, options.width) - borderWidth;
|
|
265
|
+
const multiplier = spaceForMargins / (options.margin.left + options.margin.right);
|
|
266
|
+
options.margin.left = Math.max(0, Math.floor(options.margin.left * multiplier));
|
|
267
|
+
options.margin.right = Math.max(0, Math.floor(options.margin.right * multiplier));
|
|
268
|
+
}
|
|
269
|
+
const labelWidth = isWidthOverride ? options.width : Math.min(columns - borderWidth - marginWidth(), maxContentWidth);
|
|
270
|
+
options.title = fitLabel(options.title, labelWidth, options.borderStyle, cornerWidths.top);
|
|
271
|
+
options.footer = fitLabel(options.footer, labelWidth, options.borderStyle, cornerWidths.bottom);
|
|
272
|
+
if (getBorderHeight(options.borderStyle) === 0 && options.height !== undefined) {
|
|
273
|
+
options.height = Math.max(0, options.height - (options.title ? 1 : 0) - (options.footer ? 1 : 0));
|
|
274
|
+
}
|
|
275
|
+
if (!isWidthOverride) {
|
|
276
|
+
for (const [label, cornerWidth] of [
|
|
277
|
+
[options.title, cornerWidths.top],
|
|
278
|
+
[options.footer, cornerWidths.bottom],
|
|
279
|
+
]) {
|
|
280
|
+
if (label)
|
|
281
|
+
widestRow = Math.max(widestRow, stringWidth(label) - borderWidth + cornerWidth);
|
|
223
282
|
}
|
|
224
|
-
options.width = Math.min(
|
|
283
|
+
options.width = Math.max(1, Math.min(widestRow, columns - borderWidth - marginWidth()));
|
|
225
284
|
}
|
|
226
|
-
|
|
285
|
+
options.width = Math.max(options.width, corners - borderWidth, 1);
|
|
286
|
+
if (options.padding.left + options.padding.right >= options.width) {
|
|
227
287
|
options.padding.left = 0;
|
|
228
288
|
options.padding.right = 0;
|
|
229
289
|
}
|
|
230
|
-
if (options.height !== undefined && options.
|
|
290
|
+
if (options.height !== undefined && options.padding.top + options.padding.bottom >= options.height) {
|
|
231
291
|
options.padding.top = 0;
|
|
232
292
|
options.padding.bottom = 0;
|
|
233
293
|
}
|
|
234
294
|
return options;
|
|
235
295
|
}
|
|
236
296
|
export { BOXES as _borderStyles };
|
|
237
|
-
export default function boxen(
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
297
|
+
export default function boxen(input, options = {}) {
|
|
298
|
+
const text = writeControls(input.replaceAll(LINE_BREAKS, NEWLINE)).toWellFormed();
|
|
299
|
+
const merged = { padding: 0, dimBorder: false, float: 'left', ...options };
|
|
300
|
+
const borderStyle = merged.borderStyle ?? 'single';
|
|
301
|
+
const textAlignment = merged.textAlignment ?? merged.align ?? 'left';
|
|
302
|
+
for (const name of ['borderColor', 'titleColor', 'backgroundColor']) {
|
|
303
|
+
const color = merged[name];
|
|
304
|
+
if (color && !isColorValid(color))
|
|
305
|
+
throw new Error(`${color} is not a valid ${name}`);
|
|
306
|
+
}
|
|
307
|
+
const borderBackgroundColor = 'borderBackgroundColor' in merged ? merged.borderBackgroundColor : 'inherit';
|
|
308
|
+
if (borderBackgroundColor !== undefined && borderBackgroundColor !== 'inherit' && !isColorValid(borderBackgroundColor)) {
|
|
309
|
+
throw new Error(`${borderBackgroundColor} is not a valid borderBackgroundColor`);
|
|
310
|
+
}
|
|
245
311
|
const resolved = {
|
|
246
312
|
...merged,
|
|
313
|
+
borderStyle,
|
|
314
|
+
textAlignment,
|
|
315
|
+
titleAlignment: merged.titleAlignment ?? 'left',
|
|
316
|
+
footerAlignment: merged.footerAlignment ?? 'left',
|
|
317
|
+
borderBackgroundColor,
|
|
247
318
|
padding: getObject(merged.padding),
|
|
248
319
|
margin: getObject(merged.margin),
|
|
249
|
-
width: merged.width
|
|
320
|
+
width: merged.width,
|
|
321
|
+
maxWidth: merged.maxWidth,
|
|
322
|
+
height: merged.height,
|
|
250
323
|
};
|
|
251
324
|
const dimensions = determineDimensions(text, resolved);
|
|
252
|
-
|
|
325
|
+
const content = makeContentText(text, dimensions);
|
|
326
|
+
return boxContent(content, Math.max(dimensions.width, widestLine(content)), dimensions);
|
|
253
327
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flagstaff",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "The staff the flag flies from. A terminal frame loop with a static projection for agents and screen readers, and a plugin host for spinners, progress, boxes and tables. Drop-in paths for ora, log-update, boxen and cli-table3. No dependency outside the burgee family.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|