From ee2e621d5b19de08becd33c13520f8826407cf84 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 15:00:27 +0200 Subject: change data in response to channel-list to a tree --- mumctl/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mumctl/src') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index ae4acc5..97dba53 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,5 +1,5 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; -use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; +use ipc_channel::ipc::{self, IpcSender}; use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; -- cgit v1.2.1 From d740d78b0b18543928355a7183ba770a6cc0368a Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 15:47:50 +0200 Subject: add pretty print for channel list --- mumctl/src/main.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'mumctl/src') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 97dba53..592bd6b 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,10 +1,10 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; use ipc_channel::ipc::{self, IpcSender}; -use log::*; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; -use std::{fs, io}; +use std::{fs, io, iter}; use colored::Colorize; +use mumlib::state::Channel; macro_rules! err_print { ($func:expr) => { @@ -16,7 +16,6 @@ macro_rules! err_print { fn main() { setup_logger(); - debug!("Logger up!"); let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) @@ -68,7 +67,12 @@ fn main() { if let Some(_matches) = matches.subcommand_matches("list") { match send_command(Command::ChannelList) { Ok(res) => { - println!("{:#?}", res.unwrap()); + match res { + Some(CommandResponse::ChannelList { channels }) => { + print_channel(&channels, 0); + } + _ => unreachable!(), + } } Err(e) => println!("{} {}", "error:".red(), e), } @@ -115,3 +119,17 @@ fn send_command(command: Command) -> mumlib::error::Result(), channel.name.bold(), if channel.max_users != 0 { + format!(" {}/{}", channel.users.len(), channel.max_users) + } else { + "".to_string() + }); + for user in &channel.users { + println!("{}-{}", iter::repeat(" ").take(depth + 1).collect::(), user.name); + } + for child in &channel.children { + print_channel(child, depth + 1); + } +} \ No newline at end of file -- cgit v1.2.1 From b10d20a20496eb9287975fc9fd9b688d59896031 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 16:16:23 +0200 Subject: change status data transfer struct --- mumctl/src/main.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'mumctl/src') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index 592bd6b..cfd48f8 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -88,8 +88,6 @@ fn main() { } Err(e) => println!("{} {}", "error:".red(), e), } - let res = send_command(Command::Status).unwrap().unwrap(); - println!("{:#?}", res); } else if let Some(matches) = matches.subcommand_matches("completions") { app.gen_completions_to("mumctl", match matches.value_of("shell").unwrap_or("zsh") { -- cgit v1.2.1 From c873e4250c58d872763129bff2c0b0e6c91c0a2a Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 18:39:36 +0200 Subject: add pretty printing for mumctl status --- mumctl/src/main.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'mumctl/src') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index cfd48f8..c73b793 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -6,6 +6,8 @@ use std::{fs, io, iter}; use colored::Colorize; use mumlib::state::Channel; +const INDENTATION: &str = " "; + macro_rules! err_print { ($func:expr) => { if let Err(e) = $func { @@ -84,7 +86,18 @@ fn main() { } else if let Some(_matches) = matches.subcommand_matches("status") { match send_command(Command::Status) { Ok(res) => { - println!("{:#?}", res.unwrap()); + match res { + Some(CommandResponse::Status { server_state }) => { + println!("Connected to {} as {}", server_state.host, server_state.username); + let own_channel = server_state.channels.iter().find(|e| e.users.iter().any(|e| e.name == server_state.username)).unwrap(); + println!("Currently in {} with {} other client{}:", own_channel.name, own_channel.users.len() - 1, if own_channel.users.len() == 2 { "" } else { "s" }); + println!("{}{}", INDENTATION, own_channel.name); + for user in &own_channel.users { + println!("{}{}{}", INDENTATION, INDENTATION, user); + } + } + _ => unreachable!(), + } } Err(e) => println!("{} {}", "error:".red(), e), } @@ -119,13 +132,13 @@ fn send_command(command: Command) -> mumlib::error::Result(), channel.name.bold(), if channel.max_users != 0 { + println!("{}{}{}", iter::repeat(INDENTATION).take(depth).collect::(), channel.name.bold(), if channel.max_users != 0 { format!(" {}/{}", channel.users.len(), channel.max_users) } else { "".to_string() }); for user in &channel.users { - println!("{}-{}", iter::repeat(" ").take(depth + 1).collect::(), user.name); + println!("{}-{}", iter::repeat(INDENTATION).take(depth + 1).collect::(), user); } for child in &channel.children { print_channel(child, depth + 1); -- cgit v1.2.1 From 821107122299eebde5da1223ea328f63782ceac9 Mon Sep 17 00:00:00 2001 From: Eskil Queseth Date: Sat, 17 Oct 2020 20:44:54 +0200 Subject: cargo fmt --- mumctl/src/main.rs | 161 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 91 insertions(+), 70 deletions(-) (limited to 'mumctl/src') diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs index c73b793..bcb5e71 100644 --- a/mumctl/src/main.rs +++ b/mumctl/src/main.rs @@ -1,10 +1,10 @@ use clap::{App, AppSettings, Arg, Shell, SubCommand}; +use colored::Colorize; use ipc_channel::ipc::{self, IpcSender}; use mumlib::command::{Command, CommandResponse}; use mumlib::setup_logger; -use std::{fs, io, iter}; -use colored::Colorize; use mumlib::state::Channel; +use std::{fs, io, iter}; const INDENTATION: &str = " "; @@ -21,37 +21,38 @@ fn main() { let mut app = App::new("mumctl") .setting(AppSettings::ArgRequiredElseHelp) - .subcommand(SubCommand::with_name("server") - .setting(AppSettings::ArgRequiredElseHelp) - .subcommand(SubCommand::with_name("connect") - .setting(AppSettings::ArgRequiredElseHelp) - .arg(Arg::with_name("host") - .required(true) - .index(1)) - .arg(Arg::with_name("username") - .required(true) - .index(2))) - .subcommand(SubCommand::with_name("disconnect"))) - .subcommand(SubCommand::with_name("channel") - .setting(AppSettings::ArgRequiredElseHelp) - .subcommand(SubCommand::with_name("list") - .arg(Arg::with_name("short") - .short("s") - .long("short"))) - .subcommand(SubCommand::with_name("connect") - .arg(Arg::with_name("channel") - .required(true)))) + .subcommand( + SubCommand::with_name("server") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand( + SubCommand::with_name("connect") + .setting(AppSettings::ArgRequiredElseHelp) + .arg(Arg::with_name("host").required(true).index(1)) + .arg(Arg::with_name("username").required(true).index(2)), + ) + .subcommand(SubCommand::with_name("disconnect")), + ) + .subcommand( + SubCommand::with_name("channel") + .setting(AppSettings::ArgRequiredElseHelp) + .subcommand( + SubCommand::with_name("list") + .arg(Arg::with_name("short").short("s").long("short")), + ) + .subcommand( + SubCommand::with_name("connect").arg(Arg::with_name("channel").required(true)), + ), + ) .subcommand(SubCommand::with_name("status")) - .subcommand(SubCommand::with_name("completions") - .arg(Arg::with_name("zsh") - .long("zsh")) - .arg(Arg::with_name("bash") - .long("bash")) - .arg(Arg::with_name("fish") - .long("fish"))); + .subcommand( + SubCommand::with_name("completions") + .arg(Arg::with_name("zsh").long("zsh")) + .arg(Arg::with_name("bash").long("bash")) + .arg(Arg::with_name("fish").long("fish")), + ); let matches = app.clone().get_matches(); - + if let Some(matches) = matches.subcommand_matches("server") { if let Some(matches) = matches.subcommand_matches("connect") { let host = matches.value_of("host").unwrap(); @@ -68,14 +69,12 @@ fn main() { } else if let Some(matches) = matches.subcommand_matches("channel") { if let Some(_matches) = matches.subcommand_matches("list") { match send_command(Command::ChannelList) { - Ok(res) => { - match res { - Some(CommandResponse::ChannelList { channels }) => { - print_channel(&channels, 0); - } - _ => unreachable!(), + Ok(res) => match res { + Some(CommandResponse::ChannelList { channels }) => { + print_channel(&channels, 0); } - } + _ => unreachable!(), + }, Err(e) => println!("{} {}", "error:".red(), e), } } else if let Some(matches) = matches.subcommand_matches("connect") { @@ -85,42 +84,53 @@ fn main() { } } else if let Some(_matches) = matches.subcommand_matches("status") { match send_command(Command::Status) { - Ok(res) => { - match res { - Some(CommandResponse::Status { server_state }) => { - println!("Connected to {} as {}", server_state.host, server_state.username); - let own_channel = server_state.channels.iter().find(|e| e.users.iter().any(|e| e.name == server_state.username)).unwrap(); - println!("Currently in {} with {} other client{}:", own_channel.name, own_channel.users.len() - 1, if own_channel.users.len() == 2 { "" } else { "s" }); - println!("{}{}", INDENTATION, own_channel.name); - for user in &own_channel.users { - println!("{}{}{}", INDENTATION, INDENTATION, user); + Ok(res) => match res { + Some(CommandResponse::Status { server_state }) => { + println!( + "Connected to {} as {}", + server_state.host, server_state.username + ); + let own_channel = server_state + .channels + .iter() + .find(|e| e.users.iter().any(|e| e.name == server_state.username)) + .unwrap(); + println!( + "Currently in {} with {} other client{}:", + own_channel.name, + own_channel.users.len() - 1, + if own_channel.users.len() == 2 { + "" + } else { + "s" } + ); + println!("{}{}", INDENTATION, own_channel.name); + for user in &own_channel.users { + println!("{}{}{}", INDENTATION, INDENTATION, user); } - _ => unreachable!(), } - } + _ => unreachable!(), + }, Err(e) => println!("{} {}", "error:".red(), e), } } else if let Some(matches) = matches.subcommand_matches("completions") { - app.gen_completions_to("mumctl", - match matches.value_of("shell").unwrap_or("zsh") { - "bash" => { - Shell::Bash - }, - "fish" => { - Shell::Fish - }, - _ => { - Shell::Zsh - }, - }, - &mut io::stdout()); - return; + app.gen_completions_to( + "mumctl", + match matches.value_of("shell").unwrap_or("zsh") { + "bash" => Shell::Bash, + "fish" => Shell::Fish, + _ => Shell::Zsh, + }, + &mut io::stdout(), + ); + return; }; } fn send_command(command: Command) -> mumlib::error::Result> { - let (tx_client, rx_client) = ipc::channel::>>().unwrap(); + let (tx_client, rx_client) = + ipc::channel::>>().unwrap(); let server_name = fs::read_to_string("/var/tmp/mumd-oneshot").unwrap(); //TODO don't panic @@ -132,15 +142,26 @@ fn send_command(command: Command) -> mumlib::error::Result(), channel.name.bold(), if channel.max_users != 0 { - format!(" {}/{}", channel.users.len(), channel.max_users) - } else { - "".to_string() - }); + println!( + "{}{}{}", + iter::repeat(INDENTATION).take(depth).collect::(), + channel.name.bold(), + if channel.max_users != 0 { + format!(" {}/{}", channel.users.len(), channel.max_users) + } else { + "".to_string() + } + ); for user in &channel.users { - println!("{}-{}", iter::repeat(INDENTATION).take(depth + 1).collect::(), user); + println!( + "{}-{}", + iter::repeat(INDENTATION) + .take(depth + 1) + .collect::(), + user + ); } for child in &channel.children { print_channel(child, depth + 1); } -} \ No newline at end of file +} -- cgit v1.2.1