Skip to content

Commit

Permalink
Add TypeScript definition (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
Richienb and sindresorhus authored Nov 18, 2021
1 parent ff1704d commit 2715d14
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
55 changes: 55 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {WriteStream} from 'node:tty';

export interface Options {
/**
Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
@default true
*/
readonly sniffFlags?: boolean;
}

/**
Levels:
- `0` - All colors disabled.
- `1` - Basic 16 colors support.
- `2` - ANSI 256 colors support.
- `3` - Truecolor 16 million colors support.
*/
export type ColorSupportLevel = 0 | 1 | 2 | 3;

/**
Detect whether the terminal supports color.
*/
export interface ColorSupport {
/**
The color level.
*/
level: ColorSupportLevel;

/**
Whether basic 16 colors are supported.
*/
hasBasic: boolean;

/**
Whether ANSI 256 colors are supported.
*/
has256: boolean;

/**
Whether Truecolor 16 million colors are supported.
*/
has16m: boolean;
}

export type ColorInfo = ColorSupport | false;

export function createSupportsColor(stream: WriteStream, options?: Options): ColorInfo;

declare const supportsColor: {
stdout: ColorInfo;
stderr: ColorInfo;
};

export default supportsColor;
13 changes: 13 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {stdout, stderr} from 'node:process';
import {expectType} from 'tsd';
import supportsColor, {createSupportsColor, Options, ColorInfo} from './index.js';

const options: Options = {};

expectType<ColorInfo>(supportsColor.stdout);
expectType<ColorInfo>(supportsColor.stderr);

expectType<ColorInfo>(createSupportsColor(stdout));
expectType<ColorInfo>(createSupportsColor(stderr));

expectType<ColorInfo>(createSupportsColor(stdout, options));
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"node": ">=12"
},
"scripts": {
"//test": "xo && ava",
"test": "xo"
"//test": "xo && ava && tsd",
"test": "xo && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
],
"keywords": [
Expand All @@ -49,8 +50,10 @@
"16m"
],
"devDependencies": {
"@types/node": "^16.11.7",
"ava": "^3.15.0",
"import-fresh": "^3.3.0",
"tsd": "^0.18.0",
"typescript": "^4.4.3",
"xo": "^0.44.0"
}
Expand Down

0 comments on commit 2715d14

Please sign in to comment.