Skip to content

Commit

Permalink
html mode vs text mode
Browse files Browse the repository at this point in the history
pass `html: true` to get the html version. otherwise plain text.
  • Loading branch information
cocktailpeanut committed Mar 21, 2023
1 parent 5719c2d commit 7f43738
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,6 @@ const stripAnsi = (str) => {
const regex = new RegExp(pattern, 'g')
return str.replace(regex, '');
}
const htmlencode = (str) => {
let encodedStr = '';
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
if (charCode < 128) {
// ASCII characters
switch (str[i]) {
case '<': encodedStr += '&lt;'; break;
case '>': encodedStr += '&gt;'; break;
case '&': encodedStr += '&amp;'; break;
case '"': encodedStr += '&quot;'; break;
case '\'': encodedStr += '&#39;'; break;
case '\n': encodedStr += '<br>'; break;
case '\r': break; // ignore
case '\t': encodedStr += '&nbsp;&nbsp;&nbsp;&nbsp;'; break;
case '\b': encodedStr += '&nbsp;'; break;
case '\f': encodedStr += '&nbsp;'; break;
default: encodedStr += String.fromCharCode(charCode); break;
}
} else {
// Non-ASCII characters
encodedStr += "&#" + charCode + ";";
}
}
return encodedStr;
}


class Dalai {
Expand Down Expand Up @@ -86,14 +60,40 @@ class Dalai {
this.torrent = new TorrentDownloader()
this.config = {
name: 'xterm-color',
cols: 200,
cols: 1000,
rows: 30,
}
this.cores = {
llama: new L(this),
alpaca: new A(this),
}
}
htmlencode (str) {
let encodedStr = '';
for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);
if (charCode < 128) {
// ASCII characters
switch (str[i]) {
case '<': encodedStr += '&lt;'; break;
case '>': encodedStr += '&gt;'; break;
case '&': encodedStr += '&amp;'; break;
case '"': encodedStr += '&quot;'; break;
case '\'': encodedStr += '&#39;'; break;
case '\n': encodedStr += '<br>'; break;
case '\r': break; // ignore
case '\t': encodedStr += '&nbsp;&nbsp;&nbsp;&nbsp;'; break;
case '\b': encodedStr += '&nbsp;'; break;
case '\f': encodedStr += '&nbsp;'; break;
default: encodedStr += String.fromCharCode(charCode); break;
}
} else {
// Non-ASCII characters
encodedStr += "&#" + charCode + ";";
}
}
return encodedStr;
}
down(url, dest, headers) {
return new Promise((resolve, reject) => {
const task = path.basename(dest)
Expand Down Expand Up @@ -240,7 +240,11 @@ class Dalai {
await this.exec(`${main_bin_path} ${chunks.join(" ")}`, this.cores[Core].home, (proc, msg) => {
if (endpattern.test(msg)) ended = true
if (started && !ended) {
cb(msg)
if (req.html) {
cb(this.htmlencode(msg))
} else {
cb(msg)
}
} else if (ended && writeEnd) {
cb('\n\n<end>')
writeEnd = false
Expand Down Expand Up @@ -479,7 +483,7 @@ class Dalai {
const ptyProcess = pty.spawn(shell, [], config)
ptyProcess.onData((data) => {
if (cb) {
cb(ptyProcess, htmlencode(stripAnsi(data)))
cb(ptyProcess, stripAnsi(data))
} else {
process.stdout.write(data);
}
Expand Down

0 comments on commit 7f43738

Please sign in to comment.