horrible code, will refactor for support of ALL platforms, first step for mac/apple music

This commit is contained in:
gallant 2023-05-05 11:41:29 -05:00
parent b279a3ec9d
commit b50fd694b5
3 changed files with 97 additions and 21 deletions

1
README.md Normal file
View file

@ -0,0 +1 @@
mac support is only for macs with support of apple script atm

View file

@ -1,7 +1,7 @@
use clap::Command;
//use ansi_term::Colour::*;
use mpris::{PlaybackStatus, PlayerFinder};
mod scriptwrap;
use scriptwrap::Player;
fn main() -> Result<(), Box<dyn std::error::Error>>{
@ -14,30 +14,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
.subcommand(Command::new("next"))
.subcommand(Command::new("previous"))
.get_matches();
let pl_find = PlayerFinder::new()
.map_err(|e| format!("[ERROR] D-Bus denied: {}", e))?;
let pl = pl_find
.find_active()
.map_err(|e| format!("[ERROR] No player: {}",e))?;
//programs need time to run?? crazy.. .
match m.subcommand() {
Some(("pause", _)) => pl.pause()?,
Some(("play", _)) => pl.play()?,
Some(("next", _)) => pl.next()?,
Some(("previous", _)) => pl.previous()?,
Some(("pause", _)) => print!("{}",Player::pause("Music")?),
Some(("play", _)) => print!("{}",Player::play("Music")?),
Some(("next", _)) => print!("{}",Player::next("Music")?),
Some(("previous", _)) => print!("{}",Player::previous("Music")?),
_ => {
let status = pl
.get_playback_status()
.map_err(|e| format!("[ERROR] Can't get status: {}",e))?;
if status != PlaybackStatus::Stopped
{
println!("currently playing: {:?}", pl
.get_metadata().unwrap()
.title().unwrap());
}
print!("{}",Player::get_track("Music")?)
}
}

86
src/scriptwrap.rs Normal file
View file

@ -0,0 +1,86 @@
use std::process::Command;
pub struct Player;
impl Player{
pub fn play(player_name: &str) -> Result<String,Box<dyn std::error::Error>>
{
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
if it is running then
play
else
return "not running"
end if
end tell"#))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn pause(player_name: &str) -> Result<String,Box<dyn std::error::Error>>
{
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
if it is running then
pause
else
return "not running"
end if
end tell"#))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn next(player_name: &str) -> Result<String,Box<dyn std::error::Error>>
{
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
if it is running then
next track
else
return "not running"
end if
end tell"#))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn previous(player_name: &str) -> Result<String,Box<dyn std::error::Error>>
{
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
if it is running then
back track
else
return "not running"
end if
end tell"#))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn get_track(player_name: &str) -> Result<String,Box<dyn std::error::Error>>
{
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
if it is running then
get name of current track
else
return "not running"
end if
end tell"#))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
}