forked from unjs/consola
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
17 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,6 +293,9 @@ import { | |
leftAlign, | ||
align, | ||
box, | ||
colors, | ||
getColor, | ||
colorize, | ||
} from "consola/utils"; | ||
|
||
// CommonJS | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from "./utils/box"; | ||
|
||
export * from "./utils/color"; | ||
export { | ||
stripAnsi, | ||
centerAlign, | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Based on https://github.com/jorgebucaran/colorette | ||
* Read LICENSE file for more information | ||
* https://github.com/jorgebucaran/colorette/blob/20fc196d07d0f87c61e0256eadd7831c79b24108/index.js | ||
*/ | ||
|
||
import * as tty from "node:tty"; | ||
|
||
// TODO: Migrate to std-env | ||
const { | ||
env = {}, | ||
argv = [], | ||
platform = "", | ||
} = typeof process === "undefined" ? {} : process; | ||
const isDisabled = "NO_COLOR" in env || argv.includes("--no-color"); | ||
const isForced = "FORCE_COLOR" in env || argv.includes("--color"); | ||
const isWindows = platform === "win32"; | ||
const isDumbTerminal = env.TERM === "dumb"; | ||
const isCompatibleTerminal = | ||
tty && tty.isatty && tty.isatty(1) && env.TERM && !isDumbTerminal; | ||
const isCI = | ||
"CI" in env && | ||
("GITHUB_ACTIONS" in env || "GITLAB_CI" in env || "CIRCLECI" in env); | ||
const isColorSupported = | ||
!isDisabled && | ||
(isForced || (isWindows && !isDumbTerminal) || isCompatibleTerminal || isCI); | ||
|
||
function replaceClose( | ||
index: number, | ||
string: string, | ||
close: string, | ||
replace: string, | ||
head = string.slice(0, Math.max(0, index)) + replace, | ||
tail = string.slice(Math.max(0, index + close.length)), | ||
next = tail.indexOf(close) | ||
): string { | ||
return head + (next < 0 ? tail : replaceClose(next, tail, close, replace)); | ||
} | ||
|
||
function clearBleed( | ||
index: number, | ||
string: string, | ||
open: string, | ||
close: string, | ||
replace: string | ||
) { | ||
return index < 0 | ||
? open + string + close | ||
: open + replaceClose(index, string, close, replace) + close; | ||
} | ||
|
||
function filterEmpty( | ||
open: string, | ||
close: string, | ||
replace = open, | ||
at = open.length + 1 | ||
) { | ||
return (string: string) => | ||
string || !(string === "" || string === undefined) | ||
? clearBleed( | ||
("" + string).indexOf(close, at), | ||
string, | ||
open, | ||
close, | ||
replace | ||
) | ||
: ""; | ||
} | ||
|
||
function init(open: number, close: number, replace?: string) { | ||
return filterEmpty(`\u001B[${open}m`, `\u001B[${close}m`, replace); | ||
} | ||
|
||
const colorDefs = { | ||
reset: init(0, 0), | ||
bold: init(1, 22, "\u001B[22m\u001B[1m"), | ||
dim: init(2, 22, "\u001B[22m\u001B[2m"), | ||
italic: init(3, 23), | ||
underline: init(4, 24), | ||
inverse: init(7, 27), | ||
hidden: init(8, 28), | ||
strikethrough: init(9, 29), | ||
black: init(30, 39), | ||
red: init(31, 39), | ||
green: init(32, 39), | ||
yellow: init(33, 39), | ||
blue: init(34, 39), | ||
magenta: init(35, 39), | ||
cyan: init(36, 39), | ||
white: init(37, 39), | ||
gray: init(90, 39), | ||
bgBlack: init(40, 49), | ||
bgRed: init(41, 49), | ||
bgGreen: init(42, 49), | ||
bgYellow: init(43, 49), | ||
bgBlue: init(44, 49), | ||
bgMagenta: init(45, 49), | ||
bgCyan: init(46, 49), | ||
bgWhite: init(47, 49), | ||
blackBright: init(90, 39), | ||
redBright: init(91, 39), | ||
greenBright: init(92, 39), | ||
yellowBright: init(93, 39), | ||
blueBright: init(94, 39), | ||
magentaBright: init(95, 39), | ||
cyanBright: init(96, 39), | ||
whiteBright: init(97, 39), | ||
bgBlackBright: init(100, 49), | ||
bgRedBright: init(101, 49), | ||
bgGreenBright: init(102, 49), | ||
bgYellowBright: init(103, 49), | ||
bgBlueBright: init(104, 49), | ||
bgMagentaBright: init(105, 49), | ||
bgCyanBright: init(106, 49), | ||
bgWhiteBright: init(107, 49), | ||
}; | ||
|
||
export type ColorName = keyof typeof colorDefs; | ||
export type ColorFunction = (text: string | number) => string; | ||
|
||
function createColors(useColor = isColorSupported) { | ||
return useColor | ||
? colorDefs | ||
: Object.fromEntries(Object.keys(colorDefs).map((key) => [key, String])); | ||
} | ||
|
||
export const colors = createColors() as Record<ColorName, ColorFunction>; | ||
|
||
export function getColor( | ||
color: ColorName, | ||
fallback: ColorName = "reset" | ||
): ColorFunction { | ||
return colors[color] || colors[fallback]; | ||
} | ||
|
||
export function colorize(color: ColorName, text: string | number): string { | ||
return getColor(color)(text); | ||
} |
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