-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
ff1704d
commit 2715d14
Showing
3 changed files
with
73 additions
and
2 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
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; |
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,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)); |
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