From a99a914ddfb161fd1c267ffb6ad773bf985d9a84 Mon Sep 17 00:00:00 2001 From: Noel Date: Sun, 25 Dec 2022 22:28:05 -0700 Subject: [PATCH 1/2] Improve TypeScript typings and colour detection --- example/example.js | 2 +- index.d.ts | 10 +++---- src/index.ts | 41 +++++++++++---------------- src/utils/hasColoursSupported.ts | 48 ++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 src/utils/hasColoursSupported.ts diff --git a/example/example.js b/example/example.js index 7439536..d2fad63 100644 --- a/example/example.js +++ b/example/example.js @@ -35,4 +35,4 @@ console.log(leeks.colours.blue`hello there`); leeks.alias('primary', 'colours', leeks.colours.green); console.log(leeks.colours.primary('hi')); -console.log(leeks.short('&!3&0&0 Hello &r &2&o&nworld!&r &lBold&r &nUnderlined&r &oItalic&r ✏HEX&r &!#009999&0more HEX')); \ No newline at end of file +console.log(leeks.short('&!3&0&0 Hello &r &2&o&nworld!&r &lBold&r &nUnderlined&r &oItalic&r ✏HEX&r &!#009999&0more HEX')); diff --git a/index.d.ts b/index.d.ts index aad2075..1510da1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -117,17 +117,17 @@ declare module 'leeks.js' { * @param {string} type Either "colours", "colors" or "styles" * @param {string} value The colour/style you want to use, e.g leeks.colours.green */ - export type alias = () => void; + export const alias: (name: string, type: 'colours' | 'colors' | 'styled', value: any) => void; /** Enable colour support for leeks.js */ - export type enableColours = () => void; + export const enableColours: () => void; /** Alias for `enableColours` */ - export type enableColors = () => void; + export const enableColors: () => void; /** Disable colour support for leeks.js */ - export type disableColours = () => void; + export const disableColours: () => void; /** Alias for `disableColours` */ - export type disableColors = () => void; + export const disableColors: () => void; } diff --git a/src/index.ts b/src/index.ts index 3a7312e..091746c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,23 +3,14 @@ * @copyright David Ralph 2019-2021 * @license MIT */ + +import * as detect from './utils/hasColoursSupported'; import Colours from './data/Colours'; import Styles from './data/Styles'; import Keywords from './data/Keywords'; import ShortCodes from './data/ShortCodes'; -const isNode = typeof process !== 'undefined'; -const hasColors = typeof process.stdout?.hasColors !== 'undefined'; - -/** - * Check if colours are supported (returns false on browser) - */ -const colorsEnabled = !isNode ? false : ( - (!('NO_COLOR' in process.env) && process.env.FORCE_COLOR !== '0') || - (hasColors ? process.stdout.hasColors() : false) -); - -let enabled = colorsEnabled; +let colorsEnabled = detect.hasColoursSupported(); /** * Change the colour of the given text (List: https://docs.davidcralph.co.uk/#/leeks) @@ -27,7 +18,7 @@ let enabled = colorsEnabled; */ const colours = []; for (const c in Colours) { - colours[c] = (t: string) => enabled ? `\x1b[${Colours[c]}m${t}\x1b[0m` : t; + colours[c] = (t: string) => colorsEnabled ? `\x1b[${Colours[c]}m${t}\x1b[0m` : t; } /** @@ -36,7 +27,7 @@ for (const c in Colours) { */ const styles = []; for (const s in Styles) { - styles[s] = (t: string) => enabled ? `\x1b[${Styles[s]}m${t}\x1b[0m` : t; + styles[s] = (t: string) => colorsEnabled ? `\x1b[${Styles[s]}m${t}\x1b[0m` : t; } /** @@ -45,7 +36,7 @@ for (const s in Styles) { */ const keywords = []; for (const k in Keywords) { - keywords[k] = (t: string) => enabled ? rgb(Keywords[k], t) : t; + keywords[k] = (t: string) => colorsEnabled ? rgb(Keywords[k], t) : t; } /** @@ -54,7 +45,7 @@ for (const k in Keywords) { */ const bgKeywords = []; for (const k in Keywords) { - bgKeywords[k] = (t: string) => enabled ? rgbBg(Keywords[k], t) : t; + bgKeywords[k] = (t: string) => colorsEnabled ? rgbBg(Keywords[k], t) : t; } /** @@ -63,7 +54,7 @@ for (const k in Keywords) { * @param {string} t The text to show with the 8-bit colour */ export function eightBit(i: string, t: string) { - if (!enabled) { + if (!colorsEnabled) { return t; } @@ -76,7 +67,7 @@ export function eightBit(i: string, t: string) { * @param {string} t The text to show with the 8-bit colour */ export function eightBitBg(i: string, t: string) { - if (!enabled) { + if (!colorsEnabled) { return t; } @@ -89,7 +80,7 @@ export function eightBitBg(i: string, t: string) { * @param {string} t The text to show with the RGB colour */ export function rgb(rgb: [number, number, number], t: string) { - if (!enabled) { + if (!colorsEnabled) { return t; } @@ -103,7 +94,7 @@ export function rgb(rgb: [number, number, number], t: string) { * @param {string} t The text to show with the RGB colour */ export function rgbBg(rgb: [number, number, number], t: string) { - if (!enabled) { + if (!colorsEnabled) { return t; } @@ -138,7 +129,7 @@ export function hexBg(hex: string, t: string) { * @param {string} t The text to format */ export function short(t: string) { - return enabled + return colorsEnabled ? t .replace(/&!?[0-9a-f]/gi, code => `\x1b[${Colours[ShortCodes.colours[code]]}m`) .replace(/&[i-pr]/gi, code => `\x1b[${Styles[ShortCodes.styles[code]]}m`) @@ -174,15 +165,15 @@ export function alias(name: string, type: string, value: string) { * Enable colour support for leeks.js */ export function enableColours() { - enabled = true; -}; + colorsEnabled = true; +} /** * Disable colour support for leeks.js */ export function disableColours() { - enabled = false; -}; + colorsEnabled = false; +} export { colours as colors, diff --git a/src/utils/hasColoursSupported.ts b/src/utils/hasColoursSupported.ts new file mode 100644 index 0000000..d0889a1 --- /dev/null +++ b/src/utils/hasColoursSupported.ts @@ -0,0 +1,48 @@ +const isNode = typeof process !== 'undefined'; +const truthy = new Set(['yes', 'true', '1', 'enable', 'e', 'enabled']); +const falsy = new Set(['no', 'false', '0', 'disable', 'd', 'disabled']); + +/** + * Method to detect if the current host has colors enabled or not. The result is cached on the first + * invocation of {@link hasColoursSupported}, the rest will just return that variable. + */ +export function hasColoursSupported() { + // Check if we have the `process` global available. If not, let's not enable it. + if (!isNode) { + return false; + } + + // Check if the `NO_COLOR` system enviornment variable is available. + if ('NO_COLOR' in process.env) { + return false; + } + + // Check if `FORCE_COLORS` exists and see if it is truthy. + if ('FORCE_COLORS' in process.env) { + // Check if it is a truthy value + if (truthy.has(process.env.FORCE_COLORS)) { + return true; + } + + // Check if `FORCE_COLORS` was a falsy value + if (falsy.has(process.env.FORCE_COLORS)) { + return true; + } + } + + // Check if any CI service supports colours + if ('CI' in process.env) { + return ['GITHUB_ACTIONS', 'TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(i => i in process.env) || ( + typeof process.env.CI_NAME !== undefined && process.env.CI_NAME === 'codeship' + ); + } + + // Check if the process' standard output has colours enabled from the `hasColors` function. + if (typeof process.stdout?.hasColors !== 'undefined') { + return process.stdout.hasColors(); + } + + // Otherwise, fallback to false + console.log('fallback'); + return false; +} From a6ac3d33a87eeae6018bdf50c7fd0b86a4dba959 Mon Sep 17 00:00:00 2001 From: Noel Date: Wed, 28 Dec 2022 15:10:17 -0700 Subject: [PATCH 2/2] remove console.log --- src/utils/hasColoursSupported.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/hasColoursSupported.ts b/src/utils/hasColoursSupported.ts index d0889a1..88a822e 100644 --- a/src/utils/hasColoursSupported.ts +++ b/src/utils/hasColoursSupported.ts @@ -43,6 +43,5 @@ export function hasColoursSupported() { } // Otherwise, fallback to false - console.log('fallback'); return false; }