1
Fork 0

Add a print option.

This commit is contained in:
Bauke 2023-04-22 12:12:34 +02:00
parent 4b703a0549
commit 9c900c919e
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
1 changed files with 33 additions and 7 deletions

View File

@ -11,6 +11,7 @@ async function main(): Promise<void> {
.option("--next-song", "Play the next song.")
.option("--play-pause", "Toggle play or pause.")
.option("--previous-song", "Play the previous song.")
.option("--print <print:string>", "Print data from the current song.")
.option(
"--volume <volume:number>",
"Change the volume by a relative amount",
@ -36,19 +37,44 @@ async function main(): Promise<void> {
if (options.volume !== undefined) {
await fetch(`${remoteApi}/setvolumerel/${options.volume}`);
}
if (options.print !== undefined) {
const status = await getStatus();
const progressPercentage = ((status.progress / status.track.duration) * 100)
.toFixed(2);
const markersAndReplacements = [
["album", status.album],
["artist", status.artist],
["title", status.title],
["progress", progressPercentage],
];
let formattedString = options.print;
for (const [marker, replacement] of markersAndReplacements) {
const regex = new RegExp(`<${marker}>`, "g");
formattedString = formattedString.replace(regex, replacement);
}
console.log(formattedString);
}
}
type Status = {
artist?: string;
title?: string;
album: string;
artist: string;
progress: number;
title: string;
track: {
duration: number;
};
};
async function notifyCurrentSong(): Promise<void> {
const status: Status = await (await fetch(`${remoteApi}/status`)).json();
if (status.artist === undefined || status.title === undefined) {
return;
}
async function getStatus(): Promise<Status> {
return await (await fetch(`${remoteApi}/status`)).json();
}
async function notifyCurrentSong(): Promise<void> {
const status = await getStatus();
await Deno.run({
cmd: ["notify-send", status.title, status.artist],
}).status();