LogMessageAnsi.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // @ts-ignore
  16. accumulated[key] = match[2];
  17. }
  18. return accumulated;
  19. }, {});
  20. }
  21. interface Props {
  22. value: string;
  23. }
  24. interface State {
  25. chunks: ParsedChunk[];
  26. prevValue: string;
  27. }
  28. export class LogMessageAnsi extends PureComponent<Props, State> {
  29. state: State = {
  30. chunks: [],
  31. prevValue: '',
  32. };
  33. static getDerivedStateFromProps(props: Props, state: State) {
  34. if (props.value === state.prevValue) {
  35. return null;
  36. }
  37. const parsed = ansicolor.parse(props.value);
  38. return {
  39. chunks: parsed.spans.map(span => {
  40. return span.css
  41. ? {
  42. style: convertCSSToStyle(span.css),
  43. text: span.text,
  44. }
  45. : { text: span.text };
  46. }),
  47. prevValue: props.value,
  48. };
  49. }
  50. render() {
  51. const { chunks } = this.state;
  52. return chunks.map((chunk, index) =>
  53. chunk.style ? (
  54. <span key={index} style={chunk.style}>
  55. {chunk.text}
  56. </span>
  57. ) : (
  58. chunk.text
  59. )
  60. );
  61. }
  62. }