aboutsummaryrefslogtreecommitdiff
path: root/src/command.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-23 17:53:59 +0100
committerAkshay <[email protected]>2020-07-23 17:53:59 +0100
commit91cb3ebf5b3af666b13f4821ff25f219fdccd7b9 (patch)
treec91d6cffd4d215b325e81bedfdcfdfdcea76a537 /src/command.rs
parent02bba52346d948e90fbc1b351486f0f1307c243d (diff)
parentd1d1aeb3d5aaa75f262467c5e683e76ce7a844ab (diff)
Merge branch 'feature/windows' of https://github.com/filalex77/dijo into feature/windows
Diffstat (limited to 'src/command.rs')
-rw-r--r--src/command.rs87
1 files changed, 75 insertions, 12 deletions
diff --git a/src/command.rs b/src/command.rs
index f856b00..38d48e9 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -1,21 +1,75 @@
1use std::fmt; 1use std::fmt;
2 2
3use cursive::event::{Event, EventResult, Key};
3use cursive::theme::{BaseColor, Color, ColorStyle}; 4use cursive::theme::{BaseColor, Color, ColorStyle};
4use cursive::view::Resizable; 5use cursive::view::Resizable;
5use cursive::views::{EditView, LinearLayout, TextView}; 6use cursive::views::{EditView, LinearLayout, OnEventView, TextView};
6use cursive::Cursive; 7use cursive::Cursive;
7 8
8use crate::{app::App, CONFIGURATION}; 9use crate::{app::App, CONFIGURATION};
9 10
11static COMMANDS: &'static [&'static str] = &[
12 "add",
13 "add-auto",
14 "delete",
15 "track-up",
16 "track-down",
17 "month-prev",
18 "month-next",
19 "quit",
20 "write",
21 "help",
22];
23
24fn get_command_completion(prefix: &str) -> Option<String> {
25 let first_match = COMMANDS.iter().filter(|&x| x.starts_with(prefix)).next();
26 return first_match.map(|&x| x.into());
27}
28
29fn get_habit_completion(prefix: &str, habit_names: &[String]) -> Option<String> {
30 let first_match = habit_names.iter().filter(|&x| x.starts_with(prefix)).next();
31 eprintln!("{:?}| {:?}", prefix, first_match);
32 return first_match.map(|x| x.into());
33}
34
10pub fn open_command_window(s: &mut Cursive) { 35pub fn open_command_window(s: &mut Cursive) {
11 let command_window = EditView::new() 36 let habit_list: Vec<String> = s
12 .filler(" ") 37 .call_on_name("Main", |view: &mut App| {
13 .on_submit(call_on_app) 38 return view.list_habits();
14 .style(ColorStyle::new( 39 })
15 Color::Dark(BaseColor::Black), 40 .unwrap();
16 Color::Dark(BaseColor::White), 41 let style = ColorStyle::new(Color::Dark(BaseColor::Black), Color::Dark(BaseColor::White));
17 )) 42 let command_window = OnEventView::new(
18 .fixed_width(CONFIGURATION.view_width * CONFIGURATION.grid_width); 43 EditView::new()
44 .filler(" ")
45 .on_submit(call_on_app)
46 .style(style),
47 )
48 .on_event_inner(
49 Event::Key(Key::Tab),
50 move |view: &mut EditView, _: &Event| {
51 let contents = view.get_content();
52 if !contents.contains(" ") {
53 let completion = get_command_completion(&*contents);
54 if let Some(c) = completion {
55 let cb = view.set_content(c);
56 return Some(EventResult::Consumed(Some(cb)));
57 };
58 return None;
59 } else {
60 let word = contents.split(' ').last().unwrap();
61 let completion = get_habit_completion(word, &habit_list);
62 eprintln!("{:?} | {:?}", completion, contents);
63 if let Some(c) = completion {
64 let cb =
65 view.set_content(format!("{}", contents) + c.strip_prefix(word).unwrap());
66 return Some(EventResult::Consumed(Some(cb)));
67 };
68 return None;
69 }
70 },
71 )
72 .fixed_width(CONFIGURATION.view_width * CONFIGURATION.grid_width);
19 s.call_on_name("Frame", |view: &mut LinearLayout| { 73 s.call_on_name("Frame", |view: &mut LinearLayout| {
20 let mut commandline = LinearLayout::horizontal() 74 let mut commandline = LinearLayout::horizontal()
21 .child(TextView::new(":")) 75 .child(TextView::new(":"))
@@ -59,15 +113,17 @@ pub enum Command {
59 Delete(String), 113 Delete(String),
60 TrackUp(String), 114 TrackUp(String),
61 TrackDown(String), 115 TrackDown(String),
116 Help(Option<String>),
117 Write,
62 Quit, 118 Quit,
63 Blank, 119 Blank,
64} 120}
65 121
66#[derive(Debug)] 122#[derive(Debug)]
67pub enum CommandLineError { 123pub enum CommandLineError {
68 InvalidCommand(String), 124 InvalidCommand(String), // command name
69 InvalidArg(u32), // position 125 InvalidArg(u32), // position
70 NotEnoughArgs(String, u32), 126 NotEnoughArgs(String, u32), // command name, required no. of args
71} 127}
72 128
73impl std::error::Error for CommandLineError {} 129impl std::error::Error for CommandLineError {}
@@ -134,9 +190,16 @@ impl Command {
134 } 190 }
135 return Ok(Command::TrackDown(args[0].to_string())); 191 return Ok(Command::TrackDown(args[0].to_string()));
136 } 192 }
193 "h" | "?" | "help" => {
194 if args.is_empty() {
195 return Ok(Command::Help(None));
196 }
197 return Ok(Command::Help(Some(args[0].to_string())));
198 }
137 "mprev" | "month-prev" => return Ok(Command::MonthPrev), 199 "mprev" | "month-prev" => return Ok(Command::MonthPrev),
138 "mnext" | "month-next" => return Ok(Command::MonthNext), 200 "mnext" | "month-next" => return Ok(Command::MonthNext),
139 "q" | "quit" => return Ok(Command::Quit), 201 "q" | "quit" => return Ok(Command::Quit),
202 "w" | "write" => return Ok(Command::Write),
140 "" => return Ok(Command::Blank), 203 "" => return Ok(Command::Blank),
141 s => return Err(CommandLineError::InvalidCommand(s.into())), 204 s => return Err(CommandLineError::InvalidCommand(s.into())),
142 } 205 }