-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path4-keyboard.js
More file actions
67 lines (50 loc) · 1.19 KB
/
4-keyboard.js
File metadata and controls
67 lines (50 loc) · 1.19 KB
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
'use strict';
const { Observable } = require('rxjs');
const operators = require('rxjs/operators');
const { map, filter, take, reduce, debounceTime } = operators;
process.stdin.setRawMode(true);
// Keyboard stream
const keyboard = new Observable((subscriber) => {
process.stdin.on('data', (data) => {
subscriber.next(data);
});
});
keyboard.subscribe((data) => {
console.log('---');
console.dir({ keyboard: data });
});
// Cursors
const arrows = {
65: '🡅',
66: '🡇',
67: '🡆',
68: '🡄',
};
const cursors = keyboard.pipe(
filter((buf) => (buf[0] === 27) && (buf[1] === 91)),
map((buf) => buf[2]),
map((key) => arrows[key]),
//throttleTime(1000),
debounceTime(2000),
);
cursors.subscribe((cursor) => {
console.dir({ cursor });
});
// Keypress
const keypress = keyboard.pipe(map((buf) => buf[0]));
keypress.subscribe((key) => {
console.dir({ keypress: key });
});
// Take first 5 chars
const take5 = keypress.pipe(
take(5),
map((key) => String.fromCharCode(key)),
reduce((acc, char) => acc + char)
);
take5.subscribe((s) => {
console.dir({ take5: s });
});
// Exit / Ctrl+C
keypress
.pipe(filter((x) => x === 3))
.subscribe(process.exit.bind(null, 0));