From 501e44c193cc6dd1299956f1ab85465a444b84a2 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 30 Jun 2020 10:11:31 +0530 Subject: basic command mode and parsing --- src/command.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/command.rs diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..5391266 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,49 @@ +use cursive::view::Resizable; +use cursive::views::{Dialog, EditView}; +use cursive::Cursive; + +use crate::app::App; + +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); +} + +fn call_on_app(s: &mut Cursive, input: &str) { + s.call_on_name("Main", |view: &mut App| { + view.parse_command(input); + }); + s.pop_layer(); +} + +pub enum Command { + Add(String, String, Option), + Delete, + Blank, +} + +impl Command { + pub fn from_string>(input: P) -> Self { + let mut strings: Vec<&str> = input.as_ref().trim().split(' ').collect(); + if strings.is_empty() { + return Command::Blank; + } + + 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.len() < 2 { + return Command::Blank; + } + let goal = args.get(2).map(|g| g.parse::().ok()).flatten(); + return Command::Add( + args.get_mut(0).unwrap().to_string(), + args.get_mut(1).unwrap().to_string(), + goal, + ); + } + _ => return Command::Blank, + } + } +} -- cgit v1.2.3