Skip to content

Commit

Permalink
Support colour output in stdout for jupyter and julia engines
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly committed Feb 21, 2025
1 parent a0ff13f commit 1971bd5
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/core/jupyter/jupyter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ async function mdFromCodeCell(
}
md.push(text.join(""));
} else {
md.push(mdOutputStream(stream));
md.push(await mdOutputStream(stream, options));
}
} else if (output.output_type === "error") {
md.push(await mdOutputError(output as JupyterOutputError, options));
Expand Down Expand Up @@ -1766,7 +1766,7 @@ function isMarkdown(output: JupyterOutput, options: JupyterToMarkdownOptions) {
return isDisplayDataType(output, options, displayDataIsMarkdown);
}

function mdOutputStream(output: JupyterOutputStream) {
async function mdOutputStream(output: JupyterOutputStream, options: JupyterToMarkdownOptions) {
let text: string[] = [];
if (typeof output.text === "string") {
text = [output.text];
Expand All @@ -1781,14 +1781,23 @@ function mdOutputStream(output: JupyterOutputStream) {
/<ipython-input.*?>:\d+:\s+/,
"",
);
return mdCodeOutput(
[firstLine, ...text.slice(1)].map(colors.stripColor),
);
text = [firstLine, ...text.slice(1)];
}
}

// normal default handling
return mdCodeOutput(text.map(colors.stripColor));
if (options.toHtml && text.some(hasAnsiEscapeCodes)) {
const linesHTML = await convertToHtmlSpans(text.join("\n"));
return mdMarkdownOutput(
[
"\n::: {.ansi-escaped-output}\n```{=html}\n<pre>",
linesHTML,
"</pre>\n```\n:::\n",
],
);
} else {
// normal default behavior
return mdCodeOutput(text.map(colors.stripColor));
}
}

async function mdOutputError(
Expand Down Expand Up @@ -1864,8 +1873,8 @@ async function mdOutputDisplayData(
// if output is invalid, warn and emit empty
const data = output.data[mimeType] as unknown;
if (!Array.isArray(data) || data.some((s) => typeof s !== "string")) {
return mdWarningOutput(`Unable to process text plain output data
which does not appear to be plain text: ${JSON.stringify(data)}`);
return await mdWarningOutput(`Unable to process text plain output data
which does not appear to be plain text: ${JSON.stringify(data)}`, options);
}
const lines = data as string[];
// pandas inexplicably outputs html tables as text/plain with an enclosing single-quote
Expand Down Expand Up @@ -1900,9 +1909,10 @@ which does not appear to be plain text: ${JSON.stringify(data)}`);
}

// no type match found
return mdWarningOutput(
return await mdWarningOutput(
"Unable to display output for mime type(s): " +
Object.keys(output.data).join(", "),
Object.keys(output.data).join(", "),
options,
);
}

Expand Down Expand Up @@ -2061,12 +2071,12 @@ function mdEnclosedOutput(begin: string, text: string[], end: string) {
return md.join("");
}

function mdWarningOutput(msg: string) {
return mdOutputStream({
async function mdWarningOutput(msg: string, options: JupyterToMarkdownOptions) {
return await mdOutputStream({
output_type: "stream",
name: "stderr",
text: [msg],
});
}, options);
}

function isWarningOutput(output: JupyterOutput) {
Expand Down

0 comments on commit 1971bd5

Please sign in to comment.