-
Notifications
You must be signed in to change notification settings - Fork 0
/
mfp.js
executable file
·400 lines (359 loc) · 8.34 KB
/
mfp.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env node
const blessed = require("blessed");
const RSSParser = require("rss-parser");
const parser = new RSSParser();
const music = {};
let musicState = false;
const mpd = require("mpd");
const cmd = mpd.cmd;
const client = mpd.connect({
port: process.env.MFP_PORT || 6600,
host: process.env.MFP_HOST || "localhost",
});
const formatSeconds = (totalSeconds) => {
totalSeconds = Math.round(totalSeconds);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
};
const errorHandling = (message) => {
screen.append(errorBox);
errorBox.show();
errorBox.setContent(message);
screen.render();
setTimeout(() => {
errorBox.hide();
screen.render();
}, 2_000);
};
const play = (streamUrl) => {
client.sendCommand(cmd("clear", []), (err) => {
if (err) return errorHandling(err);
client.sendCommand(cmd("add", [streamUrl]), (err) => {
if (err) return errorHandling(err);
client.sendCommand(cmd("play", []), (err) => {
if (err) return errorHandling(err);
});
});
});
};
const pausePlay = () => {
if (musicState) {
client.sendCommand(cmd("pause", [1]), (err, _msg) => {
if (err) return errorHandling(err);
});
} else {
client.sendCommand(cmd("play", []), (err, _msg) => {
if (err) return errorHandling(err);
});
}
musicState = !musicState;
};
const jump = (seconds) => {
client.sendCommand(cmd("status", []), (err, msg) => {
if (err) return errorHandling(err);
const status = mpd.parseKeyValueMessage(msg);
if (status.state === "play") {
const currentTime = parseInt(status.elapsed.split(".")[0], 10);
const newPosition = currentTime + seconds;
client.sendCommand(cmd("seekcur", [`${newPosition}`]), (err) => {
if (err) return errorHandling(err);
});
}
});
};
const searchInList = (query) => {
const items = feedList.items.map((item) => item.getContent());
const index = items.findIndex((item) =>
item.toLowerCase().includes(query.toLowerCase()),
);
feedList.select(index !== -1 ? index : 0);
screen.render();
};
const screen = blessed.screen({
smartCSR: true,
title: "Music For Programming",
});
const feedList = blessed.list({
parent: screen,
top: 0,
left: 0,
width: "100%",
height: "71%",
keys: true,
label: " Press ? for help ",
border: { type: "line" },
padding: { left: 1 },
noCellBorders: true,
invertSelected: false,
scrollbar: {
ch: " ",
style: { bg: "blue" },
track: {
style: { bg: "grey" },
},
},
style: {
item: { hover: { bg: "blue" } },
selected: { fg: "black", bg: "blue", bold: true },
label: {
fg: "lightgrey",
},
},
});
const description = blessed.box({
parent: feedList,
top: "71%",
left: "0%",
width: "100%",
height: "18%",
tags: true,
label: " Description ",
border: {
type: "line",
},
padding: { top: 1, left: 1 },
style: {
fg: "white",
border: {
fg: "white",
},
label: {
fg: "lightgrey",
},
},
});
const playerLeft = blessed.box({
parent: feedList,
top: "89%",
left: "0",
width: "67%",
height: "11%",
valign: "middle",
tags: true,
label: " Player ",
border: {
type: "line",
},
padding: { left: 1 },
style: {
fg: "white",
border: {
fg: "white",
},
label: {
fg: "lightgrey",
},
},
});
const playerRight = blessed.box({
parent: feedList,
top: "89%",
left: "67%",
width: "35%-2",
height: "11%",
align: "center",
valign: "middle",
tags: true,
label: " Statistic ",
border: {
type: "line",
},
style: {
fg: "white",
border: {
fg: "white",
},
label: {
fg: "lightgrey",
},
},
});
const helpBox = blessed.box({
parent: screen,
top: "center",
left: "center",
width: "50%",
height: "37%",
label: " Help ",
content: `q|Esc Detach mfp from the MPD server
ENTER Start playing at this file
SPACE Pause/Play
/ Search
j Move down in the list
k Move up in the list
gg Jump to the first item in the list
G Jump to the last item in the list
h Jump back 10 seconds
l Jump forward 10 seconds
? Help
q Quit
`,
border: { type: "line" },
style: {
border: { fg: "white" },
fg: "white",
label: {
fg: "lightgrey",
},
},
hidden: true,
});
const errorBox = blessed.box({
parent: screen,
top: "center",
left: "center",
width: "70%",
height: "37%",
label: " Error ",
content: "",
border: { type: "line" },
style: {
border: { fg: "red" },
fg: "white",
label: {
fg: "lightgrey",
},
},
hidden: true,
});
const prompt = blessed.prompt({
top: "center",
left: "center",
width: "50%",
height: "20%",
border: {
type: "line",
},
style: {
border: { fg: "white" },
fg: "white",
label: {
fg: "lightgrey",
},
},
label: " Search ",
tags: true,
keys: true,
hidden: true,
});
screen.append(feedList);
screen.append(playerLeft);
screen.append(playerRight);
screen.append(description);
screen.append(prompt);
const loadAndDisplayFeed = async (url) => {
try {
const feed = await parser.parseURL(url);
const items = feed.items.map((item, index) => {
music[item.title] = {
id: index,
content: item.content,
mp3: item.comments,
pubDate: item.pubDate,
};
return item.title;
});
feedList.setItems(items);
screen.render();
} catch (error) {
errorHandling(error);
}
};
const feed = process.env.MFP_FEED || "https://musicforprogramming.net/rss.xml";
loadAndDisplayFeed(feed);
const updateUi = () => {
client.sendCommand(cmd("status", []), (err, msg) => {
if (err) return errorHandling(err);
const status = mpd.parseKeyValueMessage(msg);
const elapsed = formatSeconds(status.elapsed);
const duration = formatSeconds(status.duration);
const state =
status.state === "play"
? "{green-fg}[play]{/green-fg}"
: "{blue-fg}[pause]{/blue-fg}";
const bitrate = status.bitrate + " kbps";
client.sendCommand(cmd("currentsong", []), (err, msg) => {
if (err) return errorHandling(err);
const songInfo = mpd.parseKeyValueMessage(msg)?.Title;
const artist = mpd.parseKeyValueMessage(msg)?.Artist;
const selectedItem = feedList.getItem(feedList.selected)?.getContent();
const content = `{bold}${state} ${songInfo} [${artist}]{/bold}`;
const instruments = `{bold}{blue-fg}[${elapsed} Time] [${duration} Length] [${bitrate}]{/blue-fg}{/bold}`;
playerLeft.setContent(content);
playerRight.setContent(instruments);
description.setContent(
`{bold}{green-fg}${selectedItem}{/green-fg}{/bold}` +
"\n" +
music[selectedItem]?.content,
);
screen.render();
});
});
};
setInterval(updateUi, 900);
screen.key(["space"], (_ch, _key) => {
pausePlay();
});
screen.key("enter", () => {
const selectedItem = feedList.getItem(feedList.selected).getContent();
const mp3 = music[selectedItem].mp3;
play(mp3);
});
screen.key("?", function () {
if (helpBox.hidden) {
screen.append(helpBox);
helpBox.show();
screen.render();
screen.once("keypress", () => {
helpBox.hide();
screen.render();
});
}
});
screen.key(["escape", "q"], (_ch, _key) => {
return process.exit(0);
});
let firstGPressed = false;
screen.key(["g"], (_ch, _key) => {
if (!firstGPressed) {
firstGPressed = true;
setTimeout(() => {
if (firstGPressed) {
feedList.select(0);
firstGPressed = false;
screen.render();
}
}, 300);
} else {
feedList.select(0);
firstGPressed = false;
screen.render();
}
});
screen.program.on("keypress", function (_ch, key) {
if (key.name === "g" && key.shift) {
feedList.select(feedList.items.length - 1);
screen.render();
}
});
screen.key("/", () => {
prompt.input("", (_err, value) => {
searchInList(value || "...");
});
});
screen.key(["j"], (_ch, _key) => {
feedList.down();
screen.render();
});
screen.key(["k"], (_ch, _key) => {
feedList.up();
screen.render();
});
screen.key(["h"], (_ch, _key) => {
jump(-10);
});
screen.key(["l"], (_ch, _key) => {
jump(10);
});
screen.render();