use subcommands instead of options
This commit is contained in:
parent
3578eae01a
commit
a0a3139964
|
|
@ -0,0 +1,43 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
#[clap(author = "krolyxon")]
|
||||
/// MPD client made with Rust
|
||||
pub struct Args {
|
||||
/// pause
|
||||
#[clap(short, long, default_value = "false")]
|
||||
pub pause: bool,
|
||||
|
||||
/// toggle pause
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub toggle_pause: bool,
|
||||
|
||||
/// show current status
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub show_status: bool,
|
||||
|
||||
/// use fzf selector for selecting songs
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub fzf_select: bool,
|
||||
|
||||
/// use dmenu selector for selecting songss
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub dmenu_select: bool,
|
||||
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum Command {
|
||||
#[command(arg_required_else_help = true)]
|
||||
Volume {
|
||||
vol: String,
|
||||
},
|
||||
|
||||
Dmenu,
|
||||
Fzf,
|
||||
Status,
|
||||
Pause,
|
||||
Toggle,
|
||||
}
|
||||
|
|
@ -13,8 +13,8 @@ impl Connection {
|
|||
let songs_filenames: Vec<String> = conn
|
||||
.listall()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|x| x.clone().file)
|
||||
.into_iter()
|
||||
.map(|x| x.file)
|
||||
.collect();
|
||||
|
||||
Ok(Self {
|
||||
|
|
@ -70,7 +70,7 @@ impl Connection {
|
|||
let current_song = self.conn.currentsong();
|
||||
let status = self.conn.status().unwrap();
|
||||
|
||||
if current_song.is_ok() {
|
||||
if current_song.is_ok() && status.state != State::Stop {
|
||||
let song = current_song.unwrap();
|
||||
if let Some(s) = song {
|
||||
println!("{} - {}", s.artist.unwrap(), s.title.unwrap());
|
||||
|
|
@ -82,7 +82,7 @@ impl Connection {
|
|||
);
|
||||
}
|
||||
|
||||
// Play controls
|
||||
// Playback controls
|
||||
pub fn pause(&mut self) {
|
||||
self.conn.pause(true).unwrap();
|
||||
}
|
||||
|
|
@ -92,14 +92,15 @@ impl Connection {
|
|||
}
|
||||
|
||||
// Volume controls
|
||||
pub fn inc_volume(&mut self, inc: i8) {
|
||||
pub fn set_volume(&mut self, u: String) {
|
||||
let cur = self.conn.status().unwrap().volume;
|
||||
self.conn.volume(cur + inc).unwrap();
|
||||
let sym = u.get(0..1).unwrap();
|
||||
let u: i8 = u.parse::<i8>().unwrap();
|
||||
if sym == "+" || sym == "-" {
|
||||
self.conn.volume(cur + u).unwrap();
|
||||
} else {
|
||||
self.conn.volume(u).unwrap();
|
||||
}
|
||||
|
||||
pub fn dec_volume(&mut self, dec: i8) {
|
||||
let cur = self.conn.status().unwrap().volume;
|
||||
self.conn.volume(cur - dec).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
52
src/main.rs
52
src/main.rs
|
|
@ -1,53 +1,23 @@
|
|||
mod cli;
|
||||
mod connection;
|
||||
use clap::Parser;
|
||||
use cli::Args;
|
||||
use cli::Command;
|
||||
use connection::Connection;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
/// pause
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub pause: bool,
|
||||
|
||||
/// toggle pause
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub toggle_pause: bool,
|
||||
|
||||
/// show current status
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub show_status: bool,
|
||||
|
||||
/// use fzf selector for selecting songs
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub fzf_select: bool,
|
||||
|
||||
/// use dmenu selector for selecting songss
|
||||
#[arg(short, long, default_value = "false")]
|
||||
pub dmenu_select: bool,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Args::parse();
|
||||
let mut conn = Connection::new("127.0.0.1:6600")?;
|
||||
|
||||
if args.show_status {
|
||||
conn.status();
|
||||
match args.command {
|
||||
Command::Volume { vol } => {
|
||||
conn.set_volume(vol);
|
||||
}
|
||||
|
||||
if args.toggle_pause {
|
||||
conn.toggle_pause();
|
||||
}
|
||||
|
||||
if args.pause {
|
||||
conn.pause();
|
||||
}
|
||||
|
||||
if args.fzf_select {
|
||||
conn.play_fzf();
|
||||
}
|
||||
|
||||
if args.dmenu_select {
|
||||
conn.play_dmenu();
|
||||
Command::Dmenu => conn.play_dmenu(),
|
||||
Command::Fzf => conn.play_fzf(),
|
||||
Command::Status => conn.status(),
|
||||
Command::Pause => conn.pause(),
|
||||
Command::Toggle => conn.toggle_pause(),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Reference in New Issue