From 6fff48cfab6d2a9b86ad5847c175523bae21b3ec Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 4 Apr 2021 14:16:54 -0700 Subject: [PATCH] * Messages now grow from the top to the bottom(new messages ontop) + Option to enable/disable message-box borders * Switching over to using bold! macro where applicable This change mostly reduces the eyesoreness of the termion code --- tui/src/main.rs | 78 ++++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/tui/src/main.rs b/tui/src/main.rs index b6cda5c..05710c4 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -3,10 +3,12 @@ mod command; mod config; mod api_types; mod cache; +#[macro_use] mod common; use crate::util::event::{Event, Events}; use crate::command::Command; use crate::cache::Cache; +use crate::config::ConfigFile; use std::{env, fs, error::Error, io}; use clap::{App as Clap, Arg, ArgMatches}; use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen}; @@ -36,13 +38,21 @@ struct App { cache: Cache } -impl Default for App { - fn default() -> App { +impl App { + fn new(config: &ConfigFile) -> App { + let init_commands = vec![ + Command::Text("! /chan | /channel - Switches text context to that channel".into()), + Command::Text("! /serv | /server - Must include port if its not default to 80/443".into()), + Command::Text("! /lh - List hosts in config (-H flag does this on startup)".into()), + Command::Text("! /help - Shows the help menu".into()), + Command::Text("! Commands: available".into()), + ]; + let cache = Cache::from(&config); App { input: String::new(), input_mode: InputMode::Normal, - messages: Vec::new(), - cache: Cache::default() // empty cache lad + messages: init_commands, + cache } } } @@ -58,6 +68,11 @@ fn get_args() -> ArgMatches<'static> { .value_name("CONFIG") .help("Specify path of config to use") .takes_value(true)) + .arg(Arg::with_name("no_border") + .short("n") + .long("no-border") + .takes_value(false) + .help("Removes border from messages box ")) .arg(Arg::with_name("no_login") .short("l") .long("no-login") @@ -69,20 +84,20 @@ async fn main() -> Result<(), Box> { // parameter things first let args = get_args(); - let (config_path, mut config): (String, config::Config) = if args.args.len() == 0 { + let (config_path, config): (String, config::ConfigFile) = if let Some(path) = args.value_of("config") { + match fs::read_to_string(path) { + Ok(data) => (path.into(), serde_json::from_str(&data).unwrap()), + Err(e) => panic!("Unable to parse config file @{}: {}", path, e) + } + } else { let home = env::var("HOME").unwrap(); let path = format!("{}/.config/freechat/config.json", home); match fs::read_to_string(&path) { - Ok(data) => (path, serde_json::from_str(&data).unwrap()), - Err(e) => panic!("Unable to parse config file @{} : {}", path, e) - } - } else{ - let path = args.value_of("config").unwrap(); - match fs::read_to_string(path) { Ok(data) => (path.into(), serde_json::from_str(&data).unwrap()), - Err(e) => panic!("Unable to parse config @ {} : {}", path, e) + Err(e) => panic!("Unable to parse config file @{}: {}", path, e) } }; + let border_opt = args.is_present("no_border"); // Terminal initialization let stdout = io::stdout().into_raw_mode()?; @@ -95,7 +110,7 @@ async fn main() -> Result<(), Box> { let mut events = Events::new(); // Create default app state - let mut app = App::default(); + let mut app = App::new(&config); loop { // Draw UI @@ -105,9 +120,9 @@ async fn main() -> Result<(), Box> { .margin(2) .constraints( [ - Constraint::Length(1), - Constraint::Length(3), - Constraint::Min(1), + Constraint::Length(1), // Info line + Constraint::Length(3), // Text box (w/ borders + Constraint::Min(1), // the rest of it ] .as_ref(), ) @@ -116,18 +131,13 @@ async fn main() -> Result<(), Box> { let (msg, style) = match app.input_mode { InputMode::Normal => ( vec![ - Span::styled("I", Style::default().add_modifier(Modifier::BOLD)), - Span::raw(" for insert mode"), + bold!("I"), + normal!(" for insert mode"), ], Style::default() ), InputMode::Editing => ( - vec![ - Span::raw("Keys: "), - Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)), - Span::styled(" Enter ", Style::default().add_modifier(Modifier::BOLD)), - Span::styled("/help", Style::default().add_modifier(Modifier::BOLD)) - ], + vec![ bold!("/help for commands") ], Style::default(), ), }; @@ -145,15 +155,22 @@ async fn main() -> Result<(), Box> { f.render_widget(input, chunks[1]); let mut lines: Vec = Vec::new(); - for cmd in app.messages.iter() { + for cmd in app.messages.iter().rev() { &lines.push(cmd.styled()); } - let list = Paragraph::new(lines) - .wrap(Wrap { trim: false }) - .block(Block::default() - .borders(Borders::ALL) - .title("Messages")); + let mut list = if border_opt { + Paragraph::new(lines) + .wrap(Wrap { trim: false }) + .block(Block::default() + .title("Messages")) + } else { + Paragraph::new(lines) + .wrap(Wrap { trim: false }) + .block(Block::default() + .title("Messages") + .borders(Borders::ALL)) + }; f.render_widget(list, chunks[2]); @@ -200,6 +217,7 @@ async fn main() -> Result<(), Box> { Command::Channel(id) => app.cache.switch_channel(id).await, Command::Server(host) => app.cache.switch_server(&host).await, Command::Message(msg) => app.cache.send_message(&msg).await, + Command::ListHost => app.cache.list_hosts(), _ => cmd }); }