diff --git a/example/test.js b/example/test.js index 11f94ee..7439536 100644 --- a/example/test.js +++ b/example/test.js @@ -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 ✏HEX&r &!#009999&0more HEX')); \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index adc15cb..aad2075 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 diff --git a/src/data/ShortCodes.ts b/src/data/ShortCodes.ts new file mode 100644 index 0000000..41b1966 --- /dev/null +++ b/src/data/ShortCodes.ts @@ -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' + } +}; diff --git a/src/index.ts b/src/index.ts index e0c2e84..8730610 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -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`; @@ -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`; @@ -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; @@ -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; @@ -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 @@ -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"'); } };