Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a99a914

Browse files
committed
Improve TypeScript typings and colour detection
1 parent 9da1271 commit a99a914

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

example/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ console.log(leeks.colours.blue`hello there`);
3535
leeks.alias('primary', 'colours', leeks.colours.green);
3636
console.log(leeks.colours.primary('hi'));
3737

38-
console.log(leeks.short('&!3&0&0 Hello &r &2&o&nworld!&r &lBold&r &nUnderlined&r &oItalic&r &#009999HEX&r &!#009999&0more HEX'));
38+
console.log(leeks.short('&!3&0&0 Hello &r &2&o&nworld!&r &lBold&r &nUnderlined&r &oItalic&r &#009999HEX&r &!#009999&0more HEX'));

index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ declare module 'leeks.js' {
117117
* @param {string} type Either "colours", "colors" or "styles"
118118
* @param {string} value The colour/style you want to use, e.g leeks.colours.green
119119
*/
120-
export type alias = () => void;
120+
export const alias: (name: string, type: 'colours' | 'colors' | 'styled', value: any) => void;
121121

122122
/** Enable colour support for leeks.js */
123-
export type enableColours = () => void;
123+
export const enableColours: () => void;
124124

125125
/** Alias for `enableColours` */
126-
export type enableColors = () => void;
126+
export const enableColors: () => void;
127127

128128
/** Disable colour support for leeks.js */
129-
export type disableColours = () => void;
129+
export const disableColours: () => void;
130130

131131
/** Alias for `disableColours` */
132-
export type disableColors = () => void;
132+
export const disableColors: () => void;
133133
}

src/index.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,22 @@
33
* @copyright David Ralph 2019-2021
44
* @license MIT
55
*/
6+
7+
import * as detect from './utils/hasColoursSupported';
68
import Colours from './data/Colours';
79
import Styles from './data/Styles';
810
import Keywords from './data/Keywords';
911
import ShortCodes from './data/ShortCodes';
1012

11-
const isNode = typeof process !== 'undefined';
12-
const hasColors = typeof process.stdout?.hasColors !== 'undefined';
13-
14-
/**
15-
* Check if colours are supported (returns false on browser)
16-
*/
17-
const colorsEnabled = !isNode ? false : (
18-
(!('NO_COLOR' in process.env) && process.env.FORCE_COLOR !== '0') ||
19-
(hasColors ? process.stdout.hasColors() : false)
20-
);
21-
22-
let enabled = colorsEnabled;
13+
let colorsEnabled = detect.hasColoursSupported();
2314

2415
/**
2516
* Change the colour of the given text (List: https://docs.davidcralph.co.uk/#/leeks)
2617
* @param {string} t The text to change the colour of
2718
*/
2819
const colours = [];
2920
for (const c in Colours) {
30-
colours[c] = (t: string) => enabled ? `\x1b[${Colours[c]}m${t}\x1b[0m` : t;
21+
colours[c] = (t: string) => colorsEnabled ? `\x1b[${Colours[c]}m${t}\x1b[0m` : t;
3122
}
3223

3324
/**
@@ -36,7 +27,7 @@ for (const c in Colours) {
3627
*/
3728
const styles = [];
3829
for (const s in Styles) {
39-
styles[s] = (t: string) => enabled ? `\x1b[${Styles[s]}m${t}\x1b[0m` : t;
30+
styles[s] = (t: string) => colorsEnabled ? `\x1b[${Styles[s]}m${t}\x1b[0m` : t;
4031
}
4132

4233
/**
@@ -45,7 +36,7 @@ for (const s in Styles) {
4536
*/
4637
const keywords = [];
4738
for (const k in Keywords) {
48-
keywords[k] = (t: string) => enabled ? rgb(Keywords[k], t) : t;
39+
keywords[k] = (t: string) => colorsEnabled ? rgb(Keywords[k], t) : t;
4940
}
5041

5142
/**
@@ -54,7 +45,7 @@ for (const k in Keywords) {
5445
*/
5546
const bgKeywords = [];
5647
for (const k in Keywords) {
57-
bgKeywords[k] = (t: string) => enabled ? rgbBg(Keywords[k], t) : t;
48+
bgKeywords[k] = (t: string) => colorsEnabled ? rgbBg(Keywords[k], t) : t;
5849
}
5950

6051
/**
@@ -63,7 +54,7 @@ for (const k in Keywords) {
6354
* @param {string} t The text to show with the 8-bit colour
6455
*/
6556
export function eightBit(i: string, t: string) {
66-
if (!enabled) {
57+
if (!colorsEnabled) {
6758
return t;
6859
}
6960

@@ -76,7 +67,7 @@ export function eightBit(i: string, t: string) {
7667
* @param {string} t The text to show with the 8-bit colour
7768
*/
7869
export function eightBitBg(i: string, t: string) {
79-
if (!enabled) {
70+
if (!colorsEnabled) {
8071
return t;
8172
}
8273

@@ -89,7 +80,7 @@ export function eightBitBg(i: string, t: string) {
8980
* @param {string} t The text to show with the RGB colour
9081
*/
9182
export function rgb(rgb: [number, number, number], t: string) {
92-
if (!enabled) {
83+
if (!colorsEnabled) {
9384
return t;
9485
}
9586

@@ -103,7 +94,7 @@ export function rgb(rgb: [number, number, number], t: string) {
10394
* @param {string} t The text to show with the RGB colour
10495
*/
10596
export function rgbBg(rgb: [number, number, number], t: string) {
106-
if (!enabled) {
97+
if (!colorsEnabled) {
10798
return t;
10899
}
109100

@@ -138,7 +129,7 @@ export function hexBg(hex: string, t: string) {
138129
* @param {string} t The text to format
139130
*/
140131
export function short(t: string) {
141-
return enabled
132+
return colorsEnabled
142133
? t
143134
.replace(/&!?[0-9a-f]/gi, code => `\x1b[${Colours[ShortCodes.colours[code]]}m`)
144135
.replace(/&[i-pr]/gi, code => `\x1b[${Styles[ShortCodes.styles[code]]}m`)
@@ -174,15 +165,15 @@ export function alias(name: string, type: string, value: string) {
174165
* Enable colour support for leeks.js
175166
*/
176167
export function enableColours() {
177-
enabled = true;
178-
};
168+
colorsEnabled = true;
169+
}
179170

180171
/**
181172
* Disable colour support for leeks.js
182173
*/
183174
export function disableColours() {
184-
enabled = false;
185-
};
175+
colorsEnabled = false;
176+
}
186177

187178
export {
188179
colours as colors,

src/utils/hasColoursSupported.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const isNode = typeof process !== 'undefined';
2+
const truthy = new Set(['yes', 'true', '1', 'enable', 'e', 'enabled']);
3+
const falsy = new Set(['no', 'false', '0', 'disable', 'd', 'disabled']);
4+
5+
/**
6+
* Method to detect if the current host has colors enabled or not. The result is cached on the first
7+
* invocation of {@link hasColoursSupported}, the rest will just return that variable.
8+
*/
9+
export function hasColoursSupported() {
10+
// Check if we have the `process` global available. If not, let's not enable it.
11+
if (!isNode) {
12+
return false;
13+
}
14+
15+
// Check if the `NO_COLOR` system enviornment variable is available.
16+
if ('NO_COLOR' in process.env) {
17+
return false;
18+
}
19+
20+
// Check if `FORCE_COLORS` exists and see if it is truthy.
21+
if ('FORCE_COLORS' in process.env) {
22+
// Check if it is a truthy value
23+
if (truthy.has(process.env.FORCE_COLORS)) {
24+
return true;
25+
}
26+
27+
// Check if `FORCE_COLORS` was a falsy value
28+
if (falsy.has(process.env.FORCE_COLORS)) {
29+
return true;
30+
}
31+
}
32+
33+
// Check if any CI service supports colours
34+
if ('CI' in process.env) {
35+
return ['GITHUB_ACTIONS', 'TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(i => i in process.env) || (
36+
typeof process.env.CI_NAME !== undefined && process.env.CI_NAME === 'codeship'
37+
);
38+
}
39+
40+
// Check if the process' standard output has colours enabled from the `hasColors` function.
41+
if (typeof process.stdout?.hasColors !== 'undefined') {
42+
return process.stdout.hasColors();
43+
}
44+
45+
// Otherwise, fallback to false
46+
console.log('fallback');
47+
return false;
48+
}

0 commit comments

Comments
 (0)