3
3
* @copyright David Ralph 2019-2021
4
4
* @license MIT
5
5
*/
6
+
7
+ import * as detect from './utils/hasColoursSupported' ;
6
8
import Colours from './data/Colours' ;
7
9
import Styles from './data/Styles' ;
8
10
import Keywords from './data/Keywords' ;
9
11
import ShortCodes from './data/ShortCodes' ;
10
12
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 ( ) ;
23
14
24
15
/**
25
16
* Change the colour of the given text (List: https://docs.davidcralph.co.uk/#/leeks)
26
17
* @param {string } t The text to change the colour of
27
18
*/
28
19
const colours = [ ] ;
29
20
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 ;
31
22
}
32
23
33
24
/**
@@ -36,7 +27,7 @@ for (const c in Colours) {
36
27
*/
37
28
const styles = [ ] ;
38
29
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 ;
40
31
}
41
32
42
33
/**
@@ -45,7 +36,7 @@ for (const s in Styles) {
45
36
*/
46
37
const keywords = [ ] ;
47
38
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 ;
49
40
}
50
41
51
42
/**
@@ -54,7 +45,7 @@ for (const k in Keywords) {
54
45
*/
55
46
const bgKeywords = [ ] ;
56
47
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 ;
58
49
}
59
50
60
51
/**
@@ -63,7 +54,7 @@ for (const k in Keywords) {
63
54
* @param {string } t The text to show with the 8-bit colour
64
55
*/
65
56
export function eightBit ( i : string , t : string ) {
66
- if ( ! enabled ) {
57
+ if ( ! colorsEnabled ) {
67
58
return t ;
68
59
}
69
60
@@ -76,7 +67,7 @@ export function eightBit(i: string, t: string) {
76
67
* @param {string } t The text to show with the 8-bit colour
77
68
*/
78
69
export function eightBitBg ( i : string , t : string ) {
79
- if ( ! enabled ) {
70
+ if ( ! colorsEnabled ) {
80
71
return t ;
81
72
}
82
73
@@ -89,7 +80,7 @@ export function eightBitBg(i: string, t: string) {
89
80
* @param {string } t The text to show with the RGB colour
90
81
*/
91
82
export function rgb ( rgb : [ number , number , number ] , t : string ) {
92
- if ( ! enabled ) {
83
+ if ( ! colorsEnabled ) {
93
84
return t ;
94
85
}
95
86
@@ -103,7 +94,7 @@ export function rgb(rgb: [number, number, number], t: string) {
103
94
* @param {string } t The text to show with the RGB colour
104
95
*/
105
96
export function rgbBg ( rgb : [ number , number , number ] , t : string ) {
106
- if ( ! enabled ) {
97
+ if ( ! colorsEnabled ) {
107
98
return t ;
108
99
}
109
100
@@ -138,7 +129,7 @@ export function hexBg(hex: string, t: string) {
138
129
* @param {string } t The text to format
139
130
*/
140
131
export function short ( t : string ) {
141
- return enabled
132
+ return colorsEnabled
142
133
? t
143
134
. replace ( / & ! ? [ 0 - 9 a - f ] / gi, code => `\x1b[${ Colours [ ShortCodes . colours [ code ] ] } m` )
144
135
. replace ( / & [ i - p r ] / gi, code => `\x1b[${ Styles [ ShortCodes . styles [ code ] ] } m` )
@@ -174,15 +165,15 @@ export function alias(name: string, type: string, value: string) {
174
165
* Enable colour support for leeks.js
175
166
*/
176
167
export function enableColours ( ) {
177
- enabled = true ;
178
- } ;
168
+ colorsEnabled = true ;
169
+ }
179
170
180
171
/**
181
172
* Disable colour support for leeks.js
182
173
*/
183
174
export function disableColours ( ) {
184
- enabled = false ;
185
- } ;
175
+ colorsEnabled = false ;
176
+ }
186
177
187
178
export {
188
179
colours as colors ,
0 commit comments