update ansi-styles to 3.2.1 (#25398)

* update ansi-styles to 2.3.1

* update fromat function's params of apple rgb

* add more comments in ansi-styles

* add missing field `close` of ansiStyles.color and ansiStyles.bgColor

* update ansi-styles test

* fix two wrong types; update test file; rename identifiers.
This commit is contained in:
plylrnsdy
2018-05-08 02:46:10 +08:00
committed by Sheetal Nandi
parent 012108d8c6
commit b2159abc2e
3 changed files with 210 additions and 71 deletions

View File

@@ -1,53 +1,52 @@
import { EscapeCode } from './escape-code';
import AnsiStyles = require('ansi-styles');
import ansi = require('ansi-styles');
let ansiStyles = AnsiStyles as any,
nsNames = ['modifier', 'color', 'bgColor'],
namespaces = {
modifier: ['reset', 'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden', 'strikethrough'],
color: ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray',
'redBright', 'greenBright', 'yellowBright', 'blueBright', 'magentaBright', 'cyanBright', 'whiteBright'],
bgColor: ['bgBlack', 'bgRed', 'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan',
'bgBlackBright', 'bgRedBright', 'bgGreenBright', 'bgYellowBright', 'bgBlueBright', 'bgMagentaBright', 'bgCyanBright', 'bgWhiteBright'],
} as any,
styles = [...namespaces.modifier, ...namespaces.color, ...namespaces.bgColor],
codePair = ['open', 'close'],
codeTypes = ['ansi', 'ansi256', 'ansi16m'],
colorFormats = ['ansi', 'rgb', 'hsl', 'hsv', 'hwb', 'cmyk', 'xyz', 'lab', 'lch', 'hex', 'keyword', 'ansi256', 'hcg', 'apple', 'gray'],
codesMap = 'codes';
var styles = [
ansi.reset,
checkStyle(ansiStyles, styles);
nsNames.forEach(ns => checkStyle(ansiStyles[ns], namespaces[ns]));
ansi.bold,
ansi.dim,
ansi.italic,
ansi.underline,
ansi.inverse,
ansi.hidden,
ansi.strikethrough,
checkIsMap(ansiStyles[codesMap], `ansiStyles.${codesMap} not a Map.`);
nsNames.forEach(ns => checkExist(ansiStyles[ns], `ansiStyles.${ns} is not exist.`));
ansi.black,
ansi.red,
ansi.green,
ansi.yellow,
ansi.blue,
ansi.magenta,
ansi.cyan,
ansi.white,
ansi.gray,
['color', 'bgColor'].forEach(ns => checkConverter(ns, ansiStyles[ns], colorFormats));
ansi.bgBlack,
ansi.bgRed,
ansi.bgGreen,
ansi.bgYellow,
ansi.bgBlue,
ansi.bgMagenta,
ansi.bgCyan,
ansi.bgWhite
]
for (var key in styles) {
check(key, styles[key])
function checkStyle(namespace: any, styles: string[]) {
styles.forEach(s => checkCodePair(s, namespace[s]));
}
function checkCodePair(styleName: string, pair: any): void {
codePair.forEach(p => checkIsString(pair[p], `${styleName}.${p} is not a string.`));
}
function check(key:string, escapeCodes:ansi.EscapeCodePair): void {
if (uninitialized(escapeCodes.open)) {
throw new Error('key not found ~> ' + key + '.open')
}
if (uninitialized(escapeCodes.close)) {
throw new Error('key not found ~> ' + key + '.close')
}
function checkConverter(nsName: string, namespace: any, formats: string[]) {
formats.forEach(f => codeTypes.forEach(t => checkIsFunction(namespace[t][f], `ansiStyles.${nsName}.${t}.${f} is not a function.`)));
checkIsString(namespace.close, `${namespace}.close is not a string.`);
}
function uninitialized(val:any): boolean {
return val === null || val === undefined
function checkExist(val: any, failMsg: string): void {
if (val == null) throw new Error(failMsg);
}
function checkIsString(val: any, failMsg: string): void {
if(typeof val != 'string') throw new Error(failMsg);
}
function checkIsFunction(fn: any, failMsg: string): void {
if (typeof fn != 'function') throw new Error(failMsg);
}
function checkIsMap(map: any, failMsg: string): void {
if (!(map instanceof Map)) throw new Error(failMsg);
}

106
types/ansi-styles/escape-code.d.ts vendored Normal file
View File

@@ -0,0 +1,106 @@
import * as cssKeywords from 'color-name';
export namespace EscapeCode {
export interface CodePair {
open: string;
close: string;
}
interface Modifier {
reset: CodePair;
bold: CodePair;
dim: CodePair;
/**
* Not widely supported
*/
italic: CodePair;
underline: CodePair;
inverse: CodePair;
hidden: CodePair;
/**
* Not widely supported
*/
strikethrough: CodePair;
}
interface Color {
black: CodePair;
red: CodePair;
green: CodePair;
yellow: CodePair;
blue: CodePair;
magenta: CodePair;
cyan: CodePair;
white: CodePair;
/**
* bright black
*/
gray: CodePair;
grey: CodePair;
redBright: CodePair;
greenBright: CodePair;
yellowBright: CodePair;
blueBright: CodePair;
magentaBright: CodePair;
cyanBright: CodePair;
whiteBright: CodePair;
}
interface BackgroundColor {
bgBlack: CodePair;
bgRed: CodePair;
bgGreen: CodePair;
bgYellow: CodePair;
bgBlue: CodePair;
bgMagenta: CodePair;
bgCyan: CodePair;
bgWhite: CodePair;
bgBlackBright: CodePair;
bgRedBright: CodePair;
bgGreenBright: CodePair;
bgYellowBright: CodePair;
bgBlueBright: CodePair;
bgMagentaBright: CodePair;
bgCyanBright: CodePair;
bgWhiteBright: CodePair;
}
interface Conversions {
ansi: (ansi: number) => string
rgb: (r: number, g: number, b: number) => string
hsl: (h: number, s: number, l: number) => string
hsv: (h: number, s: number, v: number) => string
hwb: (h: number, w: number, b: number) => string
cmyk: (c: number, m: number, y: number, k: number) => string
xyz: (x: number, y: number, z: number) => string
lab: (l: number, a: number, b: number) => string
lch: (l: number, c: number, h: number) => string
hex: (hex: string) => string
/**
* color keyword in css to ansi code
*/
keyword: (keyword: keyof typeof cssKeywords) => string
ansi256: (ansi256: number) => string
hcg: (h: number, c: number, g: number) => string
/**
* apple RGB to ansi code
*/
apple: (r: number, g: number, b: number) => string
gray: (grayscale: number) => string
}
interface ColorType {
/**
* 16 color ansi code
*/
ansi: Conversions
/**
* 256 color ansi code
*/
ansi256: Conversions
/**
* truecolor(16 million color) ansi code
*/
ansi16m: Conversions
}
}

View File

@@ -1,40 +1,74 @@
// Type definitions for ansi-styles 2.0.1
// Type definitions for ansi-styles 3.2.1
// Project: https://github.com/sindresorhus/ansi-styles
// Definitions by: bryn austin bellomy <https://github.com/brynbellomy>
// plylrnsdy <https://github.com/plylrnsdy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { EscapeCode } from './escape-code';
export interface EscapeCodePair {
open: string;
close: string;
}
export declare var reset: EscapeCodePair;
export const reset: EscapeCode.CodePair;
export const bold: EscapeCode.CodePair;
export const dim: EscapeCode.CodePair;
/**
* Not widely supported
*/
export const italic: EscapeCode.CodePair;
export const underline: EscapeCode.CodePair;
export const inverse: EscapeCode.CodePair;
export const hidden: EscapeCode.CodePair;
/**
* Not widely supported
*/
export const strikethrough: EscapeCode.CodePair;
export declare var bold: EscapeCodePair;
export declare var dim: EscapeCodePair;
export declare var italic: EscapeCodePair;
export declare var underline: EscapeCodePair;
export declare var inverse: EscapeCodePair;
export declare var hidden: EscapeCodePair;
export declare var strikethrough: EscapeCodePair;
export const black: EscapeCode.CodePair;
export const red: EscapeCode.CodePair;
export const green: EscapeCode.CodePair;
export const yellow: EscapeCode.CodePair;
export const blue: EscapeCode.CodePair;
export const magenta: EscapeCode.CodePair;
export const cyan: EscapeCode.CodePair;
export const white: EscapeCode.CodePair;
/**
* bright black
*/
export const gray: EscapeCode.CodePair;
export const grey: EscapeCode.CodePair;
export declare var black: EscapeCodePair;
export declare var red: EscapeCodePair;
export declare var green: EscapeCodePair;
export declare var yellow: EscapeCodePair;
export declare var blue: EscapeCodePair;
export declare var magenta: EscapeCodePair;
export declare var cyan: EscapeCodePair;
export declare var white: EscapeCodePair;
export declare var gray: EscapeCodePair;
export const redBright: EscapeCode.CodePair;
export const greenBright: EscapeCode.CodePair;
export const yellowBright: EscapeCode.CodePair;
export const blueBright: EscapeCode.CodePair;
export const magentaBright: EscapeCode.CodePair;
export const cyanBright: EscapeCode.CodePair;
export const whiteBright: EscapeCode.CodePair;
export declare var bgBlack: EscapeCodePair;
export declare var bgRed: EscapeCodePair;
export declare var bgGreen: EscapeCodePair;
export declare var bgYellow: EscapeCodePair;
export declare var bgBlue: EscapeCodePair;
export declare var bgMagenta: EscapeCodePair;
export declare var bgCyan: EscapeCodePair;
export declare var bgWhite: EscapeCodePair;
export const bgBlack: EscapeCode.CodePair;
export const bgRed: EscapeCode.CodePair;
export const bgGreen: EscapeCode.CodePair;
export const bgYellow: EscapeCode.CodePair;
export const bgBlue: EscapeCode.CodePair;
export const bgMagenta: EscapeCode.CodePair;
export const bgCyan: EscapeCode.CodePair;
export const bgWhite: EscapeCode.CodePair;
export const bgBlackBright: EscapeCode.CodePair;
export const bgRedBright: EscapeCode.CodePair;
export const bgGreenBright: EscapeCode.CodePair;
export const bgYellowBright: EscapeCode.CodePair;
export const bgBlueBright: EscapeCode.CodePair;
export const bgMagentaBright: EscapeCode.CodePair;
export const bgCyanBright: EscapeCode.CodePair;
export const bgWhiteBright: EscapeCode.CodePair;
/**
* Raw escape codes (i.e. without the CSI escape prefix \u001B[ and render mode postfix m) are available.
*
* This is a Map with the open codes as keys and close codes as values.
*/
export const codes: Map<number, number>
export const modifier: EscapeCode.Modifier
export const color: EscapeCode.Color & EscapeCode.ColorType & { close: string }
export const bgColor: EscapeCode.BackgroundColor & EscapeCode.ColorType & { close: string }