Skip to content

Commit 9f222b2

Browse files
committed
feat: background css keywords
1 parent bc46409 commit 9f222b2

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

example/test.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ console.log(leeks.hex('#7289da', 'hello'));
2222
console.log(leeks.hexBg('#7289da', 'hello'));
2323

2424
console.log(leeks.keywords.aqua('Hello'));
25+
console.log(leeks.bgKeywords.aqua('Hello'));
2526

2627
leeks.disableColours();
2728
console.log(error('test'));

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ declare module 'leeks.js' {
3838
*/
3939
export const keywords: { [x in Keywords]: (t: string) => string };
4040

41+
/**
42+
* Change the background colour of the given text (List: https://docs.davidjcralph.co.uk/#/leeks)
43+
* @param {string} t The text to change the colour of
44+
*/
45+
export const bgKeywords: { [x in Keywords]: (t: string) => string };
46+
4147
/** Alias for `supportsColour` */
4248
export const supportsColor: boolean;
4349

src/index.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ for (const s in Styles) styles[s] = (t: string) => `\x1b[${Styles[s]}m${t}\x1b[0
3737
let keywords = [];
3838
for (const k in Keywords) keywords[k] = (t: string) => enabled ? rgb(Keywords[k], t) : t;
3939

40+
/**
41+
* Change the background colour of the given text using CSS keywords (List: https://docs.davidjcralph.co.uk/#/leeks)
42+
* @param {string} t The text to change the colour of
43+
*/
44+
let bgKeywords = [];
45+
for (const k in Keywords) bgKeywords[k] = (t: string) => enabled ? rgbBg(Keywords[k], t) : t;
46+
4047
/**
4148
* Change the colour of the given text using 8-bit colours
4249
* @param {string} i The 8-bit colour to use
4350
* @param {string} t The text to show with the 8-bit colour
4451
*/
45-
export function eightBit (i: string, t: string) {
52+
export function eightBit(i: string, t: string) {
4653
if (!enabled) return t;
4754
return '\033' + `[38;5;${i}m${t}\x1b[0m`;
4855
}
@@ -52,7 +59,7 @@ export function eightBit (i: string, t: string) {
5259
* @param {string} i The 8-bit colour to use
5360
* @param {string} t The text to show with the 8-bit colour
5461
*/
55-
export function eightBitBg (i: string, t: string) {
62+
export function eightBitBg(i: string, t: string) {
5663
if (!enabled) return t;
5764
return '\033' + `[48;5;${i}m${t}\x1b[0m`;
5865
}
@@ -62,7 +69,7 @@ export function eightBitBg (i: string, t: string) {
6269
* @param {[number, number, number]} rgb An array of the RGB to use
6370
* @param {string} t The text to show with the RGB colour
6471
*/
65-
export function rgb (rgb: [number, number, number], t: string) {
72+
export function rgb(rgb: [number, number, number], t: string) {
6673
if (!enabled) return t;
6774

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

8087
const [r, g, b] = rgb;
@@ -87,7 +94,7 @@ export function rgbBg (rgb: [number, number, number], t: string) {
8794
* @param {string} t The text to show with the hexadecimal colour
8895
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
8996
*/
90-
export function hex (hex: string, t: string) {
97+
export function hex(hex: string, t: string) {
9198
const bigint = parseInt(hex.replace('#', ''), 16);
9299
return rgb([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
93100
};
@@ -98,32 +105,33 @@ export function hex (hex: string, t: string) {
98105
* @param {string} t The text to show with the hexadecimal colour
99106
* @credit [Stack Overflow](https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb)
100107
*/
101-
export function hexBg (hex: string, t: string) {
108+
export function hexBg(hex: string, t: string) {
102109
const bigint = parseInt(hex.replace('#', ''), 16);
103110
return rgbBg([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
104111
};
105112

106113
/**
107114
* Enable colour support for leeks.js
108115
*/
109-
export function enableColours () {
116+
export function enableColours() {
110117
enabled = true;
111118
}
112119

113120
/**
114121
* Disable colour support for leeks.js
115122
*/
116-
export function disableColours () {
123+
export function disableColours() {
117124
enabled = false;
118125
}
119126

120127
export {
121-
colours as colors,
128+
colours as colors,
122129
supports as supportsColor,
123130
supports as supportsColour,
124131
enableColours as enableColors,
125132
disableColours as disableColors,
126133
colours,
127134
keywords,
135+
bgKeywords,
128136
styles
129137
};

0 commit comments

Comments
 (0)