Debian Codegraph @

cli-color/README.md

raw
1 [![Build status][build-image]][build-url]
2 [![Tests coverage][cov-image]][cov-url]
3 [![npm version][npm-image]][npm-url]
4
5 # cli-color
6
7 ## Yet another colors and formatting for the console solution
8
9 Colors, formatting and other goodies for the console. This package won't mess with built-ins and provides neat way to predefine formatting patterns, see below.
10
11 ## Installation
12
13 $ npm install cli-color
14
15 ## Usage
16
17 Usage:
18
19 ```javascript
20 var clc = require("cli-color");
21 ```
22
23 Output colored text:
24
25 ```javascript
26 console.log(clc.red("Text in red"));
27 ```
28
29 Styles can be mixed:
30
31 ```javascript
32 console.log(clc.red.bgWhite.underline("Underlined red text on white background."));
33 ```
34
35 Styled text can be mixed with unstyled:
36
37 ```javascript
38 console.log(clc.red("red") + " plain " + clc.blue("blue"));
39 ```
40
41 Styled text can be nested:
42
43 ```javascript
44 console.log(clc.red("red " + clc.blue("blue") + " red"));
45 ```
46
47 **Best way is to predefine needed stylings and then use it**:
48
49 ```javascript
50 var error = clc.red.bold;
51 var warn = clc.yellow;
52 var notice = clc.blue;
53
54 console.log(error("Error!"));
55 console.log(warn("Warning"));
56 console.log(notice("Notice"));
57 ```
58
59 _Note: No colors or styles are output when [`NO_COLOR` env var](https://no-color.org/) is set_
60
61 Supported are all ANSI colors and styles:
62
63 #### Styles
64
65 Styles will display correctly if font used in your console supports them.
66
67 - bold
68 - italic
69 - underline
70 - blink
71 - inverse
72 - strike
73
74 #### Colors
75
76 <table>
77 <thead><th>Foreground</th><th>Background</th><th></th></thead>
78 <tbody>
79 <tr><td>black</td><td>bgBlack</td><td><img src="http://medyk.org/colors/000000.png" width="30" height="30" /></td></tr>
80 <tr><td>red</td><td>bgRed</td><td><img src="http://medyk.org/colors/800000.png" width="30" height="30" /></td></tr>
81 <tr><td>green</td><td>bgGreen</td><td><img src="http://medyk.org/colors/008000.png" width="30" height="30" /></td></tr>
82 <tr><td>yellow</td><td>bgYellow</td><td><img src="http://medyk.org/colors/808000.png" width="30" height="30" /></td></tr>
83 <tr><td>blue</td><td>bgBlue</td><td><img src="http://medyk.org/colors/000080.png" width="30" height="30" /></td></tr>
84 <tr><td>magenta</td><td>bgMagenta</td><td><img src="http://medyk.org/colors/800080.png" width="30" height="30" /></td></tr>
85 <tr><td>cyan</td><td>bgCyan</td><td><img src="http://medyk.org/colors/008080.png" width="30" height="30" /></td></tr>
86 <tr><td>white</td><td>bgWhite</td><td><img src="http://medyk.org/colors/c0c0c0.png" width="30" height="30" /></td></tr>
87 </tbody>
88 </table>
89
90 ##### Bright variants
91
92 <table>
93 <thead><th>Foreground</th><th>Background</th><th></th></thead>
94 <tbody>
95 <tr><td>blackBright</td><td>bgBlackBright</td><td><img src="http://medyk.org/colors/808080.png" width="30" height="30" /></td></tr>
96 <tr><td>redBright</td><td>bgRedBright</td><td><img src="http://medyk.org/colors/ff0000.png" width="30" height="30" /></td></tr>
97 <tr><td>greenBright</td><td>bgGreenBright</td><td><img src="http://medyk.org/colors/00ff00.png" width="30" height="30" /></td></tr>
98 <tr><td>yellowBright</td><td>bgYellowBright</td><td><img src="http://medyk.org/colors/ffff00.png" width="30" height="30" /></td></tr>
99 <tr><td>blueBright</td><td>bgBlueBright</td><td><img src="http://medyk.org/colors/0000ff.png" width="30" height="30" /></td></tr>
100 <tr><td>magentaBright</td><td>bgMagentaBright</td><td><img src="http://medyk.org/colors/ff00ff.png" width="30" height="30" /></td></tr>
101 <tr><td>cyanBright</td><td>bgCyanBright</td><td><img src="http://medyk.org/colors/00ffff.png" width="30" height="30" /></td></tr>
102 <tr><td>whiteBright</td><td>bgWhiteBright</td><td><img src="http://medyk.org/colors/ffffff.png" width="30" height="30" /></td></tr>
103 </tbody>
104 </table>
105
106 ##### xTerm colors (256 colors table)
107
108 **Not supported on Windows and some terminals**. However if used in not supported environment, the closest color from basic (16 colors) palette is chosen.
109
110 Usage:
111
112 ```javascript
113 var msg = clc.xterm(202).bgXterm(236);
114 console.log(msg("Orange text on dark gray background"));
115 ```
116
117 Color table:
118
119 <img width="634" alt="Screenshot 2022-07-04 at 12 28 18" src="https://user-images.githubusercontent.com/122434/177136739-64a4bdd1-a1f5-453e-a7a0-55107bbd7922.png">
120
121 #### Reset
122
123 Terminal can be cleared with `clc.reset`
124
125 ```javascript
126 process.stdout.write(clc.reset);
127 ```
128
129 #### Erase
130
131 ##### clc.erase.screen
132
133 Entire screen
134
135 ```javascript
136 process.stdout.write(clc.erase.screen);
137 ```
138
139 ##### clc.erase.screenLeft
140
141 Left portion of a screen
142
143 ```javascript
144 process.stdout.write(clc.erase.screenLeft);
145 ```
146
147 ##### clc.erase.screenRight
148
149 Right portion of a screen
150
151 ```javascript
152 process.stdout.write(clc.erase.screenRight);
153 ```
154
155 ##### clc.erase.line
156
157 Current line
158
159 ```javascript
160 process.stdout.write(clc.erase.line);
161 ```
162
163 ##### clc.erase.lineRight
164
165 Right portion of current line
166
167 ```javascript
168 process.stdout.write(clc.erase.lineRight);
169 ```
170
171 ##### clc.erase.lineLeft
172
173 Left portion of current line
174
175 ```javascript
176 process.stdout.write(clc.erase.lineLeft);
177 ```
178
179 #### Move around functions
180
181 ##### clc.move(x, y)
182
183 Move cursor _x_ columns and _y_ rows away. Values can be positive or negative, e.g.:
184
185 ```javascript
186 process.stdout.write(clc.move(-2, -2)); // Move cursors two columns and two rows back
187 ```
188
189 ##### clc.move.to(x, y)
190
191 Absolute move. Sets cursor position at _x_ column and _y_ row
192
193 ```javascript
194 process.stdout.write(clc.move.to(0, 0)); // Move cursor to first row and first column in terminal window
195 ```
196
197 ##### clc.move.up(n)
198
199 Move cursor up _n_ rows
200
201 ```javascript
202 process.stdout.write(clc.move.up(2));
203 ```
204
205 ##### clc.move.down(n)
206
207 Move cursor down _n_ rows
208
209 ```javascript
210 process.stdout.write(clc.move.down(2));
211 ```
212
213 ##### clc.move.right(n)
214
215 Move cursor right _n_ columns
216
217 ```javascript
218 process.stdout.write(clc.move.right(2));
219 ```
220
221 ##### clc.move.left(n)
222
223 Move cursor left _n_ columns
224
225 ```javascript
226 process.stdout.write(clc.move.left(2));
227 ```
228
229 ##### clc.move.lines(n)
230
231 Move cursor `n` lines forward if `n` is positive, otherwise `n` lines backward, and place it at line beginning
232
233 ```javascript
234 process.stdout.write(clc.move.lines(2));
235 ```
236
237 ##### clc.move.top
238
239 Move cursor to top of a screen
240
241 ```javascript
242 process.stdout.write(clc.move.top);
243 ```
244
245 ##### clc.move.bottom
246
247 Move cursor to bottom of a screen
248
249 ```javascript
250 process.stdout.write(clc.move.bottom);
251 ```
252
253 ##### clc.move.lineBegin
254
255 Move cursor to begin of a line
256
257 ```javascript
258 process.stdout.write(clc.move.lineBegin);
259 ```
260
261 ##### clc.move.lineEnd
262
263 Move cursor to end of a line
264
265 ```javascript
266 process.stdout.write(clc.move.lineEnd);
267 ```
268
269 #### Terminal characteristics
270
271 ##### clc.windowSize.width
272
273 Returns terminal width
274
275 ##### clc.windowSize.height
276
277 Returns terminal height
278
279 ### Additional functionalities
280
281 #### clc.slice(str[, begin[, end]])
282
283 Slice provided string with preservation of eventual ANSI formatting
284
285 ```javascript
286 var clc = require("cli-color");
287
288 var str = clc.bold("foo") + "bar" + clc.red("elo");
289 var sliced = clc.slice(str, 1, 7); // Same as: clc.bold('oo') + 'bar' + clc.red('e')
290 ```
291
292 #### clc.strip(formatedText)
293
294 Strips ANSI formatted string to plain text
295
296 ```javascript
297 var ansiStrip = require("cli-color/strip");
298
299 var plain = ansiStrip(formatted);
300 ```
301
302 #### clc.getStrippedLength(str)
303
304 Get actual length of ANSI-formatted string
305
306 ```javascript
307 var clc = require("cli-color");
308
309 var str = clc.bold("foo") + "bar" + clc.red("elo");
310 clc.getStrippedLength(str); // 9
311 ```
312
313 #### clc.art(text, styleConf)
314
315 Create a text-graphical art. Within `styleConf`, string replacements needs to be defined, which are then used to convert `text` to styled graphical text.
316
317 ```javascript
318 var text = ".........\n" + ". Hello .\n" + ".........\n";
319 var style = { ".": clc.yellowBright("X") };
320
321 process.stdout.write(clc.art(text, style));
322 ```
323
324 #### clc.columns(data[, options])
325
326 Outputs aligned table of columns.
327
328 `data` is expected to be an array (or other iterable structure) of rows, where each row is also an array (or other iterable structure) of content to display.
329
330 Supported `options`:
331
332 - `sep`: Custom colums separator (defaults to `|`)
333 - `columns`: Per column customizations, as e.g. `[{ align: 'right' }, null, { align: 'left' }]`:
334 - `align`: Possible options: `'left'`, `'right` (efaults to `'left'`)
335
336 ```javascript
337 var clc = require("cli-color");
338
339 process.stdout.write(
340 clc.columns([
341 [clc.bold("First Name"), clc.bold("Last Name"), clc.bold("Age")],
342 ["John", "Doe", 34],
343 ["Martha", "Smith", 20],
344 ["Jan", "Kowalski", 30]
345 ])
346 );
347
348 /* Outputs:
349
350 First Name | Last Name | Age
351 John | Doe | 34
352 Martha | Smith | 20
353 Jan | Kowalski | 30
354 */
355 ```
356
357 ##### throbber(write, interval[, format])
358
359 Writes throbber string to _write_ function at given _interval_. Optionally throbber output can be formatted with given _format_ function
360
361 ```javascript
362 var setupThrobber = require("cli-color/throbber");
363
364 var throbber = setupThrobber(function (str) { process.stdout.write(str); }, 200);
365
366 throbber.start();
367
368 // at any time you can stop/start throbber
369 throbber.stop();
370 ```
371
372 ## Tests
373
374 $ npm test
375
376 ## Security contact information
377
378 To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
379
380 ## Contributors
381
382 - [@rentalhost](https://github.com/rentalhost) (David Rodrigues)
383 - Help with support for nested styles. Introduction of `clc.art` module, and significant improvements to tests coverage
384 - [@StreetStrider](https://github.com/StreetStrider)
385 - Implementation of sophistcated `clc.slice` functionality, and introduction of `clc.getStrippedLength` utility
386
387 [nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/cli-color/branches/master/shields_badge.svg
388 [nix-build-url]: https://semaphoreci.com/medikoo-org/cli-color
389 [win-build-image]: https://ci.appveyor.com/api/projects/status/mnd4catkeu181ll5?svg=true
390 [win-build-url]: https://ci.appveyor.com/project/medikoo/cli-color
391 [transpilation-image]: https://img.shields.io/badge/transpilation-free-brightgreen.svg
392 [npm-image]: https://img.shields.io/npm/v/cli-color.svg
393 [npm-url]: https://www.npmjs.com/package/cli-color
394
395 ---
396
397 <div align="center">
398 <b>
399 <a href="https://tidelift.com/subscription/pkg/npm-cli-color?utm_source=npm-cli-color&utm_medium=referral&utm_campaign=readme">Get professional support for cli-color with a Tidelift subscription</a>
400 </b>
401 <br>
402 <sub>
403 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
404 </sub>
405 </div>
406
407 [build-image]: https://github.com/medikoo/cli-color/workflows/Integrate/badge.svg
408 [build-url]: https://github.com/medikoo/cli-color/actions?query=workflow%3AIntegrate
409 [cov-image]: https://img.shields.io/codecov/c/github/medikoo/cli-color.svg
410 [cov-url]: https://codecov.io/gh/medikoo/cli-color
411 [npm-image]: https://img.shields.io/npm/v/cli-color.svg
412 [npm-url]: https://www.npmjs.com/package/cli-color