aboutsummaryrefslogtreecommitdiff
path: root/lib/ansi.js
blob: 86cbbb76f36151216d889079d9ea686522b6cf3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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";

const HEX_COLOR_LENGTH = 6;

function isDigit(ch) {
	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) {
	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:
			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;
		default:
			out += ch;
		}
	}
	return out;
}