-
-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jonathan Dahan
committed
Sep 7, 2019
1 parent
9487c00
commit 844a458
Showing
8 changed files
with
137 additions
and
1 deletion.
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')('./usekeypress'); |
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,42 @@ | ||
'use strict'; | ||
const {useState} = require('react'); | ||
const React = require('react'); | ||
const {render, useKeypress, Box} = require('../..'); | ||
|
||
const {exit} = process; | ||
|
||
const Robot = () => { | ||
const [x, setX] = useState(1); | ||
const [y, setY] = useState(1); | ||
|
||
useKeypress(key => { | ||
switch (key) { | ||
case 'h': | ||
setX(Math.max(1, x - 1)); | ||
break; | ||
case 'l': | ||
setX(Math.min(80, x + 1)); | ||
break; | ||
case 'j': | ||
setY(Math.min(40, y + 1)); | ||
break; | ||
case 'k': | ||
setY(Math.max(1, y - 1)); | ||
break; | ||
case 'q': | ||
exit(); | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
|
||
return ( | ||
<Box flexDirection="column"> | ||
<Box>Use h, j, k, and l to move the face. q to exit</Box> | ||
<Box marginTop={y} marginLeft={x}>^_^</Box> | ||
</Box> | ||
); | ||
}; | ||
|
||
render(<Robot/>); |
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,15 @@ | ||
import {useEffect, useContext} from 'react'; | ||
import {StdinContext} from '..'; | ||
|
||
export default keypressHandler => { | ||
const {stdin, setRawMode} = useContext(StdinContext); | ||
|
||
useEffect(() => { | ||
setRawMode(true); | ||
stdin.on('keypress', keypressHandler); | ||
return () => { | ||
stdin.off('keypress', keypressHandler); | ||
setRawMode(false); | ||
}; | ||
}, [stdin, setRawMode, keypressHandler]); | ||
}; |
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,22 @@ | ||
'use strict'; | ||
const React = require('react'); | ||
const {render, useKeypress} = require('../..'); | ||
|
||
const input = new Set('abcdefghijklmnopqrstuvwxyz'.split('')); | ||
|
||
const KeypressTest = () => { | ||
useKeypress(str => { | ||
input.delete(str); | ||
if (input.size === 0) { | ||
app.unmount(); | ||
} | ||
}); | ||
return null; | ||
}; | ||
|
||
const app = render(<KeypressTest/>); | ||
|
||
(async () => { | ||
await app.waitUntilExit(); | ||
console.log('exited'); | ||
})(); |