From 79e9185fd637e75301034d197730fee6f5b2afe1 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Mon, 10 Oct 2022 14:41:45 +0800 Subject: [PATCH] Sync code --- source/index.d.ts | 48 ++++++---------------------- source/index.js | 12 ++++--- source/index.test-d.ts | 18 +++++++++-- source/vendor/ansi-styles/index.d.ts | 48 +++++++++++++++++++++++++--- source/vendor/ansi-styles/index.js | 7 ++-- 5 files changed, 80 insertions(+), 53 deletions(-) diff --git a/source/index.d.ts b/source/index.d.ts index b0acc0f..dd249f5 100644 --- a/source/index.d.ts +++ b/source/index.d.ts @@ -1,45 +1,9 @@ // TODO: Make it this when TS suports that. +// import {ModifierName as Modifiers, ForegroundColorName as ForegroundColor, BackgroundColorName as BackgroundColor, ColorName as Color} from '#ansi-styles'; // import {ColorInfo, ColorSupportLevel} from '#supports-color'; +import {ModifierName as Modifiers, ForegroundColorName as ForegroundColor, BackgroundColorName as BackgroundColor, ColorName as Color} from './vendor/ansi-styles/index.js'; import {ColorInfo, ColorSupportLevel} from './vendor/supports-color/index.js'; -type BasicColor = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white'; -type BrightColor = `${BasicColor}Bright`; -type Grey = 'gray' | 'grey'; - -/** -Basic foreground colors. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ - -export type ForegroundColor = BasicColor | BrightColor | Grey; - -/** -Basic background colors. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type BackgroundColor = `bg${Capitalize}`; - -/** -Basic colors. - -[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) -*/ -export type Color = ForegroundColor | BackgroundColor; - -export type Modifiers = - | 'reset' - | 'bold' - | 'dim' - | 'italic' - | 'underline' - | 'overline' - | 'inverse' - | 'hidden' - | 'strikethrough' - | 'visible'; - export interface Options { /** Specify the color support for Chalk. @@ -277,6 +241,14 @@ export const supportsColor: ColorInfo; export const chalkStderr: typeof chalk; export const supportsColorStderr: typeof supportsColor; +export { + ModifierName as Modifiers, + ForegroundColorName as ForegroundColor, + BackgroundColorName as BackgroundColor, + ColorName as Color, +// } from '#ansi-styles'; +} from './vendor/ansi-styles/index.js'; + export { ColorInfo, ColorSupport, diff --git a/source/index.js b/source/index.js index 340eb10..7a3cc71 100644 --- a/source/index.js +++ b/source/index.js @@ -204,14 +204,16 @@ Object.defineProperties(createChalk.prototype, styles); const chalk = createChalk(); export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0}); +export { + modifierNames as modifiers, + foregroundColorNames as foregroundColors, + backgroundColorNames as backgroundColors, + colorNames as colors, +} from './vendor/ansi-styles/index.js'; + export { stdoutColor as supportsColor, stderrColor as supportsColorStderr, }; -export const modifiers = Object.keys(ansiStyles.modifier); -export const foregroundColors = Object.keys(ansiStyles.color); -export const backgroundColors = Object.keys(ansiStyles.bgColor); -export const colors = [...foregroundColors, ...backgroundColors]; - export default chalk; diff --git a/source/index.test-d.ts b/source/index.test-d.ts index a1ef854..20fdd59 100644 --- a/source/index.test-d.ts +++ b/source/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType, expectAssignable, expectError} from 'tsd'; -import chalk, {Chalk, ChalkInstance, Color, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js'; +import chalk, {Chalk, ChalkInstance, Modifiers, ForegroundColor, BackgroundColor, Color, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js'; // - supportsColor - expectType(supportsColor); @@ -141,6 +141,20 @@ expectType(chalk.underline``); expectType(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`); expectType(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`); -// -- Color types == +// -- Modifiers types +expectAssignable('strikethrough'); +expectError('delete'); + +// -- Foreground types +expectAssignable('red'); +expectError('pink'); + +// -- Background types +expectAssignable('bgRed'); +expectError('bgPink'); + +// -- Color types -- expectAssignable('red'); +expectAssignable('bgRed'); expectError('hotpink'); +expectError('bgHotpink'); diff --git a/source/vendor/ansi-styles/index.d.ts b/source/vendor/ansi-styles/index.d.ts index 3a3e777..d4aee1e 100644 --- a/source/vendor/ansi-styles/index.d.ts +++ b/source/vendor/ansi-styles/index.d.ts @@ -180,6 +180,49 @@ export interface ConvertColor { hexToAnsi(hex: string): number; } +/** +Basic modifier names. +*/ +export type ModifierName = keyof Modifier; + +/** +Basic foreground color names. +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type ForegroundColorName = keyof ForegroundColor; + +/** +Basic background color names. +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type BackgroundColorName = keyof BackgroundColor; + +/** +Basic color names. The combination of foreground and background color names. +[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support) +*/ +export type ColorName = ForegroundColorName | BackgroundColorName; + +/** +Basic modifier names. +*/ +export const modifierNames: readonly ModifierName[]; + +/** +Basic foreground color names. +*/ +export const foregroundColorNames: readonly ForegroundColorName[]; + +/** +Basic background color names. +*/ +export const backgroundColorNames: readonly BackgroundColorName[]; + +/* +Basic color names. The combination of foreground and background color names. +*/ +export const colorNames: readonly ColorName[]; + declare const ansiStyles: { readonly modifier: Modifier; readonly color: ColorBase & ForegroundColor; @@ -187,9 +230,4 @@ declare const ansiStyles: { readonly codes: ReadonlyMap; } & ForegroundColor & BackgroundColor & Modifier & ConvertColor; -declare const modifiers: readonly Modifier[]; -declare const foregroundColors: readonly ForegroundColor[]; -declare const backgroundColors: readonly BackgroundColor[]; -declare const colors: readonly Color[]; - export default ansiStyles; diff --git a/source/vendor/ansi-styles/index.js b/source/vendor/ansi-styles/index.js index 36b26b2..1b253de 100644 --- a/source/vendor/ansi-styles/index.js +++ b/source/vendor/ansi-styles/index.js @@ -214,8 +214,9 @@ function assembleStyles() { } const ansiStyles = assembleStyles(); -export const modifiers = Object.keys(styles.modifier); -export const foregroundColors = Object.keys(styles.color); -export const backgroundColors = Object.keys(styles.bgColor); +export const modifierNames = Object.keys(styles.modifier); +export const foregroundColorNames = Object.keys(styles.color); +export const backgroundColorNames = Object.keys(styles.bgColor); +export const colorNames = [...foregroundColorNames, ...backgroundColorNames]; export default ansiStyles;