gamja/lib/ansi.js
2021-06-10 18:11:11 +02:00

53 lines
929 B
JavaScript

// See https://modern.ircdocs.horse/formatting.html
const BOLD = "\x02";
const ITALIC = "\x1D";
const UNDERLINE = "\x1F";
const STRIKETHROUGH = "\x1E";
const MONOSPACE = "\x11";
const COLOR = "\x03";
const COLOR_HEX = "\x04";
const REVERSE_COLOR = "\x16";
const RESET = "\x0F";
function isDigit(ch) {
return ch >= "0" && ch <= "9";
}
export function strip(text) {
let out = "";
for (let i = 0; i < text.length; i++) {
let ch = text[i];
switch (ch) {
case BOLD:
case ITALIC:
case UNDERLINE:
case STRIKETHROUGH:
case MONOSPACE:
case REVERSE_COLOR:
case RESET:
break; // skip
case COLOR:
if (!isDigit(text[i + 1])) {
break;
}
i++;
if (isDigit(text[i + 1])) {
i++;
}
if (text[i + 1] == "," && isDigit(text[i + 2])) {
i += 2;
if (isDigit(text[i + 1])) {
i++;
}
}
break;
case COLOR_HEX:
i += 6;
break;
default:
out += ch;
}
}
return out;
}