Add color when writing to stderr

This commit is contained in:
gingerBill
2022-05-21 16:11:10 +01:00
parent f3d225ca4f
commit b9d523e0b2
+30 -12
View File
@@ -1179,32 +1179,50 @@ function newlineCount(str, substr) {
function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
const MAX_INFO_CONSOLE_LINES = 512; const MAX_INFO_CONSOLE_LINES = 512;
let infoConsoleLines = new Array(); let infoConsoleLines = new Array();
let currentLine = ""; let currentLine = {};
currentLine[false] = "";
currentLine[true] = "";
const addConsoleLine = (line) => { const addConsoleLine = (line, isError) => {
if (!line) { if (!line) {
return; return;
} }
const println = (text) => {
let style = [
"color: #eee",
"background-color: #d20",
"padding: 2px 4px",
"border-radius: 2px",
];
if (isError) {
console.log("%c"+text, style.join(";"));
} else {
console.log(text);
}
};
// Print to console // Print to console
if (line == "\n") { if (line == "\n") {
console.log(currentLine); println(currentLine[isError]);
currentLine = ""; currentLine[isError] = "";
} else if (!line.includes("\n")) { } else if (!line.includes("\n")) {
currentLine = currentLine.concat(line); currentLine[isError] = currentLine[isError].concat(line);
} else { } else {
let lines = line.split("\n"); let lines = line.split("\n");
let printLast = lines.length > 1 && line.endsWith("\n"); let printLast = lines.length > 1 && line.endsWith("\n");
console.log(currentLine.concat(lines[0])); println(currentLine[isError].concat(lines[0]));
currentLine = ""; currentLine[isError] = "";
for (let i = 1; i < lines.length-1; i++) { for (let i = 1; i < lines.length-1; i++) {
console.log(lines[i]); println(lines[i]);
} }
let last = lines[lines.length-1]; let last = lines[lines.length-1];
if (printLast) { if (printLast) {
console.log(last); println(last);
} else { } else {
currentLine = last; currentLine[isError] = last;
} }
} }
@@ -1247,10 +1265,10 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
write: (fd, ptr, len) => { write: (fd, ptr, len) => {
const str = wasmMemoryInterface.loadString(ptr, len); const str = wasmMemoryInterface.loadString(ptr, len);
if (fd == 1) { if (fd == 1) {
addConsoleLine(str); addConsoleLine(str, false);
return; return;
} else if (fd == 2) { } else if (fd == 2) {
addConsoleLine(str); addConsoleLine(str, true);
return; return;
} else { } else {
throw new Error("Invalid fd to 'write'" + stripNewline(str)); throw new Error("Invalid fd to 'write'" + stripNewline(str));