rmptuia0a3139
use subcommands instead of options
+67 −53
3 files changed
- A src/cli.rs +43 −0
- M src/connection.rs +12 −11
- M src/main.rs +12 −42
| @@ -0,0 +1,43 @@ | |||
| 1 | + | use clap::{Parser, Subcommand}; | |
| 2 | + | #[derive(Parser, Debug)] | |
| 3 | + | #[command(version, about)] | |
| 4 | + | #[clap(author = "krolyxon")] | |
| 5 | + | /// MPD client made with Rust | |
| 6 | + | pub struct Args { | |
| 7 | + | /// pause | |
| 8 | + | #[clap(short, long, default_value = "false")] | |
| 9 | + | pub pause: bool, | |
| 10 | + | ||
| 11 | + | /// toggle pause | |
| 12 | + | #[arg(short, long, default_value = "false")] | |
| 13 | + | pub toggle_pause: bool, | |
| 14 | + | ||
| 15 | + | /// show current status | |
| 16 | + | #[arg(short, long, default_value = "false")] | |
| 17 | + | pub show_status: bool, | |
| 18 | + | ||
| 19 | + | /// use fzf selector for selecting songs | |
| 20 | + | #[arg(short, long, default_value = "false")] | |
| 21 | + | pub fzf_select: bool, | |
| 22 | + | ||
| 23 | + | /// use dmenu selector for selecting songss | |
| 24 | + | #[arg(short, long, default_value = "false")] | |
| 25 | + | pub dmenu_select: bool, | |
| 26 | + | ||
| 27 | + | #[command(subcommand)] | |
| 28 | + | pub command: Command, | |
| 29 | + | } | |
| 30 | + | ||
| 31 | + | #[derive(Debug, Subcommand)] | |
| 32 | + | pub enum Command { | |
| 33 | + | #[command(arg_required_else_help = true)] | |
| 34 | + | Volume { | |
| 35 | + | vol: String, | |
| 36 | + | }, | |
| 37 | + | ||
| 38 | + | Dmenu, | |
| 39 | + | Fzf, | |
| 40 | + | Status, | |
| 41 | + | Pause, | |
| 42 | + | Toggle, | |
| 43 | + | } | |
| @@ -13,8 +13,8 @@ impl Connection { | |||
| 13 | 13 | let songs_filenames: Vec<String> = conn | |
| 14 | 14 | .listall() | |
| 15 | 15 | .unwrap() | |
| 16 | − | .iter() | |
| 17 | − | .map(|x| x.clone().file) | |
| 16 | + | .into_iter() | |
| 17 | + | .map(|x| x.file) | |
| 18 | 18 | .collect(); | |
| 19 | 19 | ||
| 20 | 20 | Ok(Self { | |
| @@ -70,7 +70,7 @@ impl Connection { | |||
| 70 | 70 | let current_song = self.conn.currentsong(); | |
| 71 | 71 | let status = self.conn.status().unwrap(); | |
| 72 | 72 | ||
| 73 | − | if current_song.is_ok() { | |
| 73 | + | if current_song.is_ok() && status.state != State::Stop { | |
| 74 | 74 | let song = current_song.unwrap(); | |
| 75 | 75 | if let Some(s) = song { | |
| 76 | 76 | println!("{} - {}", s.artist.unwrap(), s.title.unwrap()); | |
| @@ -82,7 +82,7 @@ impl Connection { | |||
| 82 | 82 | ); | |
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | − | // Play controls | |
| 85 | + | // Playback controls | |
| 86 | 86 | pub fn pause(&mut self) { | |
| 87 | 87 | self.conn.pause(true).unwrap(); | |
| 88 | 88 | } | |
| @@ -92,14 +92,15 @@ impl Connection { | |||
| 92 | 92 | } | |
| 93 | 93 | ||
| 94 | 94 | // Volume controls | |
| 95 | − | pub fn inc_volume(&mut self, inc: i8) { | |
| 95 | + | pub fn set_volume(&mut self, u: String) { | |
| 96 | 96 | let cur = self.conn.status().unwrap().volume; | |
| 97 | − | self.conn.volume(cur + inc).unwrap(); | |
| 98 | − | } | |
| 99 | − | ||
| 100 | − | pub fn dec_volume(&mut self, dec: i8) { | |
| 101 | − | let cur = self.conn.status().unwrap().volume; | |
| 102 | − | self.conn.volume(cur - dec).unwrap(); | |
| 97 | + | let sym = u.get(0..1).unwrap(); | |
| 98 | + | let u: i8 = u.parse::<i8>().unwrap(); | |
| 99 | + | if sym == "+" || sym == "-" { | |
| 100 | + | self.conn.volume(cur + u).unwrap(); | |
| 101 | + | } else { | |
| 102 | + | self.conn.volume(u).unwrap(); | |
| 103 | + | } | |
| 103 | 104 | } | |
| 104 | 105 | } | |
| 105 | 106 | ||
| @@ -1,53 +1,23 @@ | |||
| 1 | + | mod cli; | |
| 1 | 2 | mod connection; | |
| 2 | 3 | use clap::Parser; | |
| 4 | + | use cli::Args; | |
| 5 | + | use cli::Command; | |
| 3 | 6 | use connection::Connection; | |
| 4 | 7 | ||
| 5 | − | #[derive(Parser, Debug)] | |
| 6 | − | #[command(version, about, long_about = None)] | |
| 7 | − | struct Args { | |
| 8 | − | /// pause | |
| 9 | − | #[arg(short, long, default_value = "false")] | |
| 10 | − | pub pause: bool, | |
| 11 | − | ||
| 12 | − | /// toggle pause | |
| 13 | − | #[arg(short, long, default_value = "false")] | |
| 14 | − | pub toggle_pause: bool, | |
| 15 | − | ||
| 16 | − | /// show current status | |
| 17 | − | #[arg(short, long, default_value = "false")] | |
| 18 | − | pub show_status: bool, | |
| 19 | − | ||
| 20 | − | /// use fzf selector for selecting songs | |
| 21 | − | #[arg(short, long, default_value = "false")] | |
| 22 | − | pub fzf_select: bool, | |
| 23 | − | ||
| 24 | − | /// use dmenu selector for selecting songss | |
| 25 | − | #[arg(short, long, default_value = "false")] | |
| 26 | − | pub dmenu_select: bool, | |
| 27 | − | } | |
| 28 | − | ||
| 29 | 8 | fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| 30 | 9 | let args = Args::parse(); | |
| 31 | 10 | let mut conn = Connection::new("127.0.0.1:6600")?; | |
| 32 | 11 | ||
| 33 | − | if args.show_status { | |
| 34 | − | conn.status(); | |
| 35 | − | } | |
| 36 | − | ||
| 37 | − | if args.toggle_pause { | |
| 38 | − | conn.toggle_pause(); | |
| 39 | − | } | |
| 40 | − | ||
| 41 | − | if args.pause { | |
| 42 | − | conn.pause(); | |
| 43 | − | } | |
| 44 | − | ||
| 45 | − | if args.fzf_select { | |
| 46 | − | conn.play_fzf(); | |
| 47 | − | } | |
| 48 | − | ||
| 49 | − | if args.dmenu_select { | |
| 50 | − | conn.play_dmenu(); | |
| 12 | + | match args.command { | |
| 13 | + | Command::Volume { vol } => { | |
| 14 | + | conn.set_volume(vol); | |
| 15 | + | } | |
| 16 | + | Command::Dmenu => conn.play_dmenu(), | |
| 17 | + | Command::Fzf => conn.play_fzf(), | |
| 18 | + | Command::Status => conn.status(), | |
| 19 | + | Command::Pause => conn.pause(), | |
| 20 | + | Command::Toggle => conn.toggle_pause(), | |
| 51 | 21 | } | |
| 52 | 22 | ||
| 53 | 23 | Ok(()) | |