Strip ANSI escape sequences

References: https://todo.sr.ht/~emersion/gamja/11
This commit is contained in:
Simon Ser 2020-08-04 14:25:05 +02:00
parent b37dfb77fe
commit 96c890f1f5
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 62 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import { html, Component } from "/lib/index.js";
import linkify from "/lib/linkify.js";
import { strip as stripANSI } from "/lib/ansi.js";
import { BufferType } from "/state.js";
const Status = {
@ -29,10 +30,12 @@ export default function BufferHeader(props) {
var serverInfo = props.buffer.serverInfo;
description = `Connected to ${serverInfo.name}`;
} else if (props.buffer.topic) {
description = linkify(props.buffer.topic);
description = linkify(stripANSI(props.buffer.topic));
} else if (props.buffer.who) {
var who = props.buffer.who;
var realname = stripANSI(who.realname || "");
var status = Status.HERE;
if (who.away) {
status = Status.GONE;
@ -41,7 +44,7 @@ export default function BufferHeader(props) {
status = Status.OFFLINE;
}
description = html`<${NickStatus} status=${status}/> ${who.realname} (${who.username}@${who.hostname})`;
description = html`<${NickStatus} status=${status}/> ${realname} (${who.username}@${who.hostname})`;
} else if (props.buffer.offline) {
// User is offline, but we don't have WHO information
description = html`<${NickStatus} status=${Status.OFFLINE}/> ${props.buffer.name}`;

View File

@ -1,6 +1,7 @@
import { html, Component } from "/lib/index.js";
import linkify from "/lib/linkify.js";
import * as irc from "/lib/irc.js";
import { strip as stripANSI } from "/lib/ansi.js";
import { BufferType, getNickURL, getMessageURL } from "/state.js";
function djb2(s) {
@ -64,10 +65,10 @@ class LogLine extends Component {
var action = text.slice(actionPrefix.length, -1);
lineClass = "me-tell";
content = html`* ${createNick(msg.prefix.name)} ${linkify(action)}`;
content = html`* ${createNick(msg.prefix.name)} ${linkify(stripANSI(action))}`;
} else {
lineClass = "talk";
content = html`${"<"}${createNick(msg.prefix.name)}${">"} ${linkify(text)}`;
content = html`${"<"}${createNick(msg.prefix.name)}${">"} ${linkify(stripANSI(text))}`;
}
break;
case "JOIN":
@ -94,7 +95,7 @@ class LogLine extends Component {
case "TOPIC":
var topic = msg.params[1];
content = html`
${createNick(msg.prefix.name)} changed the topic to: ${linkify(topic)}
${createNick(msg.prefix.name)} changed the topic to: ${linkify(stripANSI(topic))}
`;
break;
default:

53
lib/ansi.js Normal file
View File

@ -0,0 +1,53 @@
// 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) {
var out = "";
for (var i = 0; i < text.length; i++) {
var 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;
}