UNPKG

@bevry/ansi

Version:

ANSI colors and styles for Deno, Node.js, JavaScript, and TypeScript

157 lines (156 loc) 7.33 kB
"use strict"; // Styles // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters Object.defineProperty(exports, "__esModule", { value: true }); exports.bgBrightWhite = exports.bgBrightCyan = exports.bgBrightMagenta = exports.bgBrightBlue = exports.bgBrightYellow = exports.bgBrightGreen = exports.bgBrightRed = exports.bgBrightBlack = exports.bgWhite = exports.bgCyan = exports.bgMagenta = exports.bgBlue = exports.bgYellow = exports.bgGreen = exports.bgRed = exports.bgBlack = exports.brightWhite = exports.brightCyan = exports.brightMagenta = exports.brightBlue = exports.brightYellow = exports.brightGreen = exports.brightRed = exports.brightBlack = exports.white = exports.cyan = exports.magenta = exports.blue = exports.yellow = exports.green = exports.red = exports.black = exports.overline = exports.encircle = exports.frame = exports.strikethrough = exports.crossout = exports.conceal = exports.inverse = exports.rapidBlink = exports.blink = exports.slowBlink = exports.underline = exports.italic = exports.dim = exports.bright = exports.faint = exports.bold = exports.reset = exports.Color = void 0; exports.style = style; exports.foregroundColor = foregroundColor; exports.backgroundColor = backgroundColor; // Colors // https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit var Color; (function (Color) { Color[Color["Black"] = 0] = "Black"; Color[Color["Red"] = 1] = "Red"; Color[Color["Green"] = 2] = "Green"; Color[Color["Yellow"] = 3] = "Yellow"; Color[Color["Blue"] = 4] = "Blue"; Color[Color["Magenta"] = 5] = "Magenta"; Color[Color["Cyan"] = 6] = "Cyan"; Color[Color["White"] = 7] = "White"; })(Color || (exports.Color = Color = {})); /** * A factory method to generate an ANSI style applier from the open and close codes * @param open - The ANSI open code for the style * @param close - The ANSI close code for the style * @returns A function that applies the ANSI style to a string */ function style(open, close) { return function (str) { return '\u001b[' + open + 'm' + str + '\u001b[' + close + 'm'; }; } /** * A factory method to generate an ANSI foreground color applier from the foreground color code * @param color The color to apply to the foreground * @param bright Whether to use the bright variant of the color * @returns A function that applies the ANSI foreground color to a string */ function foregroundColor(color, bright) { if (bright === void 0) { bright = false; } // 30-37, 38 return style((bright ? 90 : 30) + color, 39); } /** * A factory method to generate an ANSI background color applier from the background color code * @param color The color to apply to the background * @param bright Whether to use the bright variant of the color * @returns A function that applies the ANSI background color to a string */ function backgroundColor(color, bright) { if (bright === void 0) { bright = false; } // 40-47, 48 return style((bright ? 100 : 40) + color, 49); } // /** A factory method to generate an ANSI underline color applier from the underline color code */ // export function underlineColor(open: number) { // // 58, 59 // // requires a different style to set the colour though, which needs to be figured out // return style(open, 59) // } /** Make the text look normal */ exports.reset = style(0, 22); /** Make the text bold, or with increased intensity */ exports.bold = style(1, 22); /** Make the text faint, or with decreased intensity */ exports.faint = style(2, 22); /** Make the text bold, or with increased intensity */ exports.bright = exports.bold; /** Make the text faint, or with decreased intensity */ exports.dim = exports.faint; /** Make the text italicized */ exports.italic = style(3, 23); /** Make the text underlined */ exports.underline = style(4, 24); /** Make the text slow blink */ exports.slowBlink = style(5, 25); /** Make the text slow blink */ exports.blink = exports.slowBlink; /** Make the text rapid blink */ exports.rapidBlink = style(6, 25); /** Invert the colors of the text */ exports.inverse = style(7, 27); /** Make the text concealed */ exports.conceal = style(8, 28); /** Make the text crossed-out */ exports.crossout = style(9, 29); /** Make the text crossed-out */ exports.strikethrough = exports.crossout; /** Make the text framed */ exports.frame = style(51, 54); /** Make the text encircled */ exports.encircle = style(52, 54); /** Make the text overlined */ exports.overline = style(53, 55); /** Make the text black */ exports.black = foregroundColor(Color.Black); /** Make the text red */ exports.red = foregroundColor(Color.Red); /** Make the text green */ exports.green = foregroundColor(Color.Green); /** Make the text yellow */ exports.yellow = foregroundColor(Color.Yellow); /** Make the text blue */ exports.blue = foregroundColor(Color.Blue); /** Make the text magenta */ exports.magenta = foregroundColor(Color.Magenta); /** Make the text cyan */ exports.cyan = foregroundColor(Color.Cyan); /** Make the text white */ exports.white = foregroundColor(Color.White); /** Make the text bright black */ exports.brightBlack = foregroundColor(Color.Black, true); /** Make the text bright red */ exports.brightRed = foregroundColor(Color.Red, true); /** Make the text bright green */ exports.brightGreen = foregroundColor(Color.Green, true); /** Make the text bright yellow */ exports.brightYellow = foregroundColor(Color.Yellow, true); /** Make the text bright blue */ exports.brightBlue = foregroundColor(Color.Blue, true); /** Make the text bright magenta */ exports.brightMagenta = foregroundColor(Color.Magenta, true); /** Make the text bright cyan */ exports.brightCyan = foregroundColor(Color.Cyan, true); /** Make the text bright white */ exports.brightWhite = foregroundColor(Color.White, true); /** Make the text's background black */ exports.bgBlack = backgroundColor(Color.Black); /** Make the text's background red */ exports.bgRed = backgroundColor(Color.Red); /** Make the text's background green */ exports.bgGreen = backgroundColor(Color.Green); /** Make the text's background yellow */ exports.bgYellow = backgroundColor(Color.Yellow); /** Make the text's background blue */ exports.bgBlue = backgroundColor(Color.Blue); /** Make the text's background magenta */ exports.bgMagenta = backgroundColor(Color.Magenta); /** Make the text's background cyan */ exports.bgCyan = backgroundColor(Color.Cyan); /** Make the text's background white */ exports.bgWhite = backgroundColor(Color.White); /** Make the text's background bright black */ exports.bgBrightBlack = backgroundColor(Color.Black, true); /** Make the text's background bright red */ exports.bgBrightRed = backgroundColor(Color.Red, true); /** Make the text's background bright green */ exports.bgBrightGreen = backgroundColor(Color.Green, true); /** Make the text's background bright yellow */ exports.bgBrightYellow = backgroundColor(Color.Yellow, true); /** Make the text's background bright blue */ exports.bgBrightBlue = backgroundColor(Color.Blue, true); /** Make the text's background bright magenta */ exports.bgBrightMagenta = backgroundColor(Color.Magenta, true); /** Make the text's background bright cyan */ exports.bgBrightCyan = backgroundColor(Color.Cyan, true); /** Make the text's background bright white */ exports.bgBrightWhite = backgroundColor(Color.White, true);