Skip to content

Commit 5eb17f3

Browse files
authored
Add example for rendering subprocess output (#406)
1 parent deb9d96 commit 5eb17f3

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

examples/subprocess-output/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
require('import-jsx')('./subprocess-output');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
const childProcess = require('child_process');
3+
const React = require('react');
4+
const stripAnsi = require('strip-ansi');
5+
const {render, Text, Box} = require('../..');
6+
7+
const SubprocessOutput = () => {
8+
const [output, setOutput] = React.useState('');
9+
10+
React.useEffect(() => {
11+
const subProcess = childProcess.spawn('node', ['examples/jest']);
12+
13+
subProcess.stdout.on('data', newOutput => {
14+
const lines = stripAnsi(newOutput.toString('utf8')).split('\n');
15+
setOutput(lines.slice(-5).join('\n'));
16+
});
17+
}, [setOutput]);
18+
19+
return (
20+
<Box flexDirection="column" padding={1}>
21+
<Text>Сommand output:</Text>
22+
<Box marginTop={1}>
23+
<Text>{output}</Text>
24+
</Box>
25+
</Box>
26+
);
27+
};
28+
29+
render(<SubprocessOutput />);

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,7 @@ You can even inspect and change the props of components, and see the results imm
17661766
- [Write to stdout](examples/use-stdout/use-stdout.js) - Write to stdout bypassing main Ink output.
17671767
- [Write to stderr](examples/use-stderr/use-stderr.js) - Write to stderr bypassing main Ink output.
17681768
- [Static](examples/static/static.js) - Use `<Static>` to render permanent output.
1769+
- [Child process](examples/subprocess-output) - Render output from a child process.
17691770

17701771
## Maintainers
17711772

0 commit comments

Comments
 (0)