-
Notifications
You must be signed in to change notification settings - Fork 1
/
spawnPianobar.js
150 lines (135 loc) · 4.24 KB
/
spawnPianobar.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
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
const execa = require('execa'),
path = require('path'),
homedir = require('homedir')(),
mappings = {
nextSong: 'n',
dislikeSong: '-',
likeSong: '+',
quit: 'q',
selectStation: 's ',
playPauseSong: 'p',
play: 'P',
pause: 'p',
playOrPauseSong: 'p',
shuffle: 'x',
createStation: 'c',
createGenreStation: 'g',
deleteStation: 'd',
renameStation: 'r',
history: 'h'
}
let once = true
class Spawner {
constructor(start, options) {
this.stdin = options.stdin || process.stdin
this.options = Object.assign({
onExitCloseChild: true,
takeInput: true,
onExit: function (exitCode, signal) {
},
onEnd: function () {
},
onData: function (data) {
}
}, options)
if (start === true && once) {
this.pianobar = execa('./pianobar', { cwd: path.resolve(__dirname, 'pianobar') })
this.setUpPianobar()
once = false
} else {
this.pianobar = start
}
}
setUpPianobar() {
if (this.options.takeInput) {
// without this, we would only get streams once enter is pressed
if (this.stdin.setRawMode) {
this.stdin.setRawMode(true);
}
// resume this.stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
this.stdin.resume();
// i don't want binary, do you?
this.stdin.setEncoding('utf8');
let lineByLine = false;
// on any data into this.stdin
this.stdin.on('data', key => {
// ctrl-c ( end of text )
if (key === '\u0003') {
this.options.onExit()
if (this.options.onExitCloseChild) {
this.pianobar.kill()
}
process.exit();
}
if (lineByLine || lineByLine === '') {
if (key === '\u000D') {
//ENTER/RETURN
this.pianobar.stdin.write(lineByLine + '/n')
lineByLine = false
return
} else if (key === '\u0008') {
//BACK_SPACE
lineByLine = lineByLine.slice(0, -1)
} else {
//ANY OTHER KEY
lineByLine += key
}
process.stdout.write(key)
return
} else {
if (key === 's') {
lineByLine = ''
this.pianobar.stdin.write(key)
return
}
}
//send single char and flush this.stdin
this.pianobar.stdin.write(key + "\n")
// write the key to stdout all normal like
process.stdout.write(key)
})
}
this.pianobar.stdout.setEncoding('utf8')
this.pianobar.stdout.on('data', this.options.onData)
this.pianobar.stdout.on('end', this.options.onEnd)
this.pianobar.on('exit', this.options.onExit)
if (this.options.onExitCloseChild) {
process.on('exit', () => {
this.pianobar.kill()
})
process.on('SIGINT', () => {
this.pianobar.kill()
});
}
}
getPianobar() {
return this.pianobar
}
writeCommand(key) {
if (key in mappings) {
this.pianobar.stdin.write(mappings[key] + "\n")
return true
}
this.pianobar.stdin.write(key + "\n")
return false
}
respawn() {
once = true
//other respawn code
}
}
module.exports = Spawner
if (!module.parent) {
var proccess = new Spawner(true, {
onExit: function (exitCode, signal) {
process.exit()
},
onEnd: function () {
process.exit()
},
onData: function (data) {
console.log(data)
}
})
}