|
| 1 | +/* eslint-disable no-bitwise */ |
| 2 | +/* eslint-disable prefer-template */ |
| 3 | + |
| 4 | +// ISC License |
| 5 | + |
| 6 | +// Copyright (c) 2021 Alexey Raspopov, Kostiantyn Denysov, Anton Verinov |
| 7 | + |
| 8 | +// Permission to use, copy, modify, and/or distribute this software for any |
| 9 | +// purpose with or without fee is hereby granted, provided that the above |
| 10 | +// copyright notice and this permission notice appear in all copies. |
| 11 | + |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | +// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | +// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | +// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | +// |
| 20 | +// https://github.com/alexeyraspopov/picocolors/blob/b6261487e7b81aaab2440e397a356732cad9e342/picocolors.js#L1 |
| 21 | + |
| 22 | +const { env, stdout } = globalThis?.process ?? {}; |
| 23 | + |
| 24 | +const enabled = |
| 25 | + env && |
| 26 | + !env.NO_COLOR && |
| 27 | + (env.FORCE_COLOR || (stdout?.isTTY && !env.CI && env.TERM !== 'dumb')); |
| 28 | + |
| 29 | +const replaceClose = ( |
| 30 | + str: string, |
| 31 | + close: string, |
| 32 | + replace: string, |
| 33 | + index: number, |
| 34 | +): string => { |
| 35 | + const start = str.substring(0, index) + replace; |
| 36 | + const end = str.substring(index + close.length); |
| 37 | + const nextIndex = end.indexOf(close); |
| 38 | + return ~nextIndex |
| 39 | + ? start + replaceClose(end, close, replace, nextIndex) |
| 40 | + : start + end; |
| 41 | +}; |
| 42 | + |
| 43 | +const formatter = |
| 44 | + (open: string, close: string, replace = open) => |
| 45 | + (input: string) => { |
| 46 | + const string = '' + input; |
| 47 | + const index = string.indexOf(close, open.length); |
| 48 | + return ~index |
| 49 | + ? open + replaceClose(string, close, replace, index) + close |
| 50 | + : open + string + close; |
| 51 | + }; |
| 52 | + |
| 53 | +export const reset = enabled ? (s: string) => `\x1b[0m${s}\x1b[0m` : String; |
| 54 | +export const bold = enabled |
| 55 | + ? formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m') |
| 56 | + : String; |
| 57 | +export const dim = enabled |
| 58 | + ? formatter('\x1b[2m', '\x1b[22m', '\x1b[22m\x1b[2m') |
| 59 | + : String; |
| 60 | +export const italic = enabled ? formatter('\x1b[3m', '\x1b[23m') : String; |
| 61 | +export const underline = enabled ? formatter('\x1b[4m', '\x1b[24m') : String; |
| 62 | +export const inverse = enabled ? formatter('\x1b[7m', '\x1b[27m') : String; |
| 63 | +export const hidden = enabled ? formatter('\x1b[8m', '\x1b[28m') : String; |
| 64 | +export const strikethrough = enabled |
| 65 | + ? formatter('\x1b[9m', '\x1b[29m') |
| 66 | + : String; |
| 67 | +export const black = enabled ? formatter('\x1b[30m', '\x1b[39m') : String; |
| 68 | +export const red = enabled ? formatter('\x1b[31m', '\x1b[39m') : String; |
| 69 | +export const green = enabled ? formatter('\x1b[32m', '\x1b[39m') : String; |
| 70 | +export const yellow = enabled ? formatter('\x1b[33m', '\x1b[39m') : String; |
| 71 | +export const blue = enabled ? formatter('\x1b[34m', '\x1b[39m') : String; |
| 72 | +export const magenta = enabled ? formatter('\x1b[35m', '\x1b[39m') : String; |
| 73 | +export const purple = enabled |
| 74 | + ? formatter('\x1b[38;2;173;127;168m', '\x1b[39m') |
| 75 | + : String; |
| 76 | +export const cyan = enabled ? formatter('\x1b[36m', '\x1b[39m') : String; |
| 77 | +export const white = enabled ? formatter('\x1b[37m', '\x1b[39m') : String; |
| 78 | +export const gray = enabled ? formatter('\x1b[90m', '\x1b[39m') : String; |
| 79 | +export const bgBlack = enabled ? formatter('\x1b[40m', '\x1b[49m') : String; |
| 80 | +export const bgRed = enabled ? formatter('\x1b[41m', '\x1b[49m') : String; |
| 81 | +export const bgGreen = enabled ? formatter('\x1b[42m', '\x1b[49m') : String; |
| 82 | +export const bgYellow = enabled ? formatter('\x1b[43m', '\x1b[49m') : String; |
| 83 | +export const bgBlue = enabled ? formatter('\x1b[44m', '\x1b[49m') : String; |
| 84 | +export const bgMagenta = enabled ? formatter('\x1b[45m', '\x1b[49m') : String; |
| 85 | +export const bgCyan = enabled ? formatter('\x1b[46m', '\x1b[49m') : String; |
| 86 | +export const bgWhite = enabled ? formatter('\x1b[47m', '\x1b[49m') : String; |
0 commit comments