From e46a6819b5f6a83ae81b3484369d2072c7f09d35 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 19 Jul 2020 10:55:52 +0530 Subject: improve command mode positioning/ux --- src/app/impl_self.rs | 4 +-- src/command.rs | 90 +++++++++++++++++++++++++++++----------------------- src/main.rs | 8 +++-- src/theme.rs | 6 ++-- 4 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index 7bf9ab0..b82dfb9 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -237,9 +237,7 @@ impl App { Command::Quit => self.save_state(), Command::MonthNext => self.sift_forward(), Command::MonthPrev => self.sift_backward(), - _ => { - eprintln!("UNKNOWN COMMAND!"); - } + Command::Blank => {} }, Err(e) => { self.message.set_message(e.to_string()); diff --git a/src/command.rs b/src/command.rs index d285138..f856b00 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,21 +1,46 @@ use std::fmt; +use cursive::theme::{BaseColor, Color, ColorStyle}; use cursive::view::Resizable; -use cursive::views::{Dialog, EditView}; +use cursive::views::{EditView, LinearLayout, TextView}; use cursive::Cursive; -use crate::app::App; +use crate::{app::App, CONFIGURATION}; pub fn open_command_window(s: &mut Cursive) { - let command_window = Dialog::around(EditView::new().on_submit(call_on_app).fixed_width(40)); - s.add_layer(command_window); + let command_window = EditView::new() + .filler(" ") + .on_submit(call_on_app) + .style(ColorStyle::new( + Color::Dark(BaseColor::Black), + Color::Dark(BaseColor::White), + )) + .fixed_width(CONFIGURATION.view_width * CONFIGURATION.grid_width); + s.call_on_name("Frame", |view: &mut LinearLayout| { + let mut commandline = LinearLayout::horizontal() + .child(TextView::new(":")) + .child(command_window); + commandline.set_focus_index(1); + view.add_child(commandline); + view.set_focus_index(1); + }); } fn call_on_app(s: &mut Cursive, input: &str) { + // things to do after recieving the command + // 1. parse the command + // 2. clean existing command messages + // 3. remove the command window + // 4. handle quit command s.call_on_name("Main", |view: &mut App| { let cmd = Command::from_string(input); + view.clear_message(); view.parse_command(cmd); }); + s.call_on_name("Frame", |view: &mut LinearLayout| { + view.set_focus_index(0); + view.remove_child(view.get_focus_index()); + }); // special command that requires access to // our main cursive object, has to be parsed again @@ -24,8 +49,6 @@ fn call_on_app(s: &mut Cursive, input: &str) { if let Ok(Command::Quit) = Command::from_string(input) { s.quit(); } - - s.pop_layer(); } #[derive(PartialEq)] @@ -72,41 +95,27 @@ impl Command { let first = strings.first().unwrap().to_string(); let mut args: Vec = strings.iter_mut().skip(1).map(|s| s.to_string()).collect(); - match first.as_ref() { - "add" | "a" => { - if args.is_empty() { - return Err(CommandLineError::NotEnoughArgs(first, 1)); - } - let goal = args - .get(1) - .map(|x| { - x.parse::() - .map_err(|_| CommandLineError::InvalidArg(1)) - }) - .transpose()?; - return Ok(Command::Add( - args.get_mut(0).unwrap().to_string(), - goal, - false, - )); - } - "add-auto" | "aa" => { - if args.is_empty() { - return Err(CommandLineError::NotEnoughArgs(first, 1)); - } - let goal = args - .get(1) - .map(|x| { - x.parse::() - .map_err(|_| CommandLineError::InvalidArg(1)) - }) - .transpose()?; - return Ok(Command::Add( - args.get_mut(0).unwrap().to_string(), - goal, - true, - )); + let mut _add = |auto: bool, first: String| { + if args.is_empty() { + return Err(CommandLineError::NotEnoughArgs(first, 1)); } + let goal = args + .get(1) + .map(|x| { + x.parse::() + .map_err(|_| CommandLineError::InvalidArg(2)) + }) + .transpose()?; + return Ok(Command::Add( + args.get_mut(0).unwrap().to_string(), + goal, + auto, + )); + }; + + match first.as_ref() { + "add" | "a" => _add(false, first), + "add-auto" | "aa" => _add(true, first), "delete" | "d" => { if args.is_empty() { return Err(CommandLineError::NotEnoughArgs(first, 1)); @@ -128,6 +137,7 @@ impl Command { "mprev" | "month-prev" => return Ok(Command::MonthPrev), "mnext" | "month-next" => return Ok(Command::MonthNext), "q" | "quit" => return Ok(Command::Quit), + "" => return Ok(Command::Blank), s => return Err(CommandLineError::InvalidCommand(s.into())), } } diff --git a/src/main.rs b/src/main.rs index f83fc83..d96119e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use crate::utils::{load_configuration_file, AppConfig}; use clap::{App as ClapApp, Arg}; use cursive::termion; -use cursive::views::NamedView; +use cursive::views::{LinearLayout, NamedView}; use lazy_static::lazy_static; lazy_static! { @@ -52,7 +52,11 @@ fn main() { } else { let mut s = termion().unwrap(); let app = App::load_state(); - s.add_layer(NamedView::new("Main", app)); + let layout = NamedView::new( + "Frame", + LinearLayout::vertical().child(NamedView::new("Main", app)), + ); + s.add_layer(layout); s.add_global_callback(':', |s| open_command_window(s)); s.set_theme(theme::theme_gen()); diff --git a/src/theme.rs b/src/theme.rs index 5c4072b..f29b273 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -1,6 +1,6 @@ use cursive::theme::Color::*; use cursive::theme::PaletteColor::*; -use cursive::theme::{BaseColor, BorderStyle, Palette, Theme}; +use cursive::theme::{BaseColor, BorderStyle, ColorStyle, Palette, Theme}; pub fn pallete_gen() -> Palette { let mut p = Palette::default(); @@ -8,7 +8,7 @@ pub fn pallete_gen() -> Palette { p[Shadow] = Light(BaseColor::Black); p[View] = Dark(BaseColor::Black); p[Primary] = Dark(BaseColor::White); - p[Secondary] = Light(BaseColor::Black); + p[Secondary] = Dark(BaseColor::Black); p[Tertiary] = Dark(BaseColor::Green); p[TitlePrimary] = Light(BaseColor::White); p[Highlight] = Dark(BaseColor::Red); @@ -20,7 +20,7 @@ pub fn pallete_gen() -> Palette { pub fn theme_gen() -> Theme { let mut t = Theme::default(); t.shadow = false; - t.borders = BorderStyle::Simple; + t.borders = BorderStyle::None; t.palette = pallete_gen(); return t; } -- cgit v1.2.3