-
-
Notifications
You must be signed in to change notification settings - Fork 80
Add TypeScript type definition #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0fe9e8
feat: Add TypeScript type definition
ExE-Boss 3dd1e3b
Address review comments
ExE-Boss 6689bcd
Address review comments
ExE-Boss 78f7ed9
Update index.d.ts
sindresorhus ca20c36
Update package.json
sindresorhus 743ad90
docs: Add missing JSDoc comments to type definition
ExE-Boss 02af831
Update index.test-d.ts
sindresorhus 634f224
Update index.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,197 @@ | ||
import {KEYWORD} from 'color-convert/conversions'; | ||
|
||
declare namespace ansiStyles { | ||
interface ColorConvert { | ||
/** | ||
* The RGB color space | ||
* | ||
* @param red (0-255) | ||
* @param green (0-255) | ||
* @param blue (0-255) | ||
*/ | ||
rgb(red: number, green: number, blue: number): string; | ||
|
||
/** | ||
* The HSL color space | ||
* | ||
* @param hue (0-360) | ||
* @param saturation (0-100) | ||
* @param lightness (0-100) | ||
*/ | ||
hsl(hue: number, saturation: number, lightness: number): string; | ||
|
||
/** | ||
* The HSV color space | ||
* | ||
* @param hue (0-360) | ||
* @param saturation (0-100) | ||
* @param value (0-100) | ||
*/ | ||
hsv(hue: number, saturation: number, value: number): string; | ||
|
||
/** | ||
* The HSV color space | ||
* | ||
* @param hue (0-360) | ||
* @param whiteness (0-100) | ||
* @param blackness (0-100) | ||
*/ | ||
hwb(hue: number, whiteness: number, blackness: number): string; | ||
|
||
/** | ||
* The CMYK color space | ||
* | ||
* @param cyan (0-100) | ||
* @param magenta (0-100) | ||
* @param yellow (0-100) | ||
* @param black (0-100) | ||
*/ | ||
cmyk(cyan: number, magenta: number, yellow: number, black: number): string; | ||
|
||
/** | ||
* The XYZ color space | ||
* | ||
* @param x (0-100) | ||
* @param y (0-100) | ||
* @param z (0-100) | ||
*/ | ||
xyz(x: number, y: number, z: number): string; | ||
|
||
/** | ||
* The LAB color space | ||
* | ||
* @param x (0-100) | ||
* @param y (0-100) | ||
* @param z (0-100) | ||
*/ | ||
lab(lightness: number, a: number, b: number): string; | ||
|
||
/** | ||
* The LCH/HCL color space | ||
* | ||
* @param lightness (0-100) | ||
* @param chroma (0-100) | ||
* @param hue (0-360) | ||
*/ | ||
lch(lightness: number, chroma: number, hue: number): string; | ||
|
||
/** | ||
* The RGB HEX color space | ||
* | ||
* @param hex A hexadecimal string containing RGB data | ||
*/ | ||
hex(hex: string): string; | ||
|
||
keyword(keyword: KEYWORD): string; | ||
ansi(ansi: number): string; | ||
ansi256(ansi: number): string; | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* The HCG color space | ||
* | ||
* @param hue (0-360) | ||
* @param chroma (0-100) | ||
* @param gray (0-100) | ||
*/ | ||
hcg(hue: number, chroma: number, gray: number): string; | ||
|
||
/** | ||
* The Apple RGB 16-bit color space | ||
* | ||
* @param red (0-65535) | ||
* @param green (0-65535) | ||
* @param blue (0-65535) | ||
*/ | ||
apple(r16: number, g16: number, b16: number): string; | ||
|
||
/** | ||
* Grayscale color | ||
* | ||
* @param gray (0-100) | ||
*/ | ||
gray(gray: number): string; | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
interface CSIPair { | ||
/** The ANSI terminal Control Sequence for setting this style */ | ||
readonly open: string; | ||
|
||
/** The ANSI terminal Control Sequence for resetting this style */ | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readonly close: string; | ||
} | ||
|
||
interface ColorBase<Close extends string> { | ||
readonly ansi: ColorConvert; | ||
readonly ansi256: ColorConvert; | ||
readonly ansi16m: ColorConvert; | ||
|
||
/** The ANSI terminal Control Sequence for resetting this color */ | ||
readonly close: Close; | ||
} | ||
|
||
interface Modifier { | ||
readonly bold: CSIPair; | ||
readonly dim: CSIPair; | ||
readonly hidden: CSIPair; | ||
readonly inverse: CSIPair; | ||
readonly italic: CSIPair; | ||
readonly reset: CSIPair; | ||
readonly strikethrough: CSIPair; | ||
readonly underline: CSIPair; | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
interface ForegroundColor { | ||
readonly black: CSIPair; | ||
readonly red: CSIPair; | ||
readonly green: CSIPair; | ||
readonly yellow: CSIPair; | ||
readonly blue: CSIPair; | ||
readonly cyan: CSIPair; | ||
readonly magenta: CSIPair; | ||
readonly white: CSIPair; | ||
|
||
readonly gray: CSIPair; | ||
readonly grey: CSIPair; | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
readonly blackBright: CSIPair; | ||
readonly redBright: CSIPair; | ||
readonly greenBright: CSIPair; | ||
readonly yellowBright: CSIPair; | ||
readonly blueBright: CSIPair; | ||
readonly cyanBright: CSIPair; | ||
readonly magentaBright: CSIPair; | ||
readonly whiteBright: CSIPair; | ||
} | ||
|
||
interface BackgroundColor { | ||
readonly bgBlack: CSIPair; | ||
readonly bgRed: CSIPair; | ||
readonly bgGreen: CSIPair; | ||
readonly bgYellow: CSIPair; | ||
readonly bgBlue: CSIPair; | ||
readonly bgCyan: CSIPair; | ||
readonly bgMagenta: CSIPair; | ||
readonly bgWhite: CSIPair; | ||
|
||
readonly bgGray: CSIPair; | ||
readonly bgGrey: CSIPair; | ||
|
||
readonly bgBlackBright: CSIPair; | ||
readonly bgRedBright: CSIPair; | ||
readonly bgGreenBright: CSIPair; | ||
readonly bgYellowBright: CSIPair; | ||
readonly bgBlueBright: CSIPair; | ||
readonly bgCyanBright: CSIPair; | ||
readonly bgMagentaBright: CSIPair; | ||
readonly bgWhiteBright: CSIPair; | ||
} | ||
} | ||
|
||
declare const ansiStyles: { | ||
readonly modifier: ansiStyles.Modifier; | ||
readonly color: ansiStyles.ForegroundColor & ansiStyles.ColorBase<'\u001B[39m'>; | ||
ExE-Boss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readonly bgColor: ansiStyles.BackgroundColor & ansiStyles.ColorBase<'\u001B[49m'>; | ||
readonly codes: ReadonlyMap<number, number>; | ||
} & ansiStyles.BackgroundColor & ansiStyles.ForegroundColor & ansiStyles.Modifier; | ||
|
||
export = ansiStyles; |
This file contains hidden or 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,144 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import colorConvert = require('color-convert'); | ||
import ansiStyles = require('.'); | ||
|
||
declare function keyof<T>(type: T): keyof T; | ||
|
||
type ExpectedColorSpaces = Exclude<keyof typeof colorConvert, 'ansi16'> | 'ansi'; | ||
let expectedColors!: ExpectedColorSpaces; | ||
|
||
expectType<ReadonlyMap<number, number>>(ansiStyles.codes); | ||
|
||
// - ColorConvert - | ||
// -- Foreground color -- | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.color.ansi)); | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.color.ansi256)); | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.color.ansi16m)); | ||
|
||
expectType<keyof typeof ansiStyles.color.ansi>(expectedColors); | ||
expectType<keyof typeof ansiStyles.color.ansi256>(expectedColors); | ||
expectType<keyof typeof ansiStyles.color.ansi16m>(expectedColors); | ||
|
||
// -- Background color -- | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.bgColor.ansi)); | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.bgColor.ansi256)); | ||
expectType<ExpectedColorSpaces>(keyof(ansiStyles.bgColor.ansi16m)); | ||
|
||
expectType<keyof typeof ansiStyles.bgColor.ansi>(expectedColors); | ||
expectType<keyof typeof ansiStyles.bgColor.ansi256>(expectedColors); | ||
expectType<keyof typeof ansiStyles.bgColor.ansi16m>(expectedColors); | ||
|
||
// - Static colors - | ||
// -- Namespaced -- | ||
// --- Foreground color --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.black); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.red); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.green); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.yellow); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.blue); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.cyan); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.magenta); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.white); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.color.gray); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.grey); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.color.blackBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.redBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.greenBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.yellowBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.blueBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.cyanBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.magentaBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.color.whiteBright); | ||
|
||
expectType<'\x1B[39m'>(ansiStyles.color.close); | ||
|
||
// --- Background color --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgBlack); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgRed); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgGreen); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgYellow); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgBlue); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgCyan); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgMagenta); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgWhite); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgGray); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgGrey); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgBlackBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgRedBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgGreenBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgYellowBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgBlueBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgCyanBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgMagentaBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgColor.bgWhiteBright); | ||
|
||
expectType<'\x1B[49m'>(ansiStyles.bgColor.close); | ||
|
||
// --- Modifiers --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.bold); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.dim); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.hidden); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.inverse); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.italic); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.reset); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.strikethrough); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.modifier.underline); | ||
|
||
// -- Top level -- | ||
// --- Foreground color --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.black); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.red); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.green); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.yellow); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.blue); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.cyan); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.magenta); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.white); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.gray); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.grey); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.blackBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.redBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.greenBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.yellowBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.blueBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.cyanBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.magentaBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.whiteBright); | ||
|
||
// --- Background color --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgBlack); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgRed); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgGreen); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgYellow); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgBlue); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgCyan); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgMagenta); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgWhite); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.bgGray); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgGrey); | ||
|
||
expectType<ansiStyles.CSIPair>(ansiStyles.bgBlackBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgRedBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgGreenBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgYellowBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgBlueBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgCyanBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgMagentaBright); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bgWhiteBright); | ||
|
||
// --- Modifiers --- | ||
expectType<ansiStyles.CSIPair>(ansiStyles.bold); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.dim); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.hidden); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.inverse); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.italic); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.reset); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.strikethrough); | ||
expectType<ansiStyles.CSIPair>(ansiStyles.underline); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.