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

124 lines
3.1 KiB
Rust

use std::{process::Command, str, fs, fs::File};
pub struct SongMetadata
{
pub name: String,
pub artist: String,
pub album: String,
pub art_url: String,
pub is_playing: bool,
}
impl SongMetadata
{
pub fn new() -> SongMetadata
{
SongMetadata {
name: "NONE".to_string(),
artist: "NONE".to_string(),
album: "NONE".to_string(),
art_url: "NONE".to_string(),
is_playing: true,
}
}
}
const COM_START: &str = "playerctl --player=strawberry";
pub fn get_song_metadata() -> SongMetadata
{
let command = format!("{} {}", COM_START, "metadata --format {{title}},{{artist}},{{album}},{{mpris:artUrl}}");
let result = run_command(&command);
let command = format!("{} {}", COM_START, "status");
let status = run_command(&command).to_ascii_lowercase();
let status = status.trim();
let mut sm = SongMetadata::new();
let split: Vec<&str> = result.split(',').collect();
if split.len() < 4
{
return sm;
}
sm.name = split[0].to_string();
sm.artist = split[1].to_string();
sm.album = split[2].to_string();
sm.art_url = split[3].to_string();
sm.art_url = sm.art_url.trim().to_string();
sm.is_playing = if status == "paused" || status == "stopped"
{ false } else { true };
// let test_url = sm.art_url.trim_start_matches("file://").to_string();
// let test_url = test_url.trim();
// let paths = fs::read_dir("/tmp").unwrap();
// //println!("List of files in /tmp:");
// for path in paths
// {
// let name = path.unwrap().path();
// if name.to_str().unwrap().to_string().contains("strawberry-cover")
// {
// let temp = name.to_str().unwrap().to_string();
// let temp = temp.trim();
// println!("Found possible art file\n\tFound: {} \n\tCompare: {}", name.display(), test_url);
// if temp == test_url
// {
// println!("Attemping to open file: {}", name.display());
// let f = match File::open(name.clone())
// {
// Ok(f) => f,
// Err(e) => panic!("Failed open album art file: {}", e)
// };
// println!("File found!\t\n\n");
// break;
// }
// else
// {
// println!("\tNO MATCH\n\n");
// }
// }
// }
return sm;
}
pub fn next_track()
{
let command = format!("{} {}", COM_START, "next");
let _ = run_command(&command);
}
pub fn play()
{
let command = format!("{} {}", COM_START, "play");
let _ = run_command(&command);
}
pub fn pause()
{
let command = format!("{} {}", COM_START, "pause");
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()
}