Skip to content

Commit

Permalink
refactor: aggregate command into own component
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Oct 15, 2020
1 parent 9482d11 commit 1b1fa11
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
45 changes: 20 additions & 25 deletions src/components/Terminal/Command.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<div
css={css`
display: grid;
grid-template-columns: 1ch 1fr;
grid-gap: 1ch;
`}
>
<Prompt character={prompt} />
<div
css={css`
color: #fafafa;
white-space: pre;
`}
>
{line.map((token, key) => (
// eslint-disable-next-line react/jsx-key
<span {...getTokenProps({ token, key })} />
const Command = ({ command, getTokenProps }) => {
return (
<>
{command.lines.map((line, idx) => (
<CommandLine
key={`command-${idx}`}
line={line}
prompt={idx > 0 ? '>' : '$'}
getTokenProps={getTokenProps}
/>
))}
</div>
</div>
);

{command.output.map((line, idx) => (
<ShellOutput key={`output-${idx}`} line={line} />
))}
</>
);
};

Command.propTypes = {
line: PropTypes.arrayOf(PropTypes.object).isRequired,
command: PropTypes.object.isRequired,
getTokenProps: PropTypes.func.isRequired,
prompt: PropTypes.oneOf(['$', '>']),
};

export default Command;
35 changes: 35 additions & 0 deletions src/components/Terminal/CommandLine.js
Original file line number Diff line number Diff line change
@@ -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 }) => (
<div
css={css`
display: grid;
grid-template-columns: 1ch 1fr;
grid-gap: 1ch;
`}
>
<Prompt character={prompt} />
<div
css={css`
color: #fafafa;
white-space: pre;
`}
>
{line.map((token, key) => (
// eslint-disable-next-line react/jsx-key
<span {...getTokenProps({ token, key })} />
))}
</div>
</div>
);

CommandLine.propTypes = {
line: PropTypes.arrayOf(PropTypes.object).isRequired,
getTokenProps: PropTypes.func.isRequired,
prompt: PropTypes.oneOf(['$', '>']),
};

export default CommandLine;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { css } from '@emotion/core';

const Output = ({ line }) => (
const ShellOutput = ({ line }) => (
<div
css={css`
color: #fafafa;
Expand Down Expand Up @@ -40,8 +40,8 @@ OUTPUT_COLORS.variable = OUTPUT_COLORS.purple;
OUTPUT_COLORS.success = OUTPUT_COLORS.green;
OUTPUT_COLORS.error = OUTPUT_COLORS.red;

Output.propTypes = {
ShellOutput.propTypes = {
line: PropTypes.arrayOf(PropTypes.object).isRequired,
};

export default Output;
export default ShellOutput;
30 changes: 9 additions & 21 deletions src/components/Terminal/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { Button, useClipboard } from '@newrelic/gatsby-theme-newrelic';
import Highlight from 'prism-react-renderer';
import Prism from 'prismjs';
import Command from './Command';
import Output from './Output';
import theme from './theme';
import rollupIntoCommands from './rollupIntoCommands';

Expand Down Expand Up @@ -96,26 +95,15 @@ const Terminal = ({ children }) => {
>
<code>
<Highlight Prism={Prism} code={code} language="shell">
{({ tokens, getTokenProps }) => {
const commands = rollupIntoCommands(tokens, code);

return commands.map(({ lines, output }, commandIdx) => (
<>
{lines.map((line, idx) => (
<Command
key={`${commandIdx}-${idx}`}
line={line}
prompt={idx > 0 ? '>' : '$'}
getTokenProps={getTokenProps}
/>
))}

{output.map((line, idx) => (
<Output key={`${commandIdx}-${idx}`} line={line} />
))}
</>
));
}}
{({ tokens, getTokenProps }) =>
rollupIntoCommands(tokens, code).map((command, idx) => (
<Command
key={idx}
command={command}
getTokenProps={getTokenProps}
/>
))
}
</Highlight>
</code>
</pre>
Expand Down

0 comments on commit 1b1fa11

Please sign in to comment.