cli: added options to select index of failed switchto

This commit is contained in:
Nickiel12 2022-12-24 16:32:57 -08:00
parent fea482479a
commit 7dd4702595
2 changed files with 121 additions and 9 deletions

View file

@ -1,5 +1,5 @@
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use message_types::{PartialTag, ServerResponse, UIRequest}; use message_types::{itemtag_to_partial, PartialTag, ServerResponse, UIRequest};
use serde_json; use serde_json;
use table_print::Table; use table_print::Table;
use termsize; use termsize;
@ -14,6 +14,7 @@ enum SousaCommands {
Pause, Pause,
Search, Search,
SwitchTo, SwitchTo,
StatusUpdate,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -31,10 +32,17 @@ struct CliArgs {
#[arg(index = 1, value_enum)] #[arg(index = 1, value_enum)]
action: Option<SousaCommands>, action: Option<SousaCommands>,
/// The string to search for when paired with a "Search" action /// The value used to search/switch tracks
#[arg(index = 2, requires("field"))] #[arg(
index = 2,
requires("field"),
required_if_eq_any([("action", "search"), ("action", "SwitchTo")])
)]
search_arg: Option<String>, search_arg: Option<String>,
#[arg(long, required_if_eq("action", "SwitchTo"))]
choice_index: Option<usize>,
/// The field to search for when running `search` /// The field to search for when running `search`
#[arg( #[arg(
long, long,
@ -73,21 +81,87 @@ fn main() {
}; };
serde_json::to_string(&request).unwrap() 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(); socket.write_message(Message::Text(message_string)).unwrap();
let msg = socket.read_message().expect("Error reading message"); let server_message = 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);
if resp.search_results.len() > 0 { let server_response: message_types::ServerResponse =
resp.pretty_print(); 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!("recieved: {:?}\n{:?}", resp.message, resp.search_results);
println!("Closing Socket");
socket.close(None).unwrap(); 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<PartialTag, ()> { fn parse_to_partialtag(field: String, value: String) -> Result<PartialTag, ()> {
match field.to_lowercase().as_str() { match field.to_lowercase().as_str() {
"title" => Ok(PartialTag { "title" => Ok(PartialTag {

View file

@ -94,3 +94,41 @@ pub enum UIRequest {
SwitchTo(PartialTag), SwitchTo(PartialTag),
GetStatus, 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;
}