forked from CringeStudios/gamja
Fix stripping hex color formatting
Hex colors can be set with the same formats as the regular colors: <CODE>, <CODE><COLOR>, or <CODE><COLOR>,<COLOR>. Previously we only supporteed <CODE><COLOR>. This patch enables stripping colors for all valid color formats. Co-authored-by: Simon Ser <contact@emersion.fr>
This commit is contained in:
parent
3f059567c5
commit
081f5743be
24
lib/ansi.js
24
lib/ansi.js
@ -10,10 +10,26 @@ const COLOR_HEX = "\x04";
|
|||||||
const REVERSE_COLOR = "\x16";
|
const REVERSE_COLOR = "\x16";
|
||||||
const RESET = "\x0F";
|
const RESET = "\x0F";
|
||||||
|
|
||||||
|
const HEX_COLOR_LENGTH = 6;
|
||||||
|
|
||||||
function isDigit(ch) {
|
function isDigit(ch) {
|
||||||
return ch >= "0" && ch <= "9";
|
return ch >= "0" && ch <= "9";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isHexColor(text) {
|
||||||
|
if (text.length < HEX_COLOR_LENGTH) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (let i = 0; i < HEX_COLOR_LENGTH; i++) {
|
||||||
|
let ch = text[i].toUpperCase();
|
||||||
|
let ok = (ch >= "0" && ch <= "9") || (ch >= "A" && ch <= "F");
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
export function strip(text) {
|
export function strip(text) {
|
||||||
let out = "";
|
let out = "";
|
||||||
for (let i = 0; i < text.length; i++) {
|
for (let i = 0; i < text.length; i++) {
|
||||||
@ -43,7 +59,13 @@ export function strip(text) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case COLOR_HEX:
|
case COLOR_HEX:
|
||||||
i += 6;
|
if (!isHexColor(text.slice(i + 1))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += HEX_COLOR_LENGTH;
|
||||||
|
if (text[i + 1] == "," && isHexColor(text.slice(i + 2))) {
|
||||||
|
i += 1 + HEX_COLOR_LENGTH;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
out += ch;
|
out += ch;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user