Skip to content

Instantly share code, notes, and snippets.

@brunnerh
Last active November 3, 2018 15:25
Show Gist options
  • Save brunnerh/9197583787962d5c3b781111333faa19 to your computer and use it in GitHub Desktop.
Save brunnerh/9197583787962d5c3b781111333faa19 to your computer and use it in GitHub Desktop.
/**
* Creates a basic ANSI escape code string from a number.
* @param {number} x Numeric ANSI escape code.
*/
const ansi = x => `\x1b[${x}m`;
/**
* Adds ANSI escape codes to a string.
* Resets properties at the end of the string.
* @param {string} text Text to colorize.
* @param {string[]} modifiers ANSI escape code modifiers.
*/
const ansiModify = (text, ...modifiers) => modifiers.join('') + text + ansi(0);
/** ANSI modifiers. */
const ANSI = {
Reset: ansi(0),
Bold: ansi(1),
Faint: ansi(2),
Italic: ansi(3),
Underline: ansi(4),
BlinkSlow: ansi(5),
BlinkRapid: ansi(6),
/** Swaps foreground and background colors. */
ReverseVideo: ansi(7),
Conceal: ansi(8),
CrossedOut: ansi(9),
PrimaryFont: ansi(10),
AlternativeFont1: ansi(11),
AlternativeFont2: ansi(12),
AlternativeFont3: ansi(13),
AlternativeFont4: ansi(14),
AlternativeFont5: ansi(15),
AlternativeFont6: ansi(16),
AlternativeFont7: ansi(17),
AlternativeFont8: ansi(18),
AlternativeFont9: ansi(19),
Fraktur: ansi(20),
DoublyUnderline: ansi(21),
NormalColorOrIntensity: ansi(22),
NotItalicOrFraktur: ansi(23),
UnderlineOff: ansi(24),
BlinkOff: ansi(25),
InverseOff: ansi(27),
Reveal: ansi(28),
NotCrossedOut: ansi(29),
Framed: ansi(51),
Encircled: ansi(52),
Overlined: ansi(53),
NotFramedOrEncircled: ansi(54),
NotOverlined: ansi(55),
IdeogramUnderline: ansi(60),
IdeogramDoubleUnderline: ansi(61),
IdeogramOverline: ansi(62),
IdeogramDoubleOverline: ansi(63),
IdeogramStressMarking: ansi(64),
IdeogramAttributesOff: ansi(65),
FgBlack: ansi(30),
FgRed: ansi(31),
FgGreen: ansi(32),
FgYellow: ansi(33),
FgBlue: ansi(34),
FgMagenta: ansi(35),
FgCyan: ansi(36),
FgWhite: ansi(37),
FgBrightBlack: ansi(90),
FgBrightRed: ansi(91),
FgBrightGreen: ansi(92),
FgBrightYellow: ansi(93),
FgBrightBlue: ansi(94),
FgBrightMagenta: ansi(95),
FgBrightCyan: ansi(96),
FgBrightWhite: ansi(97),
/**
* Creates an RGB modifier for the foreground.
* @param {number} r Red channel value (0 to 255).
* @param {number} g Green channel value (0 to 255).
* @param {number} b Blue channel value (0 to 255).
*/
FgRgb: (r, g, b) => `\x1b[38;2;${r};${g};${b}m`,
FgDefault: ansi(39),
BgBlack: ansi(40),
BgRed: ansi(41),
BgGreen: ansi(42),
BgYellow: ansi(43),
BgBlue: ansi(44),
BgMagenta: ansi(45),
BgCyan: ansi(46),
BgWhite: ansi(47),
BgBrightBlack: ansi(100),
BgBrightRed: ansi(101),
BgBrightGreen: ansi(102),
BgBrightYellow: ansi(103),
BgBrightBlue: ansi(104),
BgBrightMagenta: ansi(105),
BgBrightCyan: ansi(106),
BgBrightWhite: ansi(107),
/**
* Creates an RGB modifier for the background.
* @param {number} r Red channel value (0 to 255).
* @param {number} g Green channel value (0 to 255).
* @param {number} b Blue channel value (0 to 255).
*/
BgRgb: (r, g, b) => `\x1b[48;2;${r};${g};${b}m`,
BgDefault: ansi(49),
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment