You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jpmmv/src/player_interface.rs

60 lines
1.2 KiB
Rust

use std::{process::Command, str};
pub struct SongMetadata
{
pub name: String,
pub artist: String,
pub album: String,
}
impl SongMetadata
{
pub fn new() -> SongMetadata
{
SongMetadata {
name: "NONE".to_string(),
artist: "NONE".to_string(),
album: "NONE".to_string(),
}
}
}
const COM_START: &str = "playerctl --player=strawberry";
pub fn get_song_metadata() -> SongMetadata
{
let command = format!("{} {}", COM_START, "metadata --format {{title}},{{artist}},{{album}}");
let result = run_command(&command);
let mut sm = SongMetadata::new();
let split: Vec<&str> = result.split(',').collect();
sm.name = split[0].to_string();
sm.artist = split[1].to_string();
sm.album = split[2].to_string();
sm
}
pub fn next_track()
{
let command = format!("{} {}", COM_START, "next");
let _ = run_command(&command);
}
fn run_command(command: &str) -> String
{
let output = Command::new("sh")
.arg("-c")
.arg(command)
.output()
.expect(&format!("Failed to run the command: {}", command));
str::from_utf8(&output.stdout).expect("Failed to convert command result to utf8 string").to_string()
}