feat: Now formats message responses as a table

This commit is contained in:
Nickiel12 2022-12-22 17:52:15 -08:00
parent 388c26fdc8
commit 7ad1c9cdf9
2 changed files with 28 additions and 4 deletions

View file

@ -1,5 +1,5 @@
[package]
name = "SousaCli"
name = "sousa_cli"
version = "0.1.0"
edition = "2021"
@ -10,5 +10,7 @@ clap = { version = "4.0.29", features = ["derive"] }
confy = "0.5.1"
serde = { version = "1.0.151", features = ["derive"] }
serde_json = "1.0.91"
table_print = "0.1.0"
termsize = "0.1.6"
tungstenite = "0.18.0"
url = "2.3.1"

View file

@ -1,6 +1,8 @@
use clap::{Parser, ValueEnum};
use message_types::{PartialTag, UIRequest};
use message_types::{PartialTag, ServerResponse, UIRequest};
use serde_json;
use table_print::Table;
use termsize;
use tungstenite::{connect, Message};
use url::Url;
@ -46,7 +48,7 @@ struct CliArgs {
fn main() {
let cli = CliArgs::parse();
let (mut socket, resp) = connect(
let (mut socket, _resp) = connect(
Url::parse(format!("ws://{}:{}", cli.hostname.unwrap(), cli.port.unwrap()).as_str())
.unwrap(),
)
@ -81,6 +83,26 @@ fn main() {
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!("recieved: {:?}\n{:?}", resp.message, resp.search_results);
resp.pretty_print();
//println!("recieved: {:?}\n{:?}", resp.message, resp.search_results);
socket.close(None).unwrap();
}
impl ServerResponse {
fn pretty_print(self: &Self) -> () {
let mut table = Table::new(vec![
"Title".to_string(),
"Artist".to_string(),
"Album".to_string(),
]);
for i in self.search_results.iter() {
table.insert_row(vec![i.title.clone(), i.artist.clone(), i.album.clone()]);
}
println!(
"{}",
table
.get_pretty(termsize::get().unwrap().cols as usize)
.unwrap()
);
}
}