Last active
April 18, 2021 03:20
-
-
Save sbrl/8c0bb5e172438b6e62dd48587cfeba84 to your computer and use it in GitHub Desktop.
[Ansi.mjs] VT100 ANSI escape sequence generator in JS. Ported from the C# version.
This file contains 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
"use strict"; | |
/** | |
* Generates various VT100 ANSI escape sequences. | |
* Ported from C#. | |
* @licence MPL-2.0 <https://www.mozilla.org/en-US/MPL/2.0/> | |
* @source https://gist.github.com/a4edd3204a03f4eedb79785751efb0f3#file-ansi-cs | |
* @author Starbeamrainbowlabs | |
* GitHub: @sbrl | Twitter: @SBRLabs | Reddit: u/Starbeamrainbowlabs | |
***** Changelog ***** | |
* 27th March 2019: | |
* - Initial public release | |
* 9th March 2020: | |
* - Add Italics (\u001b[3m]) | |
* - Export a new instance of it by default (makes it so that there's 1 global instance) | |
*/ | |
class Ansi { | |
constructor() { | |
/** | |
* Whether we should *actually* emit ANSI escape codes or not. | |
* Useful when we want to output to a log file, for example | |
* @type {Boolean} | |
*/ | |
this.enabled = true; | |
this.escape_codes(); | |
} | |
escape_codes() { | |
// Solution on how to output ANSI escape codes in C# from here: | |
// https://www.jerriepelser.com/blog/using-ansi-color-codes-in-net-console-apps | |
this.reset = this.enabled ? "\u001b[0m" : ""; | |
this.hicol = this.enabled ? "\u001b[1m" : ""; | |
this.locol = this.enabled ? "\u001b[2m" : ""; | |
this.italics = this.enabled ? "\u001b[3m" : ""; | |
this.underline = this.enabled ? "\u001b[4m" : ""; | |
this.inverse = this.enabled ? "\u001b[7m" : ""; | |
this.fblack = this.enabled ? "\u001b[30m" : ""; | |
this.fred = this.enabled ? "\u001b[31m" : ""; | |
this.fgreen = this.enabled ? "\u001b[32m" : ""; | |
this.fyellow = this.enabled ? "\u001b[33m" : ""; | |
this.fblue = this.enabled ? "\u001b[34m" : ""; | |
this.fmagenta = this.enabled ? "\u001b[35m" : ""; | |
this.fcyan = this.enabled ? "\u001b[36m" : ""; | |
this.fwhite = this.enabled ? "\u001b[37m" : ""; | |
this.bblack = this.enabled ? "\u001b[40m" : ""; | |
this.bred = this.enabled ? "\u001b[41m" : ""; | |
this.bgreen = this.enabled ? "\u001b[42m" : ""; | |
this.byellow = this.enabled ? "\u001b[43m" : ""; | |
this.bblue = this.enabled ? "\u001b[44m" : ""; | |
this.bmagenta = this.enabled ? "\u001b[45m" : ""; | |
this.bcyan = this.enabled ? "\u001b[46m" : ""; | |
this.bwhite = this.enabled ? "\u001b[47m" : ""; | |
} | |
// Thanks to http://ascii-table.com/ansi-escape-sequences.php for the following ANSI escape sequences | |
up(lines = 1) { | |
return this.enabled ? `\u001b[${lines}A` : ""; | |
} | |
down(lines = 1) { | |
return this.enabled ? `\u001b[${lines}B` : ""; | |
} | |
right(lines = 1) { | |
return this.enabled ? `\u001b[${lines}C` : ""; | |
} | |
left(lines = 1) { | |
return this.enabled ? `\u001b[${lines}D` : ""; | |
} | |
jump_to(x, y) { | |
return this.enabled ? `\u001b[${y};${x}H` : ""; | |
} | |
cursor_pos_save() { | |
return this.enabled ? `\u001b[s` : ""; | |
} | |
cursor_pos_restore() { | |
return this.enabled ? `\u001b[u` : ""; | |
} | |
} | |
export default (new Ansi()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment