LogMessageAnsi.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { PureComponent } from 'react';
  2. import ansicolor from 'vendor/ansicolor/ansicolor';
  3. interface Style {
  4. [key: string]: string;
  5. }
  6. interface ParsedChunk {
  7. style: Style;
  8. text: string;
  9. }
  10. function convertCSSToStyle(css: string): Style {
  11. return css.split(/;\s*/).reduce((accumulated, line) => {
  12. const match = line.match(/([^:\s]+)\s*:\s*(.+)/);
  13. if (match && match[1] && match[2]) {
  14. const key = match[1].replace(/-(a-z)/g, (_, character) => character.toUpperCase());
  15. accumulated[key] = match[2];
  16. }
  17. return accumulated;
  18. }, {});
  19. }
  20. interface Props {
  21. value: string;
  22. }
  23. interface State {
  24. chunks: ParsedChunk[];
  25. prevValue: string;
  26. }
  27. export class LogMessageAnsi extends PureComponent<Props, State> {
  28. state = {
  29. chunks: [],
  30. prevValue: '',
  31. };
  32. static getDerivedStateFromProps(props: Props, state: State) {
  33. if (props.value === state.prevValue) {
  34. return null;
  35. }
  36. const parsed = ansicolor.parse(props.value);
  37. return {
  38. chunks: parsed.spans.map(span => {
  39. return span.css
  40. ? {
  41. style: convertCSSToStyle(span.css),
  42. text: span.text,
  43. }
  44. : { text: span.text };
  45. }),
  46. prevValue: props.value,
  47. };
  48. }
  49. render() {
  50. const { chunks } = this.state;
  51. return chunks.map((chunk, index) =>
  52. chunk.style ? (
  53. <span key={index} style={chunk.style}>
  54. {chunk.text}
  55. </span>
  56. ) : (
  57. chunk.text
  58. )
  59. );
  60. }
  61. }