diff --git a/src/main.rs b/src/main.rs index 019e6a7..1f2276b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::{Parser, ValueEnum}; -use message_types::{PartialTag, ServerResponse, UIRequest}; +use message_types::{itemtag_to_partial, PartialTag, ServerResponse, UIRequest}; use serde_json; use table_print::Table; use termsize; @@ -14,6 +14,7 @@ enum SousaCommands { Pause, Search, SwitchTo, + StatusUpdate, } #[derive(Parser, Debug)] @@ -31,10 +32,17 @@ struct CliArgs { #[arg(index = 1, value_enum)] action: Option, - /// The string to search for when paired with a "Search" action - #[arg(index = 2, requires("field"))] + /// The value used to search/switch tracks + #[arg( + index = 2, + requires("field"), + required_if_eq_any([("action", "search"), ("action", "SwitchTo")]) + )] search_arg: Option, + #[arg(long, required_if_eq("action", "SwitchTo"))] + choice_index: Option, + /// The field to search for when running `search` #[arg( long, @@ -73,21 +81,87 @@ fn main() { }; serde_json::to_string(&request).unwrap() } + SousaCommands::StatusUpdate => { + let request = UIRequest::GetStatus; + serde_json::to_string(&request).unwrap() + } }; socket.write_message(Message::Text(message_string)).unwrap(); - let msg = socket.read_message().expect("Error reading message"); - let resp: message_types::ServerResponse = - serde_json::from_str(msg.into_text().unwrap().as_str()).unwrap(); - println!("\n{}\n", resp.message); + let server_message = socket.read_message().expect("Error reading message"); - if resp.search_results.len() > 0 { - resp.pretty_print(); + let server_response: message_types::ServerResponse = + serde_json::from_str(server_message.into_text().unwrap().as_str()).unwrap(); + + println!("{}", server_response.message.clone()); + if server_response + .message + .starts_with("Multiple results found") + { + if cli.choice_index.is_some() { + let index = cli.choice_index.unwrap(); + if index > server_response.search_results.len() { + println!("That index was larger than the list of options.\nTry running the command again without the `--choice-index` flag"); + } else { + println!("Sending second server request"); + let return_choice = server_response.search_results.get(index).unwrap(); + socket + .write_message( + serde_json::to_string(&UIRequest::SwitchTo(itemtag_to_partial( + return_choice, + ))) + .unwrap() + .into(), + ) + .unwrap(); + } + } else { + choose_switch_to(server_response); + } + } else { + println!("\n{}\n", server_response.message); + + if server_response.search_results.len() > 0 { + server_response.pretty_print(); + } } //println!("recieved: {:?}\n{:?}", resp.message, resp.search_results); + println!("Closing Socket"); socket.close(None).unwrap(); } +fn choose_switch_to(msg: ServerResponse) { + println!("\n\n{}\n\n", msg.message); + + let mut table = Table::new(vec![ + "Index".to_string(), + "Title".to_string(), + "Artist".to_string(), + "Album".to_string(), + ]); + + let mut count: usize = 0; + for i in msg.search_results.iter() { + table.insert_row(vec![ + count.to_string(), + i.title.clone(), + i.artist.clone(), + i.album.clone(), + ]); + count += 1; + } + println!( + "{}", + table + .get_pretty(termsize::get().unwrap().cols as usize) + .unwrap() + ); + + println!( + "Please enter the index of the desired song as an arguement. e.g.: `--choice-index=1`" + ); +} + fn parse_to_partialtag(field: String, value: String) -> Result { match field.to_lowercase().as_str() { "title" => Ok(PartialTag { diff --git a/src/message_types.rs b/src/message_types.rs index a9d7d9c..a21a355 100644 --- a/src/message_types.rs +++ b/src/message_types.rs @@ -94,3 +94,41 @@ pub enum UIRequest { SwitchTo(PartialTag), GetStatus, } + +pub fn itemtag_to_partial(itemtag: &ItemTag) -> PartialTag { + let mut partial_tag = PartialTag { + ..PartialTag::default() + }; + + if itemtag.path.is_empty() { + partial_tag.path = None; + } else { + partial_tag.path = Some(itemtag.path.clone()); + } + + if itemtag.title.is_empty() { + partial_tag.title = None; + } else { + partial_tag.title = Some(itemtag.title.clone()); + } + + if itemtag.album.is_empty() { + partial_tag.album = None; + } else { + partial_tag.album = Some(itemtag.album.clone()); + } + + if itemtag.artist.is_empty() { + partial_tag.artist = None; + } else { + partial_tag.artist = Some(itemtag.artist.clone()); + } + + if itemtag.album_artist.is_empty() { + partial_tag.album_artist = None; + } else { + partial_tag.album_artist = Some(itemtag.album_artist.clone()); + } + + return partial_tag; +}