forked from vadimdemedes/ink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
use-input.ts
197 lines (165 loc) · 3.88 KB
/
use-input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {useEffect} from 'react';
import useStdin from './use-stdin';
/**
* Handy information about a key that was pressed.
*/
export interface Key {
/**
* Up arrow key was pressed.
*/
upArrow: boolean;
/**
* Down arrow key was pressed.
*/
downArrow: boolean;
/**
* Left arrow key was pressed.
*/
leftArrow: boolean;
/**
* Right arrow key was pressed.
*/
rightArrow: boolean;
/**
* Page Down key was pressed.
*/
pageDown: boolean;
/**
* Page Up key was pressed.
*/
pageUp: boolean;
/**
* Return (Enter) key was pressed.
*/
return: boolean;
/**
* Escape key was pressed.
*/
escape: boolean;
/**
* Ctrl key was pressed.
*/
ctrl: boolean;
/**
* Shift key was pressed.
*/
shift: boolean;
/**
* Tab key was pressed.
*/
tab: boolean;
/**
* Backspace key was pressed.
*/
backspace: boolean;
/**
* Delete key was pressed.
*/
delete: boolean;
/**
* [Meta key](https://en.wikipedia.org/wiki/Meta_key) was pressed.
*/
meta: boolean;
}
type Handler = (input: string, key: Key) => void;
interface Options {
/**
* Enable or disable capturing of user input.
* Useful when there are multiple useInput hooks used at once to avoid handling the same input several times.
*
* @default true
*/
isActive?: boolean;
}
/**
* This hook is used for handling user input.
* It's a more convenient alternative to using `StdinContext` and listening to `data` events.
* The callback you pass to `useInput` is called for each character when user enters any input.
* However, if user pastes text and it's more than one character, the callback will be called only once and the whole string will be passed as `input`.
*
* ```
* import {useInput} from 'ink';
*
* const UserInput = () => {
* useInput((input, key) => {
* if (input === 'q') {
* // Exit program
* }
*
* if (key.leftArrow) {
* // Left arrow key pressed
* }
* });
*
* return …
* };
* ```
*/
const useInput = (inputHandler: Handler, options: Options = {}) => {
const {stdin, setRawMode, internal_exitOnCtrlC} = useStdin();
useEffect(() => {
if (options.isActive === false) {
return;
}
setRawMode(true);
return () => {
setRawMode(false);
};
}, [options.isActive, setRawMode]);
useEffect(() => {
if (options.isActive === false) {
return;
}
const handleData = (data: Buffer) => {
let input = String(data);
const key = {
upArrow: input === '\u001B[A',
downArrow: input === '\u001B[B',
leftArrow: input === '\u001B[D',
rightArrow: input === '\u001B[C',
pageDown: input === '\u001B[6~',
pageUp: input === '\u001B[5~',
return: input === '\r',
escape: input === '\u001B',
ctrl: false,
shift: false,
tab: input === '\t' || input === '\u001B[Z',
backspace: input === '\u0008',
delete: input === '\u007F' || input === '\u001B[3~',
meta: false
};
// Copied from `keypress` module
if (input <= '\u001A' && !key.return) {
input = String.fromCharCode(
input.charCodeAt(0) + 'a'.charCodeAt(0) - 1
);
key.ctrl = true;
}
if (input.startsWith('\u001B')) {
input = input.slice(1);
key.meta = true;
}
const isLatinUppercase = input >= 'A' && input <= 'Z';
const isCyrillicUppercase = input >= 'А' && input <= 'Я';
if (input.length === 1 && (isLatinUppercase || isCyrillicUppercase)) {
key.shift = true;
}
// Shift+Tab
if (key.tab && input === '[Z') {
key.shift = true;
}
if (key.tab || key.backspace || key.delete) {
input = '';
}
// 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);
}
};
stdin?.on('data', handleData);
return () => {
stdin?.off('data', handleData);
};
}, [options.isActive, stdin, internal_exitOnCtrlC, inputHandler]);
};
export default useInput;