main
|
Last change
on this file was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
690 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Encodes a string in a colour: red, yellow or green
|
|---|
| 3 | * @param {String} c colour to highlight in
|
|---|
| 4 | * @param {String} str the string to encode
|
|---|
| 5 | * @return {String} coloured string for terminal printing
|
|---|
| 6 | */
|
|---|
| 7 | function colour(c, str) {
|
|---|
| 8 | return (colour[c] || colour.black) + str + colour.black;
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | function strip(str) {
|
|---|
| 12 | re.lastIndex = 0; // reset position
|
|---|
| 13 | return str.replace(re, '');
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | colour.red = '\x1B[31m';
|
|---|
| 17 | colour.yellow = '\x1B[33m';
|
|---|
| 18 | colour.green = '\x1B[32m';
|
|---|
| 19 | colour.black = '\x1B[39m';
|
|---|
| 20 |
|
|---|
| 21 | var reStr = Object.keys(colour).map(key => colour[key]).join('|');
|
|---|
| 22 | var re = new RegExp(('(' + reStr + ')').replace(/\[/g, '\\['), 'g');
|
|---|
| 23 |
|
|---|
| 24 | colour.strip = strip;
|
|---|
| 25 |
|
|---|
| 26 | module.exports = colour;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.