Refactor
This commit is contained in:
parent
f0ba8bf593
commit
f1add467d0
5 changed files with 242 additions and 216 deletions
214
src/main.rs
214
src/main.rs
|
@ -1,14 +1,12 @@
|
|||
extern crate libnotify;
|
||||
extern crate mpd;
|
||||
|
||||
mod cli;
|
||||
use cli::*;
|
||||
mod noise;
|
||||
use noise::{commands::Noise, parser::*};
|
||||
|
||||
use clap::{Command, CommandFactory, Parser};
|
||||
use clap_complete::{generate, Generator};
|
||||
use libnotify::Notification;
|
||||
use mpd::{Client, Query, Song, State, Term};
|
||||
use std::{io, time::Duration};
|
||||
use mpd::Client;
|
||||
use std::io;
|
||||
|
||||
fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
|
||||
generate(
|
||||
|
@ -24,129 +22,40 @@ fn main() {
|
|||
|
||||
if let Some(completions) = cli.completions {
|
||||
let mut cmd = Cli::command();
|
||||
eprintln!("Generating completion file for {completions:?}...");
|
||||
eprintln!("Generatig completion file for {completions:?}...");
|
||||
print_completions(completions, &mut cmd);
|
||||
return;
|
||||
}
|
||||
|
||||
libnotify::init("noise").unwrap();
|
||||
|
||||
let n = libnotify::Notification::new("Noise", None, None);
|
||||
|
||||
let host = cli.host.unwrap_or("localhost:6600".into());
|
||||
|
||||
let mut conn = Client::connect(host).expect("Connection failed");
|
||||
|
||||
if let Some(cmd) = &cli.command {
|
||||
match cmd {
|
||||
Commands::Play { track } => {
|
||||
if let Some(i) = track {
|
||||
conn.switch(*i).unwrap();
|
||||
} else {
|
||||
conn.play().unwrap();
|
||||
}
|
||||
}
|
||||
Commands::Stop => {
|
||||
conn.stop().unwrap();
|
||||
}
|
||||
Commands::Toggle => {
|
||||
if conn.status().unwrap().state == State::Stop {
|
||||
conn.play().unwrap();
|
||||
} else {
|
||||
conn.toggle_pause().unwrap();
|
||||
}
|
||||
}
|
||||
Commands::Play { track } => conn.noise_play(*track),
|
||||
Commands::Stop => conn.stop().unwrap(),
|
||||
Commands::Toggle => conn.toggle(),
|
||||
Commands::Next => conn.next().unwrap(),
|
||||
Commands::Prev => conn.prev().unwrap(),
|
||||
Commands::List { file } => {
|
||||
let _current_song = conn.currentsong().unwrap();
|
||||
conn.queue().unwrap().iter().for_each(|x| {
|
||||
println!(
|
||||
"{:<02} {}",
|
||||
x.place.unwrap().pos,
|
||||
if *file {
|
||||
x.file.clone()
|
||||
} else {
|
||||
format_song(x.clone())
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
Commands::Update => {
|
||||
let thing = conn.update().unwrap();
|
||||
println!("{thing}")
|
||||
}
|
||||
Commands::List { file } => conn.list_queue(*file, cli.verbose),
|
||||
Commands::Current => (),
|
||||
Commands::Clear => conn.clear().unwrap(),
|
||||
Commands::Search { query, max } => {
|
||||
let query = query.join(" ");
|
||||
let max = max.unwrap_or(conn.stats().unwrap().songs);
|
||||
|
||||
conn.search(&Query::new().and(Term::Any, query), (0, max))
|
||||
.unwrap()
|
||||
.iter()
|
||||
.for_each(|x| println!("{}", x.file));
|
||||
}
|
||||
Commands::Find { query, max, append } => {
|
||||
let query = query.join(" ");
|
||||
let max = max.unwrap_or(conn.stats().unwrap().songs);
|
||||
|
||||
if *append {
|
||||
conn.findadd(&Query::new().and(Term::Any, query)).unwrap();
|
||||
} else {
|
||||
conn.find(&Query::new().and(Term::Any, query), (0, max))
|
||||
.unwrap()
|
||||
.iter()
|
||||
.for_each(|x| println!("{}", x.file));
|
||||
}
|
||||
}
|
||||
Commands::Crossfade { seconds } => {
|
||||
if let Some(crossfade) = seconds {
|
||||
conn.crossfade(*crossfade).unwrap();
|
||||
} else {
|
||||
println!(
|
||||
"Crossfade: {}",
|
||||
conn.status()
|
||||
.unwrap()
|
||||
.crossfade
|
||||
.map_or("Disabled".into(), |d| d.as_secs().to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
Commands::Shuffle => {
|
||||
send_notification("Shuffling queueueu...", &n);
|
||||
println!("Shuffling queueueu...");
|
||||
conn.shuffle(..).unwrap();
|
||||
}
|
||||
Commands::Repeat => {
|
||||
let repeat_state = conn.status().unwrap().repeat;
|
||||
conn.repeat(!repeat_state).unwrap();
|
||||
}
|
||||
Commands::Random => {
|
||||
let random_state = conn.status().unwrap().random;
|
||||
// conn.repeat(!random_state).unwrap();
|
||||
println!("{}", random_state)
|
||||
}
|
||||
Commands::Single => {
|
||||
let single_state = conn.status().unwrap().single;
|
||||
conn.single(!single_state).unwrap();
|
||||
}
|
||||
Commands::Consume => {
|
||||
let consume_state = conn.status().unwrap().consume;
|
||||
conn.consume(!consume_state).unwrap();
|
||||
}
|
||||
Commands::Volume { percentage } => {
|
||||
if let Some(volume) = percentage {
|
||||
conn.volume(*volume).unwrap()
|
||||
}
|
||||
let vol = conn.status().unwrap().volume;
|
||||
println!("Volume at {vol}%")
|
||||
}
|
||||
Commands::Search { query, max } => conn.noise_search(query, *max),
|
||||
Commands::Find { query, max, append } => conn.noise_find(query, *max, *append),
|
||||
Commands::Crossfade { seconds } => conn.noise_crossfade(*seconds),
|
||||
Commands::Shuffle => conn.shuf(),
|
||||
Commands::Repeat => conn.toggle_repeat(),
|
||||
Commands::Random => conn.toggle_random(),
|
||||
Commands::Single => conn.toggle_single(),
|
||||
Commands::Consume => conn.toggle_consume(),
|
||||
Commands::Volume { percentage } => conn.vol(*percentage),
|
||||
Commands::Munch => conn.munch(),
|
||||
Commands::Length => conn.length(),
|
||||
}
|
||||
|
||||
if let Commands::Stop
|
||||
| Commands::List { .. }
|
||||
| Commands::Update
|
||||
| Commands::Clear
|
||||
| Commands::Search { .. }
|
||||
| Commands::Find { .. }
|
||||
|
@ -156,86 +65,5 @@ fn main() {
|
|||
return;
|
||||
}
|
||||
}
|
||||
println!("{}", get_status(conn, cli.verbose, n));
|
||||
}
|
||||
|
||||
// fn toggle_state(mut client: Client, state: ) -> () {}
|
||||
|
||||
fn format_song(song: Song) -> String {
|
||||
format!(
|
||||
"{} - {}",
|
||||
song.artist.unwrap_or("Unknown".into()),
|
||||
song.title.unwrap()
|
||||
)
|
||||
}
|
||||
|
||||
trait QuickFmt {
|
||||
fn quick_fmt(&self) -> &'static str;
|
||||
}
|
||||
|
||||
impl QuickFmt for bool {
|
||||
fn quick_fmt(&self) -> &'static str {
|
||||
if *self {
|
||||
"on"
|
||||
} else {
|
||||
"off"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_status(mut client: Client, verbose: bool, notify: Notification) -> String {
|
||||
let current_song = if let Some(song) = client.currentsong().unwrap() {
|
||||
format_song(song)
|
||||
} else {
|
||||
"No song playing".into()
|
||||
};
|
||||
|
||||
send_notification(¤t_song, ¬ify);
|
||||
if !verbose {
|
||||
return current_song;
|
||||
}
|
||||
|
||||
let mut output = Vec::with_capacity(3);
|
||||
output.push(current_song);
|
||||
|
||||
let status = client.status().unwrap();
|
||||
|
||||
output.push(format!(
|
||||
"volume: {vol:<6} repeat: {repeat:<6} random: {rnd:<6} single: {single:<6} consume: {consume:<6}",
|
||||
vol = format!("{}%", status.volume),
|
||||
repeat = status.repeat.quick_fmt(),
|
||||
rnd = status.random.quick_fmt(),
|
||||
single = status.single.quick_fmt(),
|
||||
consume = status.consume.quick_fmt()
|
||||
));
|
||||
|
||||
let state = match status.state {
|
||||
State::Stop => "stopped",
|
||||
State::Pause => "paused",
|
||||
State::Play => "playing",
|
||||
};
|
||||
|
||||
let duration = status.time.map_or("".into(), |(stamp, total)| {
|
||||
let stamp = to_mins_string(stamp);
|
||||
let total = to_mins_string(total);
|
||||
format!("{stamp}/{total}")
|
||||
});
|
||||
|
||||
output.push(format!("[{state}] {duration}"));
|
||||
|
||||
output.join("\n")
|
||||
}
|
||||
|
||||
fn send_notification(body: &str, notify: &Notification) {
|
||||
let _ = notify.update("Noise", body, None);
|
||||
notify.show().unwrap();
|
||||
}
|
||||
|
||||
fn to_mins_string(dur: Duration) -> String {
|
||||
let seconds = dur.as_secs();
|
||||
|
||||
let minutes = seconds / 60;
|
||||
let rem_seconds = seconds % 60;
|
||||
|
||||
format!("{minutes:02}:{rem_seconds:02}")
|
||||
println!("{}", conn.get_status(cli.verbose));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue