From b65f9a5bfe1aa37d7366be3c71ebca087b6a6a8b Mon Sep 17 00:00:00 2001 From: Guketlev Dmitry Date: Fri, 8 Feb 2019 23:57:43 +0300 Subject: [PATCH] Add typings for color support --- types/color-support/color-support-tests.ts | 48 ++++++++++++++++++++++ types/color-support/index.d.ts | 34 +++++++++++++++ types/color-support/tsconfig.json | 23 +++++++++++ types/color-support/tslint.json | 1 + 4 files changed, 106 insertions(+) create mode 100644 types/color-support/color-support-tests.ts create mode 100644 types/color-support/index.d.ts create mode 100644 types/color-support/tsconfig.json create mode 100644 types/color-support/tslint.json diff --git a/types/color-support/color-support-tests.ts b/types/color-support/color-support-tests.ts new file mode 100644 index 0000000000..5c61067dcf --- /dev/null +++ b/types/color-support/color-support-tests.ts @@ -0,0 +1,48 @@ +import * as colorSupport from 'color-support'; + +(() => { + const result = colorSupport(); + if (!result) { + console.log('color is not supported'); + } else if (result.has16m) { + console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m'); + } else if (result.has256) { + console.log('\x1b[38;5;119m256 colors\x1b[0m'); + } else if (result.hasBasic) { + console.log('\x1b[31mbasic colors\x1b[0m'); + } else { + console.log('Level: ', result.level); + console.log('this is impossible, but colors are not supported'); + } +})(); + +(() => { + const result = colorSupport({ + alwaysReturn: false, + env: process.env, + ignoreCI: true, + ignoreDumb: true, + ignoreTTY: true, + level: 2, + stream: process.stdout, + term: process.env.TERM, + }, { + has16m: false, + has256: false, + hasBasic: false, + level: 0, + }); + + if (!result) { + console.log('color is not supported'); + } else if (result.has16m) { + console.log('\x1b[38;2;102;194;255m16m colors\x1b[0m'); + } else if (result.has256) { + console.log('\x1b[38;5;119m256 colors\x1b[0m'); + } else if (result.hasBasic) { + console.log('\x1b[31mbasic colors\x1b[0m'); + } else { + console.log('Level: ', result.level); + console.log('this is impossible, but colors are not supported'); + } +})(); diff --git a/types/color-support/index.d.ts b/types/color-support/index.d.ts new file mode 100644 index 0000000000..290a6e29b0 --- /dev/null +++ b/types/color-support/index.d.ts @@ -0,0 +1,34 @@ +// Type definitions for color-support 1.1 +// Project: https://github.com/isaacs/color-support#readme +// Definitions by: Guketlev Dmitry +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +type ColorSupportLevel = 0 | 1 | 2 | 3; + +interface ColorSupportOptions { + alwaysReturn?: boolean; + env?: NodeJS.ProcessEnv; + ignoreCI?: boolean; + ignoreDumb?: boolean; + ignoreTTY?: boolean; + level?: ColorSupportLevel; + stream?: NodeJS.WriteStream; + term?: string; +} + +interface ColorSupportResult { + level: ColorSupportLevel; + hasBasic: boolean; + has256: boolean; + has16m: boolean; +} + +type ColorSupport = (options?: ColorSupportOptions, + obj?: ColorSupportResult) => false | ColorSupportResult; + +declare var colorSupport: ColorSupport; + +export = colorSupport; diff --git a/types/color-support/tsconfig.json b/types/color-support/tsconfig.json new file mode 100644 index 0000000000..f1e6de8e28 --- /dev/null +++ b/types/color-support/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "color-support-tests.ts" + ] +} diff --git a/types/color-support/tslint.json b/types/color-support/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/color-support/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }