-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.js
106 lines (91 loc) · 2.42 KB
/
event.js
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
var readline = require('readline');
var MuteStream = require('mute-stream');
var { fromEvent } = require('rxjs');
var { filter, map, tap } = require('rxjs/operators');
var { pipe, path, when, equals, either } = require('ramda');
class Event {
constructor(player) {
this.player = player;
this.rl = this.createInterface();
this.rl.output.mute(); //停止写入
}
createInterface() {
var ms = new MuteStream();
ms.pipe(process.stdout);
return readline.createInterface({
terminal: true,
input: process.stdin,
output: ms,
})
}
listenKeyPress() {
var { input, /**output**/ } = this.rl;
fromEvent(input, 'keypress', (value, key) => ({ value: value, key: key || {} }))
.pipe(filter(({ key }) => key.name !== 'enter' && key.name !== 'return'))
.pipe(
tap(this.hKey()),
tap(this.lKey()),
tap(this.stopKey()),
tap(this.pauseKey()),
tap(this.increseKey()),
tap(this.decreaseKey()),
)
.forEach(x => { })
}
hKey() {
return pipe(
path(['key', 'name']),
when(
equals('h'),
// to do
() => { this.player.previous() }
)
)
}
lKey() {
return pipe(
path(['key', 'name']),
when(
equals('l'),
() => { this.player.next() },
)
)
}
stopKey() {
return pipe(
path(['key', 'name']),
when(
equals('x'),
() => { this.player.pause() },
)
)
}
pauseKey() {
return pipe(
path(['key', 'name']),
when(
equals('X'),
() => { this.player.stop() },
)
)
}
increseKey() {
return pipe(
path(['key', 'sequence']),
when(
either(equals(','), equals('[')),
() => { this.player.decreaseVolume() },
)
)
}
decreaseKey() {
return pipe(
path(['key', 'sequence']),
when(
either(equals('.'), equals(']')),
() => { this.player.increaseVolume() },
)
)
}
}
module.exports = Event;