|
| 1 | +(() => { |
| 2 | + const escapeHtml = (text) => text |
| 3 | + .replaceAll("&", "&") |
| 4 | + .replaceAll("<", "<") |
| 5 | + .replaceAll(">", ">"); |
| 6 | + |
| 7 | + const colorClasses = { |
| 8 | + 30: "ansi-black", |
| 9 | + 31: "ansi-red", |
| 10 | + 32: "ansi-green", |
| 11 | + 33: "ansi-yellow", |
| 12 | + 34: "ansi-blue", |
| 13 | + 35: "ansi-magenta", |
| 14 | + 36: "ansi-cyan", |
| 15 | + 37: "ansi-white", |
| 16 | + 90: "ansi-black", |
| 17 | + 91: "ansi-red", |
| 18 | + 92: "ansi-green", |
| 19 | + 93: "ansi-yellow", |
| 20 | + 94: "ansi-blue", |
| 21 | + 95: "ansi-magenta", |
| 22 | + 96: "ansi-cyan", |
| 23 | + 97: "ansi-white", |
| 24 | + }; |
| 25 | + |
| 26 | + function activeClasses(state) { |
| 27 | + const classes = []; |
| 28 | + if (state.bold) { |
| 29 | + classes.push("ansi-bold"); |
| 30 | + } |
| 31 | + if (state.dim) { |
| 32 | + classes.push("ansi-dim"); |
| 33 | + } |
| 34 | + if (state.italic) { |
| 35 | + classes.push("ansi-italic"); |
| 36 | + } |
| 37 | + if (state.underline) { |
| 38 | + classes.push("ansi-underline"); |
| 39 | + } |
| 40 | + if (state.color) { |
| 41 | + classes.push(state.color); |
| 42 | + } |
| 43 | + return classes; |
| 44 | + } |
| 45 | + |
| 46 | + function openSpan(state) { |
| 47 | + const classes = activeClasses(state); |
| 48 | + if (classes.length === 0) { |
| 49 | + return ""; |
| 50 | + } |
| 51 | + return `<span class="${classes.join(" ")}">`; |
| 52 | + } |
| 53 | + |
| 54 | + function hasStyle(state) { |
| 55 | + return activeClasses(state).length > 0; |
| 56 | + } |
| 57 | + |
| 58 | + function applyCodes(state, codes) { |
| 59 | + for (const code of codes) { |
| 60 | + if (code === 0) { |
| 61 | + state.bold = false; |
| 62 | + state.dim = false; |
| 63 | + state.italic = false; |
| 64 | + state.underline = false; |
| 65 | + state.color = ""; |
| 66 | + } else if (code === 1) { |
| 67 | + state.bold = true; |
| 68 | + } else if (code === 2) { |
| 69 | + state.dim = true; |
| 70 | + } else if (code === 3) { |
| 71 | + state.italic = true; |
| 72 | + } else if (code === 4) { |
| 73 | + state.underline = true; |
| 74 | + } else if (code === 22) { |
| 75 | + state.bold = false; |
| 76 | + state.dim = false; |
| 77 | + } else if (code === 23) { |
| 78 | + state.italic = false; |
| 79 | + } else if (code === 24) { |
| 80 | + state.underline = false; |
| 81 | + } else if (code === 39) { |
| 82 | + state.color = ""; |
| 83 | + } else if (code in colorClasses) { |
| 84 | + state.color = colorClasses[code]; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + function render(text) { |
| 90 | + const state = { bold: false, dim: false, italic: false, underline: false, color: "" }; |
| 91 | + let output = ""; |
| 92 | + let open = false; |
| 93 | + let index = 0; |
| 94 | + const sgr = /\x1b\[([0-9;]*)m/g; |
| 95 | + for (const match of text.matchAll(sgr)) { |
| 96 | + output += escapeHtml(text.slice(index, match.index)); |
| 97 | + if (open) { |
| 98 | + output += "</span>"; |
| 99 | + open = false; |
| 100 | + } |
| 101 | + const codes = match[1] === "" ? [0] : match[1].split(";").map(Number); |
| 102 | + applyCodes(state, codes); |
| 103 | + if (hasStyle(state)) { |
| 104 | + output += openSpan(state); |
| 105 | + open = true; |
| 106 | + } |
| 107 | + index = match.index + match[0].length; |
| 108 | + } |
| 109 | + output += escapeHtml(text.slice(index)); |
| 110 | + if (open) { |
| 111 | + output += "</span>"; |
| 112 | + } |
| 113 | + return output; |
| 114 | + } |
| 115 | + |
| 116 | + window.rcloneRunnerAnsi = { render }; |
| 117 | +})(); |
0 commit comments