-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli.js
executable file
·179 lines (157 loc) · 4.38 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
import process from 'node:process';
import meow from 'meow';
import getStdin from 'get-stdin';
import boxen, {_borderStyles} from 'boxen';
import indentString from 'indent-string';
const cli = meow(`
Usage
$ boxen <string>
$ echo <string> | boxen
Options
--border-color Color of the box border [black|red|green|yellow|blue|magenta|cyan|white|gray]
--background-color Color of the background [black|red|green|yellow|blue|magenta|cyan|white]
--border-style Style of the box border [single|double|round|singleDouble|doubleSingle|classic]
Can also be specified as the characters to use. See below example.
--dim-border Reduce opacity of border
--padding Space between the text and box border
--margin Space around the box
--center Center the box
--text-alignment Align the text [left|center|right] (Default: left)
--title Display a title at the top of the box
--title-alignment Align the title in the top bar [left|center|right]
--width Set a fixed width for the box
--height Set a fixed height for the box
--fullscreen Fit all available space within the terminal
Examples
$ boxen I ❤ unicorns
┌────────────┐
│I ❤ unicorns│
└────────────┘
$ boxen --border-style=doubleSingle …like everyone
╒══════════════╕
│…like everyone│
╘══════════════╛
$ boxen --border-style='1234-~|║' ASCII ftw!
1----------2
|ASCII ftw!║
3~~~~~~~~~~4
$ boxen --align=center --width=16 'Rainbows are so cool!'
┌──────────────┐
│ Rainbows are │
│ so cool! │
└──────────────┘
$ boxen --title='Yes it is' --title-alignment=center 'Is that a centered title?'
┌─────── Yes it is ───────┐
│Is that a centered title?│
└─────────────────────────┘
`, {
importMeta: import.meta,
flags: {
borderColor: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
borderStyle: {
type: 'string',
},
dimBorder: {
type: 'boolean',
},
padding: {
type: 'number',
},
margin: {
type: 'number',
},
center: {
type: 'boolean',
},
align: {
type: 'string',
},
title: {
type: 'string',
},
titleAlignment: {
type: 'string',
},
width: {
type: 'number',
},
height: {
type: 'number',
},
fullscreen: {
type: 'boolean',
},
},
});
const {input} = cli;
function cleanupBorderStyle(borderStyle) {
if (!borderStyle) {
return 'single';
}
if (borderStyle in _borderStyles) {
return borderStyle;
}
// Convert old borderStyle format (6 characters) to new one (8 characters)
if (borderStyle.length === 6) {
borderStyle = borderStyle.slice(0, 5) + borderStyle[4] + borderStyle[5].repeat(2);
}
// A string of 8 characters was given, make it a borderStyle object
if (borderStyle.length === 8) {
return {
topLeft: borderStyle[0],
topRight: borderStyle[1],
bottomLeft: borderStyle[2],
bottomRight: borderStyle[3],
top: borderStyle[4],
bottom: borderStyle[5],
left: borderStyle[6],
right: borderStyle[7],
};
}
console.error('Specified custom border style is invalid');
process.exit(1);
}
function parseMargin(options) {
if (!options.margin) {
return;
}
if (options.center) {
return {
top: options.margin,
bottom: options.margin,
};
}
return options.margin;
}
function calculateBoxLength(box, options) {
const lineNumber = options.margin ? options.margin.top || options.margin : 0;
return box.split('\n')[lineNumber].length;
}
function init(data) {
cli.flags.borderStyle = cleanupBorderStyle(cli.flags.borderStyle);
cli.flags.margin = parseMargin(cli.flags);
let box = boxen(data, cli.flags);
if (cli.flags.center) {
const boxLength = calculateBoxLength(box, cli.flags);
box = indentString(box, (process.stdout.columns - boxLength) / 2);
}
console.log(box);
}
if (input.length === 0 && process.stdin.isTTY) {
console.error('Specify a string');
process.exit(1);
}
(async () => {
if (input.length > 0) {
init(input.join(' '));
} else {
const stdin = await getStdin();
init(stdin.replace(/\n$/, ''));
}
})();