Skip to content

Commit

Permalink
Let useInput handle Ctrl+C if exitOnCtrlC is disabled (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes authored Aug 2, 2020
1 parent 8e8c1ba commit 7de558e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ export default class App extends PureComponent<Props, State> {
value={{
stdin: this.props.stdin,
setRawMode: this.handleSetRawMode,
isRawModeSupported: this.isRawModeSupported()
isRawModeSupported: this.isRawModeSupported(),
internal_exitOnCtrlC: this.props.exitOnCtrlC
}}
>
<StdoutContext.Provider
Expand Down
5 changes: 4 additions & 1 deletion src/components/StdinContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface Props {
* A boolean flag determining if the current `stdin` supports `setRawMode`. A component using `setRawMode` might want to use `isRawModeSupported` to nicely fall back in environments where raw mode is not supported.
*/
readonly isRawModeSupported: boolean;

readonly internal_exitOnCtrlC: boolean;
}

/**
Expand All @@ -24,7 +26,8 @@ export interface Props {
const StdinContext = createContext<Props>({
stdin: undefined,
setRawMode: () => {},
isRawModeSupported: false
isRawModeSupported: false,
internal_exitOnCtrlC: true
});

StdinContext.displayName = 'InternalStdinContext';
Expand Down
7 changes: 4 additions & 3 deletions src/hooks/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ interface Options {
* ```
*/
const useInput = (inputHandler: Handler, options: Options = {}) => {
const {stdin, setRawMode} = useStdin();
const {stdin, setRawMode, internal_exitOnCtrlC} = useStdin();

useEffect(() => {
if (options.isActive === false) {
Expand Down Expand Up @@ -180,7 +180,8 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
input = '';
}

if (!(input === 'c' && key.ctrl)) {
// If app is not supposed to exit on Ctrl+C, then let input listener handle it
if (!(input === 'c' && key.ctrl) || !internal_exitOnCtrlC) {
inputHandler(input, key);
}
};
Expand All @@ -190,7 +191,7 @@ const useInput = (inputHandler: Handler, options: Options = {}) => {
return () => {
stdin?.off('data', handleData);
};
}, [options.isActive, stdin, inputHandler]);
}, [options.isActive, stdin, internal_exitOnCtrlC, inputHandler]);
};

export default useInput;
24 changes: 24 additions & 0 deletions test/fixtures/use-input-ctrl-c.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, {FC} from 'react';
import {render, useInput, useApp} from '../..';

const UserInput: FC = () => {
const {exit} = useApp();

useInput((input, key) => {
if (input === 'c' && key.ctrl) {
exit();
return;
}

throw new Error('Crash');
});

return null;
};

const app = render(<UserInput />, {exitOnCtrlC: false});

(async () => {
await app.waitUntilExit();
console.log('exited');
})();
10 changes: 9 additions & 1 deletion test/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ const term = (fixture: string, args: string[] = []) => {
const result = {
write: (input: string) => {
// Give TS and Ink time to start up and render UI
// TODO: Send a signal from the Ink process when it's ready to accept input instead
setTimeout(() => {
ps.write(input);
}, 1000);
}, 3000);
},
output: '',
waitForExit: () => exitPromise
Expand Down Expand Up @@ -163,6 +164,13 @@ test('useInput - ignore input if not active', async t => {
t.true(ps.output.includes('exited'));
});

test('useInput - handle Ctrl+C when `exitOnCtrlC` is `false`', async t => {
const ps = term('use-input-ctrl-c');
ps.write('\u0003');
await ps.waitForExit();
t.true(ps.output.includes('exited'));
});

test('useStdout - write to stdout', async t => {
const ps = term('use-stdout');
await ps.waitForExit();
Expand Down

0 comments on commit 7de558e

Please sign in to comment.