aboutsummaryrefslogtreecommitdiff
path: root/src/command.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/command.rs')
-rw-r--r--src/command.rs90
1 files changed, 50 insertions, 40 deletions
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 @@
1use std::fmt; 1use std::fmt;
2 2
3use cursive::theme::{BaseColor, Color, ColorStyle};
3use cursive::view::Resizable; 4use cursive::view::Resizable;
4use cursive::views::{Dialog, EditView}; 5use cursive::views::{EditView, LinearLayout, TextView};
5use cursive::Cursive; 6use cursive::Cursive;
6 7
7use crate::app::App; 8use crate::{app::App, CONFIGURATION};
8 9
9pub fn open_command_window(s: &mut Cursive) { 10pub fn open_command_window(s: &mut Cursive) {
10 let command_window = Dialog::around(EditView::new().on_submit(call_on_app).fixed_width(40)); 11 let command_window = EditView::new()
11 s.add_layer(command_window); 12 .filler(" ")
13 .on_submit(call_on_app)
14 .style(ColorStyle::new(
15 Color::Dark(BaseColor::Black),
16 Color::Dark(BaseColor::White),
17 ))
18 .fixed_width(CONFIGURATION.view_width * CONFIGURATION.grid_width);
19 s.call_on_name("Frame", |view: &mut LinearLayout| {
20 let mut commandline = LinearLayout::horizontal()
21 .child(TextView::new(":"))
22 .child(command_window);
23 commandline.set_focus_index(1);
24 view.add_child(commandline);
25 view.set_focus_index(1);
26 });
12} 27}
13 28
14fn call_on_app(s: &mut Cursive, input: &str) { 29fn call_on_app(s: &mut Cursive, input: &str) {
30 // things to do after recieving the command
31 // 1. parse the command
32 // 2. clean existing command messages
33 // 3. remove the command window
34 // 4. handle quit command
15 s.call_on_name("Main", |view: &mut App| { 35 s.call_on_name("Main", |view: &mut App| {
16 let cmd = Command::from_string(input); 36 let cmd = Command::from_string(input);
37 view.clear_message();
17 view.parse_command(cmd); 38 view.parse_command(cmd);
18 }); 39 });
40 s.call_on_name("Frame", |view: &mut LinearLayout| {
41 view.set_focus_index(0);
42 view.remove_child(view.get_focus_index());
43 });
19 44
20 // special command that requires access to 45 // special command that requires access to
21 // our main cursive object, has to be parsed again 46 // our main cursive object, has to be parsed again
@@ -24,8 +49,6 @@ fn call_on_app(s: &mut Cursive, input: &str) {
24 if let Ok(Command::Quit) = Command::from_string(input) { 49 if let Ok(Command::Quit) = Command::from_string(input) {
25 s.quit(); 50 s.quit();
26 } 51 }
27
28 s.pop_layer();
29} 52}
30 53
31#[derive(PartialEq)] 54#[derive(PartialEq)]
@@ -72,41 +95,27 @@ impl Command {
72 95
73 let first = strings.first().unwrap().to_string(); 96 let first = strings.first().unwrap().to_string();
74 let mut args: Vec<String> = strings.iter_mut().skip(1).map(|s| s.to_string()).collect(); 97 let mut args: Vec<String> = strings.iter_mut().skip(1).map(|s| s.to_string()).collect();
75 match first.as_ref() { 98 let mut _add = |auto: bool, first: String| {
76 "add" | "a" => { 99 if args.is_empty() {
77 if args.is_empty() { 100 return Err(CommandLineError::NotEnoughArgs(first, 1));
78 return Err(CommandLineError::NotEnoughArgs(first, 1));
79 }
80 let goal = args
81 .get(1)
82 .map(|x| {
83 x.parse::<u32>()
84 .map_err(|_| CommandLineError::InvalidArg(1))
85 })
86 .transpose()?;
87 return Ok(Command::Add(
88 args.get_mut(0).unwrap().to_string(),
89 goal,
90 false,
91 ));
92 }
93 "add-auto" | "aa" => {
94 if args.is_empty() {
95 return Err(CommandLineError::NotEnoughArgs(first, 1));
96 }
97 let goal = args
98 .get(1)
99 .map(|x| {
100 x.parse::<u32>()
101 .map_err(|_| CommandLineError::InvalidArg(1))
102 })
103 .transpose()?;
104 return Ok(Command::Add(
105 args.get_mut(0).unwrap().to_string(),
106 goal,
107 true,
108 ));
109 } 101 }
102 let goal = args
103 .get(1)
104 .map(|x| {
105 x.parse::<u32>()
106 .map_err(|_| CommandLineError::InvalidArg(2))
107 })
108 .transpose()?;
109 return Ok(Command::Add(
110 args.get_mut(0).unwrap().to_string(),
111 goal,
112 auto,
113 ));
114 };
115
116 match first.as_ref() {
117 "add" | "a" => _add(false, first),
118 "add-auto" | "aa" => _add(true, first),
110 "delete" | "d" => { 119 "delete" | "d" => {
111 if args.is_empty() { 120 if args.is_empty() {
112 return Err(CommandLineError::NotEnoughArgs(first, 1)); 121 return Err(CommandLineError::NotEnoughArgs(first, 1));
@@ -128,6 +137,7 @@ impl Command {
128 "mprev" | "month-prev" => return Ok(Command::MonthPrev), 137 "mprev" | "month-prev" => return Ok(Command::MonthPrev),
129 "mnext" | "month-next" => return Ok(Command::MonthNext), 138 "mnext" | "month-next" => return Ok(Command::MonthNext),
130 "q" | "quit" => return Ok(Command::Quit), 139 "q" | "quit" => return Ok(Command::Quit),
140 "" => return Ok(Command::Blank),
131 s => return Err(CommandLineError::InvalidCommand(s.into())), 141 s => return Err(CommandLineError::InvalidCommand(s.into())),
132 } 142 }
133 } 143 }