Skip to content

Commit

Permalink
Respect ANSI color codes in check output (#392)
Browse files Browse the repository at this point in the history
Signed-off-by: James Phillips <jamesdphillips@gmail.com>
  • Loading branch information
jamesdphillips committed Nov 9, 2021
1 parent 20a7ad0 commit 5fcc5ff
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@material-ui/system": "^4.9.3",
"@material-ui/utils": "^4.7.1",
"@vx/axis": "^0.0.190",
"ansi-to-html": "^0.7.2",
"apollo-cache": "^1.3.0",
"apollo-cache-inmemory": "^1.5.1",
"apollo-client": "^2.6.2",
Expand All @@ -67,6 +68,7 @@
"d3-scale": "^3.0.0",
"debounce": "^1.2.0",
"deepmerge": "^3.2.0",
"entities": "^2.2.0",
"error-stack-parser": "^2.0.2",
"es6-error": "^4.1.1",
"express": "^4.16.4",
Expand Down
19 changes: 19 additions & 0 deletions src/lib/component/base/ANSIColor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable react/prop-types */

import React from "/vendor/react";
import Skeleton from "./Skeleton";
import { ANSIColorLazyProps } from "./ANSIColorLazy";

export type ANSIColorProps = ANSIColorLazyProps;

const Component = React.lazy(() => import("./ANSIColorLazy"));

const ANSIColor: React.FC<ANSIColorProps> = ({ ref, ...props }) => {
return (
<React.Suspense fallback={<Skeleton variant="text">loading</Skeleton>}>
<Component {...props} />
</React.Suspense>
);
};

export default ANSIColor;
8 changes: 8 additions & 0 deletions src/lib/component/base/ANSIColor.worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Convert from "ansi-to-html";
import { encodeXML } from "entities";

onmessage = message => {
const [data] = message.data.msg;
const convert = new Convert();
postMessage([message.data.key, convert.toHtml(encodeXML(data))]);
};
9 changes: 9 additions & 0 deletions src/lib/component/base/ANSIColorLazy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react";

export interface ANSIColorLazyProps
extends Omit<React.HTMLProps<HTMLSpanElement>, "children"> {
children?: string;
}

declare const ANSIColorLazy: React.FC<ANSIColorLazyProps>;
export default ANSIColorLazy;
36 changes: 36 additions & 0 deletions src/lib/component/base/ANSIColorLazy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable react/prop-types */

import React from "/vendor/react";
import Worker from "./ANSIColor.worker";

const worker = new Worker();
const pending = {};

worker.onmessage = event => {
const [key, data] = event.data;
pending[key].call(this, data);
};

let idx = 0;
function postMessage(msg, callback) {
const key = idx;
pending[idx] = data => {
callback(data);
delete pending[idx];
};
worker.postMessage({ key, msg });
idx += 1;
}

const ANSIColor = ({ children, ...other }) => {
const [result, setResult] = React.useState({ value: "", processed: false });
React.useEffect(() => {
postMessage([children], result => {
setResult({ value: result, processed: true });
});
}, [children, setResult]);

return <code dangerouslySetInnerHTML={{ __html: result.value }} {...other} />;
};

export default ANSIColor;
1 change: 1 addition & 0 deletions src/lib/component/base/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
MobileFullWidthContent,
} from "./AppLayout";
export { default as AvatarIcon } from "./AvatarIcon";
export { default as ANSIColor } from "./ANSIColor";
export { default as ButtonIcon } from "./ButtonIcon";
export { default as ButtonSet } from "./ButtonSet";
export { default as CardHighlight } from "./CardHighlight";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {

import { statusCodeToId } from "/lib/util/checkStatus";
import {
ANSIColor,
Dictionary,
DictionaryKey,
DictionaryValue,
Expand Down Expand Up @@ -291,7 +292,7 @@ class EventDetailsCheckSummary extends React.PureComponent {
<React.Fragment>
<Divider />
<CodeBlock className={classes.code}>
<CardContent>{check.output}</CardContent>
<CardContent><ANSIColor>{check.output}</ANSIColor></CardContent>
</CodeBlock>
</React.Fragment>
) : (
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,13 @@ ansi-to-html@^0.6.11:
dependencies:
entities "^1.1.2"

ansi-to-html@^0.7.2:
version "0.7.2"
resolved "https://registry.yarnpkg.com/ansi-to-html/-/ansi-to-html-0.7.2.tgz#a92c149e4184b571eb29a0135ca001a8e2d710cb"
integrity sha512-v6MqmEpNlxF+POuyhKkidusCHWWkaLcGRURzivcU3I9tv7k4JVhFcnukrM5Rlk2rUywdZuzYAZ+kbZqWCnfN3g==
dependencies:
entities "^2.2.0"

anymatch@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-2.0.0.tgz#bcb24b4f37934d9aa7ac17b4adaf89e7c76ef2eb"
Expand Down Expand Up @@ -5289,6 +5296,11 @@ enhanced-resolve@^4.1.0:
memory-fs "^0.4.0"
tapable "^1.0.0"

entities@2, entities@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==

entities@^1.1.2, entities@~1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
Expand Down

0 comments on commit 5fcc5ff

Please sign in to comment.