From 0ed5a72b11356f7f019029bd40160f2bff5ff330 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 14 Oct 2020 22:25:18 -0700 Subject: [PATCH] feat: add output coloring for each output line --- src/components/Terminal/Command.js | 35 ++++++++++++++ src/components/Terminal/Output.js | 43 ++++++++++++++++++ src/components/Terminal/SyntaxHighlighter.js | 48 ++++++++------------ 3 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 src/components/Terminal/Command.js create mode 100644 src/components/Terminal/Output.js diff --git a/src/components/Terminal/Command.js b/src/components/Terminal/Command.js new file mode 100644 index 000000000..3c5db61e1 --- /dev/null +++ b/src/components/Terminal/Command.js @@ -0,0 +1,35 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import Prompt from './Prompt'; +import { css } from '@emotion/core'; + +const Command = ({ line, prompt, getTokenProps }) => ( +
+ +
+ {line.map((token, key) => ( + // eslint-disable-next-line react/jsx-key + + ))} +
+
+); + +Command.propTypes = { + line: PropTypes.arrayOf(PropTypes.object).isRequired, + getTokenProps: PropTypes.func.isRequired, + prompt: PropTypes.oneOf(['$', '>']), +}; + +export default Command; diff --git a/src/components/Terminal/Output.js b/src/components/Terminal/Output.js new file mode 100644 index 000000000..947abec93 --- /dev/null +++ b/src/components/Terminal/Output.js @@ -0,0 +1,43 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { css } from '@emotion/core'; + +const Output = ({ line }) => ( +
+ {line.map((token, key) => ( + + {token.text} + + ))} +
+); + +const OUTPUT_COLORS = { + plain: 'currentColor', + green: 'var(--color-nord-14)', + red: 'var(--color-nord-11)', + muted: 'var(--color-nord-3)', + purple: 'var(--color-nord-15)', + blue: 'var(--color-nord-9)', +}; + +OUTPUT_COLORS.timestamp = OUTPUT_COLORS.muted; +OUTPUT_COLORS.variable = OUTPUT_COLORS.purple; +OUTPUT_COLORS.success = OUTPUT_COLORS.green; +OUTPUT_COLORS.error = OUTPUT_COLORS.red; + +Output.propTypes = { + line: PropTypes.arrayOf(PropTypes.object).isRequired, +}; + +export default Output; diff --git a/src/components/Terminal/SyntaxHighlighter.js b/src/components/Terminal/SyntaxHighlighter.js index bc76b4ec3..066e06e77 100644 --- a/src/components/Terminal/SyntaxHighlighter.js +++ b/src/components/Terminal/SyntaxHighlighter.js @@ -3,7 +3,8 @@ import PropTypes from 'prop-types'; import { css } from '@emotion/core'; import Highlight from 'prism-react-renderer'; import Prism from 'prismjs'; -import Prompt from './Prompt'; +import Command from './Command'; +import Output from './Output'; const MULTILINE_COMMAND = /\\\s*$/; const OUTPUT_TAG = /^\[output\]\s/; @@ -11,37 +12,26 @@ const OUTPUT_COLOR_TOKENS = /{([a-z]+)}(.*?(?={|$))/g; const SyntaxHighlighter = ({ code }) => ( - {({ tokens, getLineProps, getTokenProps }) => { + {({ tokens, getTokenProps }) => { const commands = rollupIntoCommands(tokens, code); - return tokens.map((line, idx) => { - const previousLine = collapse(tokens[idx - 1] || []); - const isMultiline = MULTILINE_COMMAND.test(previousLine); - + return commands.map(({ lines, output }, commandIdx) => { return ( - // eslint-disable-next-line react/jsx-key -
-
- ' : '$'} /> -
- {line.map((token, key) => ( - // eslint-disable-next-line react/jsx-key - - ))} -
-
-
+ <> + {lines.map((line, idx) => { + return ( + 0 ? '>' : '$'} + getTokenProps={getTokenProps} + /> + ); + })} + {output.map((line, idx) => ( + + ))} + ); }); }}