-
-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
useInput
hook to handle user input
- Loading branch information
Vadim Demedes
committed
Sep 15, 2019
1 parent
92e2995
commit dd3efe8
Showing
14 changed files
with
203 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
require('import-jsx')('./useinput'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
const {useState, useContext} = require('react'); | ||
const React = require('react'); | ||
const {render, useInput, Box, AppContext} = require('../..'); | ||
|
||
const Robot = () => { | ||
const {exit} = useContext(AppContext); | ||
const [x, setX] = useState(1); | ||
const [y, setY] = useState(1); | ||
|
||
useInput((input, meta) => { | ||
if (input === 'q') { | ||
exit(); | ||
} | ||
|
||
if (meta.left) { | ||
setX(Math.max(1, x - 1)); | ||
} | ||
|
||
if (meta.right) { | ||
setX(Math.min(20, x + 1)); | ||
} | ||
|
||
if (meta.up) { | ||
setY(Math.max(1, y - 1)); | ||
} | ||
|
||
if (meta.down) { | ||
setY(Math.min(10, y + 1)); | ||
} | ||
}); | ||
|
||
return ( | ||
<Box flexDirection="column"> | ||
<Box>Use arrow keys to move the face. q to exit</Box> | ||
<Box height={12} paddingLeft={x} paddingTop={y}>^_^</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
render(<Robot/>); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {useLayoutEffect, useContext} from 'react'; | ||
import {StdinContext} from '..'; | ||
|
||
export default inputHandler => { | ||
const {stdin, setRawMode} = useContext(StdinContext); | ||
|
||
useLayoutEffect(() => { | ||
setRawMode(true); | ||
|
||
return () => setRawMode(false); | ||
}, [setRawMode]); | ||
|
||
useLayoutEffect(() => { | ||
const handleData = data => { | ||
const input = String(data); | ||
const meta = { | ||
up: input === '\u001B[A', | ||
down: input === '\u001B[B', | ||
left: input === '\u001B[D', | ||
right: input === '\u001B[C' | ||
}; | ||
|
||
inputHandler(input, meta); | ||
}; | ||
|
||
stdin.on('data', handleData); | ||
|
||
return () => stdin.off('data', handleData); | ||
}, [stdin, inputHandler]); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
const React = require('react'); | ||
const {render, useInput, AppContext} = require('../..'); | ||
|
||
const UserInput = () => { | ||
const {exit} = React.useContext(AppContext); | ||
|
||
useInput(input => { | ||
if (input === 'q') { | ||
exit(); | ||
} | ||
}); | ||
|
||
return null; | ||
}; | ||
|
||
const app = render(<UserInput/>); | ||
|
||
(async () => { | ||
await app.waitUntilExit(); | ||
console.log('exited'); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {serial as test} from 'ava'; | ||
import {spawn} from 'node-pty'; | ||
|
||
test.cb('exit when user types "q" character', t => { | ||
const term = spawn('node', ['./fixtures/run', './handles-user-input'], { | ||
name: 'xterm-color', | ||
cols: 100, | ||
cwd: __dirname, | ||
env: process.env | ||
}); | ||
|
||
let output = ''; | ||
|
||
term.on('data', data => { | ||
output += data; | ||
}); | ||
|
||
let isExited = false; | ||
|
||
term.on('exit', code => { | ||
isExited = true; | ||
|
||
if (code === 0) { | ||
t.true(output.includes('exited')); | ||
t.pass(); | ||
t.end(); | ||
return; | ||
} | ||
|
||
t.fail(); | ||
t.end(); | ||
}); | ||
|
||
setTimeout(() => { | ||
t.false(isExited); | ||
term.write('q'); | ||
}, 1000); | ||
|
||
setTimeout(() => { | ||
term.kill(); | ||
t.fail(); | ||
t.end(); | ||
}, 1500); | ||
}); |