Add a print option.
This commit is contained in:
parent
4b703a0549
commit
9c900c919e
|
@ -11,6 +11,7 @@ async function main(): Promise<void> {
|
||||||
.option("--next-song", "Play the next song.")
|
.option("--next-song", "Play the next song.")
|
||||||
.option("--play-pause", "Toggle play or pause.")
|
.option("--play-pause", "Toggle play or pause.")
|
||||||
.option("--previous-song", "Play the previous song.")
|
.option("--previous-song", "Play the previous song.")
|
||||||
|
.option("--print <print:string>", "Print data from the current song.")
|
||||||
.option(
|
.option(
|
||||||
"--volume <volume:number>",
|
"--volume <volume:number>",
|
||||||
"Change the volume by a relative amount",
|
"Change the volume by a relative amount",
|
||||||
|
@ -36,19 +37,44 @@ async function main(): Promise<void> {
|
||||||
if (options.volume !== undefined) {
|
if (options.volume !== undefined) {
|
||||||
await fetch(`${remoteApi}/setvolumerel/${options.volume}`);
|
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 = {
|
type Status = {
|
||||||
artist?: string;
|
album: string;
|
||||||
title?: string;
|
artist: string;
|
||||||
|
progress: number;
|
||||||
|
title: string;
|
||||||
|
track: {
|
||||||
|
duration: number;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
async function notifyCurrentSong(): Promise<void> {
|
async function getStatus(): Promise<Status> {
|
||||||
const status: Status = await (await fetch(`${remoteApi}/status`)).json();
|
return await (await fetch(`${remoteApi}/status`)).json();
|
||||||
if (status.artist === undefined || status.title === undefined) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
async function notifyCurrentSong(): Promise<void> {
|
||||||
|
const status = await getStatus();
|
||||||
await Deno.run({
|
await Deno.run({
|
||||||
cmd: ["notify-send", status.title, status.artist],
|
cmd: ["notify-send", status.title, status.artist],
|
||||||
}).status();
|
}).status();
|
||||||
|
|
Loading…
Reference in New Issue