Skip to content

Commit

Permalink
feat: background css keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcralph committed Apr 7, 2021
1 parent bc46409 commit 9f222b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ console.log(leeks.hex('#7289da', 'hello'));
console.log(leeks.hexBg('#7289da', 'hello'));

console.log(leeks.keywords.aqua('Hello'));
console.log(leeks.bgKeywords.aqua('Hello'));

leeks.disableColours();
console.log(error('test'));
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ declare module 'leeks.js' {
*/
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 };

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

Expand Down
26 changes: 17 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ for (const s in Styles) styles[s] = (t: string) => `\x1b[${Styles[s]}m${t}\x1b[0
let keywords = [];
for (const k in Keywords) keywords[k] = (t: string) => enabled ? rgb(Keywords[k], t) : t;

/**
* Change the background colour of the given text using CSS keywords (List: https://docs.davidjcralph.co.uk/#/leeks)
* @param {string} t The text to change the colour of
*/
let bgKeywords = [];
for (const k in Keywords) bgKeywords[k] = (t: string) => enabled ? rgbBg(Keywords[k], t) : t;

/**
* Change the colour of the given text using 8-bit colours
* @param {string} i The 8-bit colour to use
* @param {string} t The text to show with the 8-bit colour
*/
export function eightBit (i: string, t: string) {
export function eightBit(i: string, t: string) {
if (!enabled) return t;
return '\033' + `[38;5;${i}m${t}\x1b[0m`;
}
Expand All @@ -52,7 +59,7 @@ export function eightBit (i: string, t: string) {
* @param {string} i The 8-bit colour to use
* @param {string} t The text to show with the 8-bit colour
*/
export function eightBitBg (i: string, t: string) {
export function eightBitBg(i: string, t: string) {
if (!enabled) return t;
return '\033' + `[48;5;${i}m${t}\x1b[0m`;
}
Expand All @@ -62,7 +69,7 @@ export function eightBitBg (i: string, t: string) {
* @param {[number, number, number]} rgb An array of the RGB to use
* @param {string} t The text to show with the RGB colour
*/
export function rgb (rgb: [number, number, number], t: string) {
export function rgb(rgb: [number, number, number], t: string) {
if (!enabled) return t;

const [r, g, b] = rgb;
Expand All @@ -74,7 +81,7 @@ export function rgb (rgb: [number, number, number], t: string) {
* @param {[number, number, number]} rgb An array of the RGB to use
* @param {string} t The text to show with the RGB colour
*/
export function rgbBg (rgb: [number, number, number], t: string) {
export function rgbBg(rgb: [number, number, number], t: string) {
if (!enabled) return t;

const [r, g, b] = rgb;
Expand All @@ -87,7 +94,7 @@ export function rgbBg (rgb: [number, number, number], t: string) {
* @param {string} t The text to show with the hexadecimal colour
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
*/
export function hex (hex: string, t: string) {
export function hex(hex: string, t: string) {
const bigint = parseInt(hex.replace('#', ''), 16);
return rgb([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};
Expand All @@ -98,32 +105,33 @@ export function hex (hex: string, t: string) {
* @param {string} t The text to show with the hexadecimal colour
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
*/
export function hexBg (hex: string, t: string) {
export function hexBg(hex: string, t: string) {
const bigint = parseInt(hex.replace('#', ''), 16);
return rgbBg([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};

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

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

export {
colours as colors,
colours as colors,
supports as supportsColor,
supports as supportsColour,
enableColours as enableColors,
disableColours as disableColors,
colours,
keywords,
bgKeywords,
styles
};

0 comments on commit 9f222b2

Please sign in to comment.