aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-06-30 05:41:31 +0100
committerAkshay <[email protected]>2020-06-30 05:41:31 +0100
commit501e44c193cc6dd1299956f1ab85465a444b84a2 (patch)
tree4bbdc64c7909838ff400ca4d41161d2aa8f6953e /src
parent0944aebc4eed8526b5fe2e5f75b28ed21dc57b57 (diff)
basic command mode and parsing
Diffstat (limited to 'src')
-rw-r--r--src/command.rs49
1 files changed, 49 insertions, 0 deletions
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 @@
1use cursive::view::Resizable;
2use cursive::views::{Dialog, EditView};
3use cursive::Cursive;
4
5use crate::app::App;
6
7pub fn open_command_window(s: &mut Cursive) {
8 let command_window = Dialog::around(EditView::new().on_submit(call_on_app).fixed_width(40));
9 s.add_layer(command_window);
10}
11
12fn call_on_app(s: &mut Cursive, input: &str) {
13 s.call_on_name("Main", |view: &mut App| {
14 view.parse_command(input);
15 });
16 s.pop_layer();
17}
18
19pub enum Command {
20 Add(String, String, Option<u32>),
21 Delete,
22 Blank,
23}
24
25impl Command {
26 pub fn from_string<P: AsRef<str>>(input: P) -> Self {
27 let mut strings: Vec<&str> = input.as_ref().trim().split(' ').collect();
28 if strings.is_empty() {
29 return Command::Blank;
30 }
31
32 let first = strings.first().unwrap().to_string();
33 let mut args: Vec<String> = strings.iter_mut().skip(1).map(|s| s.to_string()).collect();
34 match first.as_ref() {
35 "add" | "a" => {
36 if args.len() < 2 {
37 return Command::Blank;
38 }
39 let goal = args.get(2).map(|g| g.parse::<u32>().ok()).flatten();
40 return Command::Add(
41 args.get_mut(0).unwrap().to_string(),
42 args.get_mut(1).unwrap().to_string(),
43 goal,
44 );
45 }
46 _ => return Command::Blank,
47 }
48 }
49}