working and better lolz

This commit is contained in:
gallant 2023-05-08 08:18:56 -05:00
parent 82c7d556de
commit 3dde6b1a96
3 changed files with 101 additions and 68 deletions

28
Cargo.lock generated
View file

@ -4,9 +4,9 @@ version = 3
[[package]]
name = "anstream"
version = "0.3.1"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6342bd4f5a1205d7f41e94a41a901f5647c938cdfa96036338e8533c9d6c2450"
checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163"
dependencies = [
"anstyle",
"anstyle-parse",
@ -65,9 +65,9 @@ checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
[[package]]
name = "clap"
version = "4.2.5"
version = "4.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a1f23fa97e1d1641371b51f35535cb26959b8e27ab50d167a8b996b5bada819"
checksum = "34d21f9bf1b425d2968943631ec91202fe5e837264063503708b83013f8fc938"
dependencies = [
"clap_builder",
"clap_derive",
@ -76,9 +76,9 @@ dependencies = [
[[package]]
name = "clap_builder"
version = "4.2.5"
version = "4.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fdc5d93c358224b4d6867ef1356d740de2303e9892edc06c5340daeccd96bab"
checksum = "914c8c79fb560f238ef6429439a30023c862f7a28e688c58f7203f12b29970bd"
dependencies = [
"anstream",
"anstyle",
@ -279,9 +279,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.142"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "libdbus-sys"
@ -294,9 +294,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.3.6"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c"
checksum = "ece97ea872ece730aed82664c424eb4c8291e1ff2480247ccf7409044bc6479f"
[[package]]
name = "mli"
@ -327,9 +327,9 @@ checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "pkg-config"
version = "0.3.26"
version = "0.3.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
[[package]]
name = "proc-macro2"
@ -357,9 +357,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.37.18"
version = "0.37.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bbfc1d1c7c40c01715f47d71444744a81669ca84e8b63e25a55e169b1f86433"
checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d"
dependencies = [
"bitflags",
"errno",

View file

@ -3,8 +3,7 @@ use clap::Command;
mod scriptwrap;
use scriptwrap::Player;
fn main() -> Result<(), Box<dyn std::error::Error>>{
fn main() -> Result<(), Box<dyn std::error::Error>> {
let m = Command::new("mli")
.author("Gallant")
.version("0.1.0")
@ -14,20 +13,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>>{
.subcommand(Command::new("next"))
.subcommand(Command::new("previous"))
.get_matches();
let player = Player::init();
//programs need time to run?? crazy.. .
match m.subcommand() {
Some(("pause", _)) => print!("{}",player::pause()?),
Some(("play", _)) => print!("{}",player::play()?),
Some(("next", _)) => print!("{}",player::next()?),
Some(("previous", _)) => print!("{}",player::previous()?),
Some(("pause", _)) => print!("{}", player.pause()?),
Some(("play", _)) => print!("{}", player.play()?),
Some(("next", _)) => print!("{}", player.next()?),
Some(("previous", _)) => print!("{}", player.previous()?),
_ => {
print!("{}",player::get_track("Music")?)
print!("{}", player.get_track()?);
}
}

View file

@ -1,102 +1,138 @@
use std::process::Command;
pub struct Player{
players: Vec<String>
};
pub struct Player {
players: Vec<String>,
}
impl Player{
pub fn init() -> Self
{
let a = Command::new("osascript")
impl Player {
pub fn init() -> Self {
let spot = Command::new("osascript")
.arg("-e")
.arg(r#"if application "Spotify" is running
log "Spotify"
.arg(
r#"if application "Spotify" is running then
return "Spotify"
end if
if application "Music" is running
log "Music"
end if
"#)
"#,
)
.output()
.unwrap()
.stdout;
let mus = Command::new("osascript")
.arg("-e")
.arg(
r#"if application "Music" is running then
return "Music"
end if"#,
)
.output()
.unwrap()
.stdout;
let mut vecc: Vec<String> = Vec::new();
if spot.len() > 0 {
vecc.push(std::str::from_utf8(&spot).unwrap().trim().to_string());
}
if mus.len() > 0 {
vecc.push(std::str::from_utf8(&mus).unwrap().trim().to_string());
}
if vecc.len() == 0 {
//fuck it
vecc.push("Music".to_owned());
}
Player { players: vecc }
}
pub fn play(&self) -> Result<String,Box<dyn std::error::Error>>
{
pub fn play(&self) -> Result<String, Box<dyn std::error::Error>> {
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
.arg(format!(
r#"tell application "{}"
if it is running then
play
else
return "not running"
end if
end tell"#))
end tell"#,
self.players.get(0).unwrap()
))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
print!("{}", std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn pause(&self) -> Result<String,Box<dyn std::error::Error>>
{
pub fn pause(&self) -> Result<String, Box<dyn std::error::Error>> {
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
.arg(format!(
r#"tell application "{}"
if it is running then
pause
else
return "not running"
end if
end tell"#))
end tell"#,
self.players.get(0).unwrap()
))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
print!("{}", std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn next(&self) -> Result<String,Box<dyn std::error::Error>>
{
pub fn next(&self) -> Result<String, Box<dyn std::error::Error>> {
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
.arg(format!(
r#"tell application "{}"
if it is running then
next track
else
return "not running"
end if
end tell"#))
end tell"#,
self.players.get(0).unwrap()
))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
print!("{}", std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn previous(&self) -> Result<String,Box<dyn std::error::Error>>
{
pub fn previous(&self) -> Result<String, Box<dyn std::error::Error>> {
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
.arg(format!(
r#"tell application "{}"
if it is running then
back track
else
return "not running"
end if
end tell"#))
end tell"#,
self.players.get(0).unwrap()
))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
print!("{}", std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
pub fn get_track(&self) -> Result<String,Box<dyn std::error::Error>>
{
pub fn get_track(&self) -> Result<String, Box<dyn std::error::Error>> {
let a = Command::new("osascript")
.arg("-e")
.arg(format!(r#"tell application "{player_name}"
.arg(format!(
r#"tell application "{}"
if it is running then
get name of current track
else
return "not running"
end if
end tell"#))
end tell"#,
match self.players.get(0) {
Some(a) => a,
None => "Music",
}
))
.output()?;
print!("{}",std::str::from_utf8(&a.stderr)?);
print!("{}", std::str::from_utf8(&a.stderr)?);
Ok(std::str::from_utf8(&a.stdout)?.to_string())
Ok(std::str::from_utf8(&a.stdout)?.to_string())
}
}