Unverified Commit 817a2402 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

refactor: simplify the call in lib.picocolors (#60386)

Simplify the conditional formatter call in `lib/picocolors`

Closes NEXT-2018

---------
Co-authored-by: 's avatarSteven <steven@ceriously.com>
parent 94b8af22
......@@ -37,45 +37,40 @@ const replaceClose = (
: start + end
}
const formatter =
(open: string, close: string, replace = open) =>
(input: string) => {
const formatter = (open: string, close: string, replace = open) => {
if (!enabled) return String
return (input: string) => {
const string = '' + input
const index = string.indexOf(close, open.length)
return ~index
? open + replaceClose(string, close, replace, index) + close
: open + string + close
}
}
export const reset = enabled ? (s: string) => `\x1b[0m${s}\x1b[0m` : String
export const bold = enabled
? formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m')
: String
export const dim = enabled
? formatter('\x1b[2m', '\x1b[22m', '\x1b[22m\x1b[2m')
: String
export const italic = enabled ? formatter('\x1b[3m', '\x1b[23m') : String
export const underline = enabled ? formatter('\x1b[4m', '\x1b[24m') : String
export const inverse = enabled ? formatter('\x1b[7m', '\x1b[27m') : String
export const hidden = enabled ? formatter('\x1b[8m', '\x1b[28m') : String
export const strikethrough = enabled ? formatter('\x1b[9m', '\x1b[29m') : String
export const black = enabled ? formatter('\x1b[30m', '\x1b[39m') : String
export const red = enabled ? formatter('\x1b[31m', '\x1b[39m') : String
export const green = enabled ? formatter('\x1b[32m', '\x1b[39m') : String
export const yellow = enabled ? formatter('\x1b[33m', '\x1b[39m') : String
export const blue = enabled ? formatter('\x1b[34m', '\x1b[39m') : String
export const magenta = enabled ? formatter('\x1b[35m', '\x1b[39m') : String
export const purple = enabled
? formatter('\x1b[38;2;173;127;168m', '\x1b[39m')
: String
export const cyan = enabled ? formatter('\x1b[36m', '\x1b[39m') : String
export const white = enabled ? formatter('\x1b[37m', '\x1b[39m') : String
export const gray = enabled ? formatter('\x1b[90m', '\x1b[39m') : String
export const bgBlack = enabled ? formatter('\x1b[40m', '\x1b[49m') : String
export const bgRed = enabled ? formatter('\x1b[41m', '\x1b[49m') : String
export const bgGreen = enabled ? formatter('\x1b[42m', '\x1b[49m') : String
export const bgYellow = enabled ? formatter('\x1b[43m', '\x1b[49m') : String
export const bgBlue = enabled ? formatter('\x1b[44m', '\x1b[49m') : String
export const bgMagenta = enabled ? formatter('\x1b[45m', '\x1b[49m') : String
export const bgCyan = enabled ? formatter('\x1b[46m', '\x1b[49m') : String
export const bgWhite = enabled ? formatter('\x1b[47m', '\x1b[49m') : String
export const bold = formatter('\x1b[1m', '\x1b[22m', '\x1b[22m\x1b[1m')
export const dim = formatter('\x1b[2m', '\x1b[22m', '\x1b[22m\x1b[2m')
export const italic = formatter('\x1b[3m', '\x1b[23m')
export const underline = formatter('\x1b[4m', '\x1b[24m')
export const inverse = formatter('\x1b[7m', '\x1b[27m')
export const hidden = formatter('\x1b[8m', '\x1b[28m')
export const strikethrough = formatter('\x1b[9m', '\x1b[29m')
export const black = formatter('\x1b[30m', '\x1b[39m')
export const red = formatter('\x1b[31m', '\x1b[39m')
export const green = formatter('\x1b[32m', '\x1b[39m')
export const yellow = formatter('\x1b[33m', '\x1b[39m')
export const blue = formatter('\x1b[34m', '\x1b[39m')
export const magenta = formatter('\x1b[35m', '\x1b[39m')
export const purple = formatter('\x1b[38;2;173;127;168m', '\x1b[39m')
export const cyan = formatter('\x1b[36m', '\x1b[39m')
export const white = formatter('\x1b[37m', '\x1b[39m')
export const gray = formatter('\x1b[90m', '\x1b[39m')
export const bgBlack = formatter('\x1b[40m', '\x1b[49m')
export const bgRed = formatter('\x1b[41m', '\x1b[49m')
export const bgGreen = formatter('\x1b[42m', '\x1b[49m')
export const bgYellow = formatter('\x1b[43m', '\x1b[49m')
export const bgBlue = formatter('\x1b[44m', '\x1b[49m')
export const bgMagenta = formatter('\x1b[45m', '\x1b[49m')
export const bgCyan = formatter('\x1b[46m', '\x1b[49m')
export const bgWhite = formatter('\x1b[47m', '\x1b[49m')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment