* 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
This commit is contained in:
shockrah 2021-04-04 14:16:54 -07:00
parent 43e3f93aad
commit 6fff48cfab

View File

@ -3,10 +3,12 @@ mod command;
mod config; mod config;
mod api_types; mod api_types;
mod cache; mod cache;
#[macro_use] mod common;
use crate::util::event::{Event, Events}; use crate::util::event::{Event, Events};
use crate::command::Command; use crate::command::Command;
use crate::cache::Cache; use crate::cache::Cache;
use crate::config::ConfigFile;
use std::{env, fs, error::Error, io}; use std::{env, fs, error::Error, io};
use clap::{App as Clap, Arg, ArgMatches}; use clap::{App as Clap, Arg, ArgMatches};
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen}; use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
@ -36,13 +38,21 @@ struct App {
cache: Cache cache: Cache
} }
impl Default for App { impl App {
fn default() -> App { fn new(config: &ConfigFile) -> App {
let init_commands = vec![
Command::Text("! /chan | /channel <u64> - Switches text context to that channel".into()),
Command::Text("! /serv | /server <hostname> - 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 { App {
input: String::new(), input: String::new(),
input_mode: InputMode::Normal, input_mode: InputMode::Normal,
messages: Vec::new(), messages: init_commands,
cache: Cache::default() // empty cache lad cache
} }
} }
} }
@ -58,6 +68,11 @@ fn get_args() -> ArgMatches<'static> {
.value_name("CONFIG") .value_name("CONFIG")
.help("Specify path of config to use") .help("Specify path of config to use")
.takes_value(true)) .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") .arg(Arg::with_name("no_login")
.short("l") .short("l")
.long("no-login") .long("no-login")
@ -69,20 +84,20 @@ async fn main() -> Result<(), Box<dyn Error>> {
// parameter things first // parameter things first
let args = get_args(); 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 home = env::var("HOME").unwrap();
let path = format!("{}/.config/freechat/config.json", home); let path = format!("{}/.config/freechat/config.json", home);
match fs::read_to_string(&path) { 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()), 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 // Terminal initialization
let stdout = io::stdout().into_raw_mode()?; let stdout = io::stdout().into_raw_mode()?;
@ -95,7 +110,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut events = Events::new(); let mut events = Events::new();
// Create default app state // Create default app state
let mut app = App::default(); let mut app = App::new(&config);
loop { loop {
// Draw UI // Draw UI
@ -105,9 +120,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
.margin(2) .margin(2)
.constraints( .constraints(
[ [
Constraint::Length(1), Constraint::Length(1), // Info line
Constraint::Length(3), Constraint::Length(3), // Text box (w/ borders
Constraint::Min(1), Constraint::Min(1), // the rest of it
] ]
.as_ref(), .as_ref(),
) )
@ -116,18 +131,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
let (msg, style) = match app.input_mode { let (msg, style) = match app.input_mode {
InputMode::Normal => ( InputMode::Normal => (
vec![ vec![
Span::styled("I", Style::default().add_modifier(Modifier::BOLD)), bold!("I"),
Span::raw(" for insert mode"), normal!(" for insert mode"),
], ],
Style::default() Style::default()
), ),
InputMode::Editing => ( InputMode::Editing => (
vec![ vec![ bold!("/help for commands") ],
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))
],
Style::default(), Style::default(),
), ),
}; };
@ -145,15 +155,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
f.render_widget(input, chunks[1]); f.render_widget(input, chunks[1]);
let mut lines: Vec<Spans> = Vec::new(); let mut lines: Vec<Spans> = Vec::new();
for cmd in app.messages.iter() { for cmd in app.messages.iter().rev() {
&lines.push(cmd.styled()); &lines.push(cmd.styled());
} }
let list = Paragraph::new(lines) let mut list = if border_opt {
.wrap(Wrap { trim: false }) Paragraph::new(lines)
.block(Block::default() .wrap(Wrap { trim: false })
.borders(Borders::ALL) .block(Block::default()
.title("Messages")); .title("Messages"))
} else {
Paragraph::new(lines)
.wrap(Wrap { trim: false })
.block(Block::default()
.title("Messages")
.borders(Borders::ALL))
};
f.render_widget(list, chunks[2]); f.render_widget(list, chunks[2]);
@ -200,6 +217,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
Command::Channel(id) => app.cache.switch_channel(id).await, Command::Channel(id) => app.cache.switch_channel(id).await,
Command::Server(host) => app.cache.switch_server(&host).await, Command::Server(host) => app.cache.switch_server(&host).await,
Command::Message(msg) => app.cache.send_message(&msg).await, Command::Message(msg) => app.cache.send_message(&msg).await,
Command::ListHost => app.cache.list_hosts(),
_ => cmd _ => cmd
}); });
} }