Skip to content

Commit

Permalink
feat: alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcralph committed Apr 7, 2021
1 parent 9f222b2 commit f2c6202
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Simple ANSI styling for your terminal.
* Ultra lightweight (0 dependencies and small file size)
* [Chalk](https://github.com/chalk/chalk)-like API
* Supports 4-bit, 8-bit, full RGB, hex and CSS keywords
* Automatic colour support detection with functions to manually disable/enable it
* Set alias for colours and styles with leeks.alias()

## Usage
See the [documentation](https://docs.davidjcralph.co.uk/#/leeks) and [example](https://github.com/davidjcralph/leeks.js/blob/main/example/test.js)
Expand Down
7 changes: 6 additions & 1 deletion example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ leeks.disableColours();
console.log(error('test'));

leeks.enableColours();
console.log(error('test'));
console.log(error('test'));

console.log(leeks.colours.blue`hello there`);

leeks.alias('primary', leeks.colours.green);
console.log(leeks.colours.primary('hi'));
70 changes: 44 additions & 26 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,61 @@ declare module 'leeks.js' {
'bgBlack' | 'bgRed' | 'bgGreen' | 'bgYellow' | 'bgBlue' | 'bgMagenta' | 'bgCyan' | 'bgWhite' |
'bgBlackBright' | 'bgRedBright' | 'bgGreenBright' | 'bgBlueBright' | 'bgMagentaBright' | 'bgCyanBright' | 'bgWhiteBright' | 'bgYellowBright' |
'bgGray' | 'bgGrey';
type Keywords = 'black' | 'silver' | 'gray' | 'white' | 'maroon' | 'red' | 'purple' | 'fuchsia' | 'green' | 'lime' | 'olive' |
'yellow' | 'navy' | 'blue' | 'teal' | 'aqua' | 'aliceblue' | 'antiquewhite' | 'aquamarine' | 'azure' | 'beige' | 'bisque' | 'blanchedalmond' | 'blueviolet' | 'brown' | 'burlywood' | 'cadetblue' |
'chartreuse' | 'chocolate' | 'coral' | 'cornflowerblue' | 'cornsilk' | 'crimson' | 'cyan' | 'darkblue' | 'darkcyan' | 'darkgoldenrod' | 'darkgray' | 'darkgreen' | 'darkgrey' | 'darkkhaki' |
'darkmagenta' | 'darkolivegreen' | 'darkorange' | 'darkorchid' | 'darkred' | 'darksalmon' | 'darkseagreen' | 'darkslateblue' | 'darkslategray' | 'darkslategrey' | 'darkturquoise' | 'darkviolet' |
'deeppink' | 'deepskyblue' | 'dimgray' | 'dimgrey' | 'dodgerblue' | 'firebrick' | 'floralwhite' | 'forestgreen' | 'gainsboro' | 'ghostwhite' | 'gold' | 'goldenrod' | 'greenyellow' | 'grey' | 'honeydew' |
'hotpink' | 'indianred' | 'indigo' | 'ivory' | 'khaki' | 'lavender' | 'lavenderblush' | 'lawngreen' | 'lemonchiffon' | 'lightblue' | 'lightcoral' | 'lightcyan' | 'lightgoldenrodyellow' | 'lightgray' | 'lightgreen' |
type Keywords = 'black' | 'silver' | 'gray' | 'white' | 'maroon' | 'red' | 'purple' | 'fuchsia' | 'green' | 'lime' | 'olive' |
'yellow' | 'navy' | 'blue' | 'teal' | 'aqua' | 'aliceblue' | 'antiquewhite' | 'aquamarine' | 'azure' | 'beige' | 'bisque' | 'blanchedalmond' | 'blueviolet' | 'brown' | 'burlywood' | 'cadetblue' |
'chartreuse' | 'chocolate' | 'coral' | 'cornflowerblue' | 'cornsilk' | 'crimson' | 'cyan' | 'darkblue' | 'darkcyan' | 'darkgoldenrod' | 'darkgray' | 'darkgreen' | 'darkgrey' | 'darkkhaki' |
'darkmagenta' | 'darkolivegreen' | 'darkorange' | 'darkorchid' | 'darkred' | 'darksalmon' | 'darkseagreen' | 'darkslateblue' | 'darkslategray' | 'darkslategrey' | 'darkturquoise' | 'darkviolet' |
'deeppink' | 'deepskyblue' | 'dimgray' | 'dimgrey' | 'dodgerblue' | 'firebrick' | 'floralwhite' | 'forestgreen' | 'gainsboro' | 'ghostwhite' | 'gold' | 'goldenrod' | 'greenyellow' | 'grey' | 'honeydew' |
'hotpink' | 'indianred' | 'indigo' | 'ivory' | 'khaki' | 'lavender' | 'lavenderblush' | 'lawngreen' | 'lemonchiffon' | 'lightblue' | 'lightcoral' | 'lightcyan' | 'lightgoldenrodyellow' | 'lightgray' | 'lightgreen' |
'lightgrey' | 'lightpink' | 'lightsalmon' | 'lightseagreen' | 'lightskyblue' | 'lightslategray' | 'lightslategrey' | 'lightsteelblue' | 'lightyellow' | 'limegreen' | 'linen' | 'magenta' | 'mediumaquamarine' | 'mediumblue' |
'mediumorchid' | 'mediumpurple' | 'mediumseagreen' | 'mediumslateblue' | 'mediumspringgreen' | 'mediumturquoise' | 'mediumvioletred' | 'midnightblue' | 'mintcream' | 'mistyrose' | 'moccasin' | 'navajowhite' | 'oldlace' |
'olivedrab' | 'orange' | 'orangered' | 'orchid' | 'palegoldenrod' | 'palegreen' | 'paleturquoise' | 'palevioletred' | 'papayawhip' | 'peachpuff' | 'peru' | 'pink' | 'plum' | 'powderblue' | 'rosybrown' | 'royalblue' |
'saddlebrown' | 'salmon' | 'sandybrown' | 'seagreen' | 'seashell' | 'sienna' | 'skyblue' | 'slateblue' | 'slategray' | 'slategrey' | 'snow' | 'springgreen' | 'steelblue' | 'tan' | 'thistle' | 'tomato' | 'turquoise' |
'mediumorchid' | 'mediumpurple' | 'mediumseagreen' | 'mediumslateblue' | 'mediumspringgreen' | 'mediumturquoise' | 'mediumvioletred' | 'midnightblue' | 'mintcream' | 'mistyrose' | 'moccasin' | 'navajowhite' | 'oldlace' |
'olivedrab' | 'orange' | 'orangered' | 'orchid' | 'palegoldenrod' | 'palegreen' | 'paleturquoise' | 'palevioletred' | 'papayawhip' | 'peachpuff' | 'peru' | 'pink' | 'plum' | 'powderblue' | 'rosybrown' | 'royalblue' |
'saddlebrown' | 'salmon' | 'sandybrown' | 'seagreen' | 'seashell' | 'sienna' | 'skyblue' | 'slateblue' | 'slategray' | 'slategrey' | 'snow' | 'springgreen' | 'steelblue' | 'tan' | 'thistle' | 'tomato' | 'turquoise' |
'violet' | 'wheat' | 'whitesmoke' | 'yellowgreen';

/** Alias for `colours` */
export const colors: { [x in Colors]: (t: string) => string; };
export const colors: {
[x in Colors]: (t: string) => string;
};

/**
* Change the colour of the given text (List: https://docs.davidjcralph.co.uk/#/leeks)
* @param {string} t The text to change the colour of
*/
export const colours: { [x in Colors]: (t: string) => string };
export const colours: {
[x in Colors]: (t: string) => string
};

/**
* Change the style of the given text (List: https://docs.davidjcralph.co.uk/#/leeks)
* @param {string} t The text to change the style of
*/
export const styles: { [x in Styles]: (t: string) => string };
export const styles: {
[x in Styles]: (t: string) => string
};

/**
* Change the colour of the given text (List: https://docs.davidjcralph.co.uk/#/leeks)
* @param {string} t The text to change the colour of
*/
export const keywords: { [x in Keywords]: (t: string) => string };
export const keywords: {
[x in Keywords]: (t: string) => string
};

/**
* Change the background colour of the given text (List: https://docs.davidjcralph.co.uk/#/leeks)
* @param {string} t The text to change the colour of
*/
export const bgKeywords: { [x in Keywords]: (t: string) => string };
export const bgKeywords: {
[x in Keywords]: (t: string) => string
};

/** Alias for `supportsColour` */
export const supportsColor: boolean;

/** Check if colours are supported (From https://github.com/jorgebucaran/colorette) */
export const supportsColour: boolean;

/** Enable colour support for leeks.js */
export type enableColours = () => void;

/** Alias for `enableColours` */
export type enableColors = () => void;

/** Disable colour support for leeks.js */
export type disableColours = () => void;

/** Alias for `disabeColours` */
export type disableColors = () => void;

/**
* Change the background colour of the given text using 8-bit colours
* @param i The 8-bit colour to use
Expand Down Expand Up @@ -105,4 +103,24 @@ declare module 'leeks.js' {
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
*/
export const hexBg: (hex: string, t: string) => string;

/**
* Set an alias
* @param {string} name The name of the alias
* @param {string} type Either "colour" or "style"
* @param {string} value The colour/style you want to use, e.g leeks.colours.green
*/
export type alias = () => void;

/** Enable colour support for leeks.js */
export type enableColours = () => void;

/** Alias for `enableColours` */
export type enableColors = () => void;

/** Disable colour support for leeks.js */
export type disableColours = () => void;

/** Alias for `disabeColours` */
export type disableColors = () => void;
}
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ for (const k in Keywords) bgKeywords[k] = (t: string) => enabled ? rgbBg(Keyword
export function eightBit(i: string, t: string) {
if (!enabled) return t;
return '\033' + `[38;5;${i}m${t}\x1b[0m`;
}
};

/**
* Change the background colour of the given text using 8-bit colours
Expand All @@ -62,7 +62,7 @@ export function eightBit(i: string, t: string) {
export function eightBitBg(i: string, t: string) {
if (!enabled) return t;
return '\033' + `[48;5;${i}m${t}\x1b[0m`;
}
};

/**
* Change the colour of the given text using RGB
Expand Down Expand Up @@ -110,19 +110,30 @@ export function hexBg(hex: string, t: string) {
return rgbBg([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};


/**
* Set an alias
* @param {string} name The name of the alias
* @param {string} type Either "colour" or "style"
* @param {string} value The colour/style you want to use, e.g leeks.colours.green
*/
export function alias(name: string, type: string, value: string) {
[type][name] = value;
};

/**
* Enable colour support for leeks.js
*/
export function enableColours() {
enabled = true;
}
};

/**
* Disable colour support for leeks.js
*/
export function disableColours() {
enabled = false;
}
};

export {
colours as colors,
Expand Down

0 comments on commit f2c6202

Please sign in to comment.