From 1b1fa11c2f4f8b3c1ff885e4d6eeab7e90b838a0 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 14 Oct 2020 23:07:16 -0700 Subject: [PATCH] refactor: aggregate command into own component --- src/components/Terminal/Command.js | 45 +++++++++---------- src/components/Terminal/CommandLine.js | 35 +++++++++++++++ .../Terminal/{Output.js => ShellOutput.js} | 6 +-- src/components/Terminal/Terminal.js | 30 ++++--------- 4 files changed, 67 insertions(+), 49 deletions(-) create mode 100644 src/components/Terminal/CommandLine.js rename src/components/Terminal/{Output.js => ShellOutput.js} (91%) diff --git a/src/components/Terminal/Command.js b/src/components/Terminal/Command.js index 3c5db61e1..17d15a672 100644 --- a/src/components/Terminal/Command.js +++ b/src/components/Terminal/Command.js @@ -1,35 +1,30 @@ import React from 'react'; import PropTypes from 'prop-types'; -import Prompt from './Prompt'; -import { css } from '@emotion/core'; +import CommandLine from './CommandLine'; +import ShellOutput from './ShellOutput'; -const Command = ({ line, prompt, getTokenProps }) => ( -
- -
- {line.map((token, key) => ( - // eslint-disable-next-line react/jsx-key - +const Command = ({ command, getTokenProps }) => { + return ( + <> + {command.lines.map((line, idx) => ( + 0 ? '>' : '$'} + getTokenProps={getTokenProps} + /> ))} -
-
-); + + {command.output.map((line, idx) => ( + + ))} + + ); +}; Command.propTypes = { - line: PropTypes.arrayOf(PropTypes.object).isRequired, + command: PropTypes.object.isRequired, getTokenProps: PropTypes.func.isRequired, - prompt: PropTypes.oneOf(['$', '>']), }; export default Command; diff --git a/src/components/Terminal/CommandLine.js b/src/components/Terminal/CommandLine.js new file mode 100644 index 000000000..0ea2acc90 --- /dev/null +++ b/src/components/Terminal/CommandLine.js @@ -0,0 +1,35 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import Prompt from './Prompt'; +import { css } from '@emotion/core'; + +const CommandLine = ({ line, prompt, getTokenProps }) => ( +
+ +
+ {line.map((token, key) => ( + // eslint-disable-next-line react/jsx-key + + ))} +
+
+); + +CommandLine.propTypes = { + line: PropTypes.arrayOf(PropTypes.object).isRequired, + getTokenProps: PropTypes.func.isRequired, + prompt: PropTypes.oneOf(['$', '>']), +}; + +export default CommandLine; diff --git a/src/components/Terminal/Output.js b/src/components/Terminal/ShellOutput.js similarity index 91% rename from src/components/Terminal/Output.js rename to src/components/Terminal/ShellOutput.js index 053b6d12c..0d5a7beb4 100644 --- a/src/components/Terminal/Output.js +++ b/src/components/Terminal/ShellOutput.js @@ -2,7 +2,7 @@ import React from 'react'; import PropTypes from 'prop-types'; import { css } from '@emotion/core'; -const Output = ({ line }) => ( +const ShellOutput = ({ line }) => (
{ > - {({ tokens, getTokenProps }) => { - const commands = rollupIntoCommands(tokens, code); - - return commands.map(({ lines, output }, commandIdx) => ( - <> - {lines.map((line, idx) => ( - 0 ? '>' : '$'} - getTokenProps={getTokenProps} - /> - ))} - - {output.map((line, idx) => ( - - ))} - - )); - }} + {({ tokens, getTokenProps }) => + rollupIntoCommands(tokens, code).map((command, idx) => ( + + )) + }