Skip to content

Commit

Permalink
Merge pull request #16 from eartharoid/main
Browse files Browse the repository at this point in the history
Add short codes
  • Loading branch information
davidcralph authored Sep 28, 2021
2 parents 06bff07 + 5b592a3 commit dc6cd6a
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 13 deletions.
2 changes: 2 additions & 0 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ 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'));
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ declare module 'leeks.js' {
*/
export const hexBg: (hex: string, t: string) => string;

/**
* Add colours and styles to a string using short codes
* @param {string} t The text to format
*/
export const short: (t: string) => string;

/**
* Set an alias
* @param {string} name The name of the alias
Expand Down
47 changes: 47 additions & 0 deletions src/data/ShortCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default {
colours: {
'&!0': 'bgBlack',
'&!1': 'bgBlue',
'&!2': 'bgGreen',
'&!3': 'bgCyan',
'&!4': 'bgRed',
'&!5': 'bgMagenta',
'&!6': 'bgYellow',
'&!7': 'bgBlackBright',
'&!8': 'bgWhiteBright',
'&!9': 'bgBlueBright',
'&!a': 'bgGreenBright',
'&!b': 'bgCyanBright',
'&!c': 'bgRedBright',
'&!d': 'bgMagentaBright',
'&!e': 'bgYellowBright',
'&!f': 'bgWhite',
'&0': 'black',
'&1': 'blue',
'&2': 'green',
'&3': 'cyan',
'&4': 'red',
'&5': 'magenta',
'&6': 'yellow',
'&7': 'blackBright',
'&8': 'whiteBright',
'&9': 'blueBright',
'&a': 'greenBright',
'&b': 'cyanBright',
'&c': 'redBright',
'&d': 'magentaBright',
'&e': 'yellowBright',
'&f': 'white'
},
styles: {
'&i': 'inverse',
'&j': 'dim',
'&k': 'blink',
'&l': 'bold',
'&m': 'strikethrough',
'&n': 'underline',
'&o': 'italic',
'&p': 'overline',
'&r': 'reset'
}
};
44 changes: 31 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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';

Expand Down Expand Up @@ -63,7 +64,7 @@ for (const k in Keywords) {
*/
export function eightBit(i: string, t: string) {
if (!enabled) {
return t;
return t;
}

return '\033' + `[38;5;${i}m${t}\x1b[0m`;
Expand All @@ -76,7 +77,7 @@ export function eightBit(i: string, t: string) {
*/
export function eightBitBg(i: string, t: string) {
if (!enabled) {
return t;
return t;
}

return '\033' + `[48;5;${i}m${t}\x1b[0m`;
Expand All @@ -89,7 +90,7 @@ export function eightBitBg(i: string, t: string) {
*/
export function rgb(rgb: [number, number, number], t: string) {
if (!enabled) {
return t;
return t;
}

const [r, g, b] = rgb;
Expand All @@ -103,7 +104,7 @@ export function rgb(rgb: [number, number, number], t: string) {
*/
export function rgbBg(rgb: [number, number, number], t: string) {
if (!enabled) {
return t;
return t;
}

const [r, g, b] = rgb;
Expand Down Expand Up @@ -132,6 +133,23 @@ export function hexBg(hex: string, t: string) {
return rgbBg([(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255], t);
};

/**
* Add colours and styles to a string using short codes
* @param {string} t The text to format
*/
export function short(t: string) {
return enabled
? t
.replace(/&!?[0-9a-f]/gi, code => `\x1b[${Colours[ShortCodes.colours[code]]}m`)
.replace(/&[i-pr]/gi, code => `\x1b[${Styles[ShortCodes.styles[code]]}m`)
.replace(/&!?#([0-9A-Fa-f]{3,6})/gi, (match, code) => {
const bigint = parseInt(code, 16);
const [r, g, b] = [(bigint >> 16) & 255, (bigint >> 8) & 255, bigint & 255];
return `\x1b[${match.includes('!') ? '48' : '38'};2;${r};${g};${b}m`;
}) + '\x1b[0m'
: t;
}

/**
* Set an alias
* @param {string} name The name of the alias
Expand All @@ -140,15 +158,15 @@ export function hexBg(hex: string, t: string) {
*/
export function alias(name: string, type: string, value: string) {
switch (type) {
case 'colors':
case 'colours':
colours[name] = value;
break;
case 'styles':
styles[name] = value;
break;
default:
throw new Error('Must be "colours", "colors" or "styles"');
case 'colors':
case 'colours':
colours[name] = value;
break;
case 'styles':
styles[name] = value;
break;
default:
throw new Error('Must be "colours", "colors" or "styles"');
}
};

Expand Down

0 comments on commit dc6cd6a

Please sign in to comment.