2019-02-07 10:44:09 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2019-03-06 07:22:59 -06:00
|
|
|
import ansicolor from 'vendor/ansicolor/ansicolor';
|
2019-02-07 10:44:09 -06:00
|
|
|
|
|
|
|
interface Style {
|
|
|
|
[key: string]: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ParsedChunk {
|
|
|
|
style: Style;
|
|
|
|
text: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function convertCSSToStyle(css: string): Style {
|
|
|
|
return css.split(/;\s*/).reduce((accumulated, line) => {
|
|
|
|
const match = line.match(/([^:\s]+)\s*:\s*(.+)/);
|
|
|
|
|
|
|
|
if (match && match[1] && match[2]) {
|
|
|
|
const key = match[1].replace(/-(a-z)/g, (_, character) => character.toUpperCase());
|
2019-07-30 08:49:32 -05:00
|
|
|
// @ts-ignore
|
2019-02-07 10:44:09 -06:00
|
|
|
accumulated[key] = match[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
return accumulated;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
chunks: ParsedChunk[];
|
|
|
|
prevValue: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LogMessageAnsi extends PureComponent<Props, State> {
|
2019-07-30 08:49:32 -05:00
|
|
|
state: State = {
|
2019-02-07 10:44:09 -06:00
|
|
|
chunks: [],
|
|
|
|
prevValue: '',
|
|
|
|
};
|
|
|
|
|
2019-02-22 06:13:10 -06:00
|
|
|
static getDerivedStateFromProps(props: Props, state: State) {
|
2019-02-07 10:44:09 -06:00
|
|
|
if (props.value === state.prevValue) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parsed = ansicolor.parse(props.value);
|
|
|
|
|
|
|
|
return {
|
2019-02-16 08:45:19 -06:00
|
|
|
chunks: parsed.spans.map(span => {
|
|
|
|
return span.css
|
|
|
|
? {
|
|
|
|
style: convertCSSToStyle(span.css),
|
|
|
|
text: span.text,
|
|
|
|
}
|
|
|
|
: { text: span.text };
|
2019-02-07 10:44:09 -06:00
|
|
|
}),
|
2019-02-16 08:45:19 -06:00
|
|
|
prevValue: props.value,
|
2019-02-07 10:44:09 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { chunks } = this.state;
|
|
|
|
|
2019-02-19 08:41:35 -06:00
|
|
|
return chunks.map((chunk, index) =>
|
|
|
|
chunk.style ? (
|
|
|
|
<span key={index} style={chunk.style}>
|
|
|
|
{chunk.text}
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
chunk.text
|
|
|
|
)
|
2019-02-07 10:44:09 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|