Skip to content

Commit

Permalink
Merge pull request #40 from auguwu/main
Browse files Browse the repository at this point in the history
Improve TypeScript typings and colour detection
  • Loading branch information
davidcralph authored Dec 28, 2022
2 parents 9da1271 + a6ac3d3 commit a1dda64
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 31 deletions.
2 changes: 1 addition & 1 deletion example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &#009999HEX&r &!#009999&0more HEX'));
console.log(leeks.short('&!3&0&0 Hello &r &2&o&nworld!&r &lBold&r &nUnderlined&r &oItalic&r &#009999HEX&r &!#009999&0more HEX'));
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 16 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,22 @@
* @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)
* @param {string} t The text to change the colour of
*/
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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions src/utils/hasColoursSupported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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
return false;
}

0 comments on commit a1dda64

Please sign in to comment.