Skip to content

Commit

Permalink
Merge pull request #42 from eartharoid/main
Browse files Browse the repository at this point in the history
fix: `short()` when colour is disabled (closes #41)
  • Loading branch information
davidcralph authored Feb 16, 2023
2 parents a1dda64 + 73c91d7 commit 15f87b3
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,15 @@ export function hexBg(hex: string, t: string) {
* @param {string} t The text to format
*/
export function short(t: string) {
return colorsEnabled
? 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;
return t
.replace(/&!?[0-9a-f]/gi, code => colorsEnabled ? `\x1b[${Colours[ShortCodes.colours[code]]}m` : '')
.replace(/&[i-pr]/gi, code => colorsEnabled ? `\x1b[${Styles[ShortCodes.styles[code]]}m` : '')
.replace(/&!?#([0-9A-Fa-f]{3,6})/gi, (match, code) => {
if (!colorsEnabled) return '';
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`;
}) + (colorsEnabled ? '\x1b[0m' : '');
}

/**
Expand Down

0 comments on commit 15f87b3

Please sign in to comment.