add some render modes

This commit is contained in:
jill 2021-09-03 06:33:54 +03:00
parent 509f4c17dd
commit b687772d31

View file

@ -1,5 +1,26 @@
const readline = require('readline'); const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
const modes = ['classic', 'bottom', 'top'];
let mode = 0;
let modePopup = 0;
const modePopupDuration = 2;
process.stdin.on('keypress', (str, key) => {
if (key.name === 'space') {
mode++;
mode = mode % modes.length;
modePopup = modePopupDuration;
}
if (key.sequence === '\x03' || key.sequence === '\x04') process.exit(0); // ctrl+c, ctrl+d
});
const plas = [' ', '.', '*', '/', '0']; const plas = [' ', '.', '*', '/', '0'];
const progressChars = ['▏', '▎', '▍', '▌', '▋', '▊', '▉']; const progressChars = ['▏', '▎', '▍', '▌', '▋', '▊', '▉'];
//const progressChars = ['0', '1', '2', '3', '4', '5', '6', '7']; //const progressChars = ['0', '1', '2', '3', '4', '5', '6', '7'];
@ -60,6 +81,7 @@ function render(artist, album, title, songStart, songEnd, pauseSpot, paused) {
let dt = refreshrate / 1000 let dt = refreshrate / 1000
time += dt; time += dt;
errorTimer -= dt; errorTimer -= dt;
modePopup -= dt;
let w = process.stdout.columns; let w = process.stdout.columns;
let h = process.stdout.rows; let h = process.stdout.rows;
@ -74,20 +96,52 @@ function render(artist, album, title, songStart, songEnd, pauseSpot, paused) {
} }
// leaving the coordinates as non-integer values is very undefined behavior // leaving the coordinates as non-integer values is very undefined behavior
let texts = [ let texts = [];
{
value: ' ' + displaytitle + ' ', switch (mode) {
x: Math.round(w / 2 - displaytitle.length / 2 + Math.sin(time) * 5), case 0:
y: Math.floor(h / 2) - 1, texts.push({
color: '\033[7;1m' value: ' ' + displaytitle + ' ',
}, x: Math.round(w / 2 - displaytitle.length / 2 + Math.sin(time) * 5),
{ y: Math.floor(h / 2) - 1,
value: ' ' + displayalbum + ' ', color: '\033[7;1m'
x: Math.round(w / 2 - displayalbum.length / 2 - Math.sin(time) * 5), });
y: Math.floor(h / 2) + 1, texts.push({
color: '\033[7m' value: ' ' + displayalbum + ' ',
} x: Math.round(w / 2 - displayalbum.length / 2 - Math.sin(time) * 5),
]; y: Math.floor(h / 2) + 1,
color: '\033[7m'
});
break;
case 1:
texts.push({
value: ' ' + displaytitle + ' '.repeat(w),
x: 0,
y: h - 3,
color: '\033[7;1m'
});
texts.push({
value: ' ' + displayalbum + ' '.repeat(w),
x: 0,
y: h - 2,
color: '\033[7m'
});
break;
case 2:
texts.push({
value: ' ' + displaytitle + ' '.repeat(w),
x: 0,
y: 0,
color: '\033[7;1m'
});
texts.push({
value: ' ' + displayalbum + ' '.repeat(w),
x: 0,
y: 1,
color: '\033[7m'
});
break;
}
if (errorText && errorTimer > 0) { if (errorText && errorTimer > 0) {
let symbol = '⚠️'; let symbol = '⚠️';
@ -105,6 +159,15 @@ function render(artist, album, title, songStart, songEnd, pauseSpot, paused) {
}); });
} }
if (modePopup > 0) {
texts.push({
value: modes[mode],
x: Math.floor(w - ((modes[mode].length + 2) * outCirc(Math.min(modePopup, 1)))),
y: 0,
color: '\033[40;37m'
});
}
let reset = '\033[0m'; let reset = '\033[0m';
let now = paused ? pauseSpot : Date.now(); let now = paused ? pauseSpot : Date.now();
@ -154,7 +217,7 @@ function render(artist, album, title, songStart, songEnd, pauseSpot, paused) {
let pChar = progressChars[Math.floor(blockProgress * progressChars.length)]; let pChar = progressChars[Math.floor(blockProgress * progressChars.length)];
let color = (x / (w - 1) > (Math.floor(progress * w) / w)) ? '\033[40;37m' : '\033[47;30m'; let color = (x / (w - 1) > (Math.floor(progress * w) / w)) ? '\033[40;37m' : '\033[47;30m';
let pause = (paused && x === (w - 2)) ? '⏸' : null; let pause = (paused && x === (w - 2)) ? '⏸' : null;
sum += color + (pause || (timerString[x - 1] === ' ' ? null : timerString[x - 1]) || (inProgressArea ? pChar : ' ')) + '\033[0m'; sum += color + (pause || (timerString[x - 1] === ' ' ? null : timerString[x - 1]) || (inProgressArea ? pChar : ' ')) + '\033[0m';