Created
May 12, 2020 09:51
-
-
Save PotOfCoffee2Go/08acf06e249907ff0a9431fc7b1c19cb to your computer and use it in GitHub Desktop.
Linux bash color console in nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Allows coloring linux console text with nodejs. | |
Color codes are loaded into a table of 8 rows | |
with each row containing 64 columns. | |
Examples: | |
Save this file as 'colorconsole.js' | |
then run 'node ./colorconsole.js' | |
Usage: | |
let cc = new ColorConsole; | |
cc.etx(156); // set default color | |
cc.stx(3).write('brown on yellow').nl() | |
.write('now change midstream ') | |
.stx(316).write('italics') | |
.stx(3).write(' and back.') | |
.nl().nl(); | |
cc.log('log() does auto new-line!'); | |
API: | |
All functions can be chained. | |
Functions starting with _ are used internally. | |
// Create a color console. | |
let cc = new ColorConsole; | |
// Sets current color code | |
cc.stx(r, c) | |
// Sets default color code | |
cc.etx(r, c) | |
// Write a new-line, handy when using write() | |
cc.nl() | |
// Write the text unformatted in current color | |
cc.write('text') | |
// Log the text formatted via console.log() | |
cc.log('text') | |
Description: | |
The current color is set and remains active until | |
is set again with the start-of-text stx() function. | |
Text given in write() and log() are color coded with | |
the current color until a new stx() is called. | |
Each text block is terminated with the color code | |
set by the end-of-text etx() function, which is | |
effectively the default color. | |
Color codes are in a table that is referenced by | |
stx(r, c) and etx(r, c) | |
r = row 0 - 7 | |
c = column 0 - 63 | |
or | |
r = rcc where r=row and cc = column | |
The functions to write text are : | |
write(text) and log(text). | |
text = text to display | |
write() bypasses text formating | |
log() formats text via nodejs console.log() | |
The Test class displays handy charts which show the | |
text colors for row,col or rcc color code references. | |
*/ | |
// Linux bash color console | |
class ColorConsole { | |
constructor(stx = 156, etx = 156) { | |
this.ESC = String.fromCharCode(27); | |
this.NL = String.fromCharCode(10); | |
this.colors; | |
this._createColors(); | |
this.ccode = { stx: '', etx: '' }; | |
this._coord(stx, null, true); | |
this._coord(etx, null, false); | |
} | |
// Build table of the color codes | |
_createColors() { | |
let codes = []; | |
for (let x=0; x<8; x++) { | |
let code = []; | |
for (let i=30; i<38; i++) { | |
for (let a=40; a<48; a++) { | |
code.push(`[${x};${i};${a}m`) | |
} | |
} | |
codes.push(code); | |
} | |
this.colors = codes; | |
return this; | |
} | |
// Allows single number to reference color codes | |
// ex: r can be 156 which becomes r=1 c=56 | |
_coord(r, c, toStx) { | |
if (typeof c !== 'number') { | |
let x = r; r = Math.floor(x/100); c = x-(r*100); | |
} | |
if (toStx) { | |
this.ccode.stx = this.ESC + this.colors[r][c]; | |
} | |
else { | |
this.ccode.etx = this.ESC + this.colors[r][c]; | |
} | |
return this; | |
} | |
// Set start-of-text code by ref or row/col | |
stx(r, c) { return this._coord(r, c, true); } | |
// Set end--of-text code by ref or row/col | |
etx(r, c) { return this._coord(r, c, false); } | |
// Write a new-line | |
nl() { | |
process.stdout.write(this.ccode.etx + this.NL + this.ccode.etx); | |
return this; | |
} | |
// Write the text unformatted in current color | |
write(text) { | |
process.stdout.write(this.ccode.stx + text + this.ccode.etx); | |
return this; | |
} | |
// Log the text formatted in the current color | |
log(text) { | |
console.log(this.ccode.stx + text + this.ccode.etx); | |
return this; | |
} | |
} | |
// ------ | |
class Test { | |
constructor(cc = new ColorConsole) { this.cc = cc; } | |
// show list of console color codes | |
list() { console.log(this.cc.colors); } | |
// Show row/col chart on console | |
chart() { | |
console.log('\nBy row/column:'); | |
for (let r=0; r<8; r++) { // row 0 - 7 | |
this.cc.nl(); | |
for (let c=0; c<64; c++) { // col 0 - 63 | |
if (!(c % 8)) { this.cc.nl(); } // new-line every 8 codes | |
// display the table row/col's format | |
// followed by a normal space | |
let ca = /..$/.exec('0' + c); // right 2 chars | |
this.cc.stx(r, c).write(' ' + r + ',' + ca + ' ') | |
.stx(56).write(' '); | |
} | |
} | |
this.cc.nl().nl(); | |
} | |
// Show single number reference chart on console | |
chart2() { | |
console.log('\nBy single reference:'); | |
for (let r=0; r<800; r+=100) { // row 0 - 7 | |
this.cc.nl(); | |
for (let c=0; c<64; c++) { // col 0 - 63 | |
if (!(c % 8)) { this.cc.write(' ').nl(); } // new-line every 8 codes | |
let ca = /...$/.exec(' ' + String(r+c)); // right 3 chars | |
// display the table single reference format | |
// followed by a normal space | |
this.cc.stx(r+c).write(' ' + ca + ' ') | |
.stx(0, 56).write(' '); | |
} | |
} | |
this.cc.nl().nl(); | |
} | |
} | |
let test = new Test; | |
test.chart(); // Access by row/col ex: (3, 45, 'text') | |
test.chart2(); // Access by single ref ex: (345,'text') | |
test.cc.stx(3).write('old yellow/brown dog').nl(); | |
let f = 'formatted'; | |
test.cc.stx(0, 5).log(`this is ${f}`); | |
test.cc.log('Uses current color code'); | |
test.cc.stx(4).log('this is just a line'); | |
// Usage examples | |
let cc = new ColorConsole; | |
cc.etx(156); // set default color | |
cc.stx(3).write('brown on yellow').nl() | |
.write('now change midstream ') | |
.stx(316).write('italics') | |
.stx(3).write(' and back.') | |
.nl().nl(); | |
cc.log('log() does auto new-line!'); | |
// test.list(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment