diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/app/cursor.rs | 22 | ||||
-rw-r--r-- | src/app/impl_self.rs | 42 | ||||
-rw-r--r-- | src/app/impl_view.rs | 21 | ||||
-rw-r--r-- | src/app/mod.rs | 1 | ||||
-rw-r--r-- | src/habit/bit.rs | 37 | ||||
-rw-r--r-- | src/habit/count.rs | 37 | ||||
-rw-r--r-- | src/habit/mod.rs | 27 | ||||
-rw-r--r-- | src/habit/traits.rs | 61 | ||||
-rw-r--r-- | src/keybinds.rs | 146 | ||||
-rw-r--r-- | src/theme.rs | 2 | ||||
-rw-r--r-- | src/utils.rs | 4 | ||||
-rw-r--r-- | src/views.rs | 9 |
12 files changed, 266 insertions, 143 deletions
diff --git a/src/app/cursor.rs b/src/app/cursor.rs index ed6bd65..f76d591 100644 --- a/src/app/cursor.rs +++ b/src/app/cursor.rs | |||
@@ -16,7 +16,7 @@ impl Cursor { | |||
16 | 0: Local::now().naive_local().date(), | 16 | 0: Local::now().naive_local().date(), |
17 | } | 17 | } |
18 | } | 18 | } |
19 | pub fn do_move(&mut self, d: Absolute) { | 19 | pub fn small_seek(&mut self, d: Absolute) { |
20 | let today = Local::now().naive_local().date(); | 20 | let today = Local::now().naive_local().date(); |
21 | let cursor = self.0; | 21 | let cursor = self.0; |
22 | match d { | 22 | match d { |
@@ -48,4 +48,24 @@ impl Cursor { | |||
48 | Absolute::None => {} | 48 | Absolute::None => {} |
49 | } | 49 | } |
50 | } | 50 | } |
51 | fn long_seek(&mut self, offset: Duration) { | ||
52 | let cursor = self.0; | ||
53 | let today = Local::now().naive_local().date(); | ||
54 | let next = cursor.checked_add_signed(offset).unwrap_or(cursor); | ||
55 | |||
56 | if next <= today { | ||
57 | self.0 = next; | ||
58 | } else { | ||
59 | self.0 = today; | ||
60 | } | ||
61 | } | ||
62 | pub fn month_forward(&mut self) { | ||
63 | self.long_seek(Duration::weeks(4)); | ||
64 | } | ||
65 | pub fn month_backward(&mut self) { | ||
66 | self.long_seek(Duration::weeks(-4)); | ||
67 | } | ||
68 | pub fn reset(&mut self) { | ||
69 | self.0 = Local::now().naive_local().date(); | ||
70 | } | ||
51 | } | 71 | } |
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index abf5209..d5f93ff 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs | |||
@@ -6,7 +6,7 @@ use std::path::PathBuf; | |||
6 | use std::sync::mpsc::channel; | 6 | use std::sync::mpsc::channel; |
7 | use std::time::Duration; | 7 | use std::time::Duration; |
8 | 8 | ||
9 | use chrono::Local; | 9 | use chrono::{Local, NaiveDate}; |
10 | use cursive::direction::Absolute; | 10 | use cursive::direction::Absolute; |
11 | use cursive::Vec2; | 11 | use cursive::Vec2; |
12 | use notify::{watcher, RecursiveMode, Watcher}; | 12 | use notify::{watcher, RecursiveMode, Watcher}; |
@@ -27,7 +27,6 @@ impl App { | |||
27 | focus: 0, | 27 | focus: 0, |
28 | _file_watcher: watcher, | 28 | _file_watcher: watcher, |
29 | file_event_recv: rx, | 29 | file_event_recv: rx, |
30 | view_month_offset: 0, | ||
31 | cursor: Cursor::new(), | 30 | cursor: Cursor::new(), |
32 | message: Message::startup(), | 31 | message: Message::startup(), |
33 | }; | 32 | }; |
@@ -54,42 +53,42 @@ impl App { | |||
54 | if self.habits.is_empty() { | 53 | if self.habits.is_empty() { |
55 | return ViewMode::Day; | 54 | return ViewMode::Day; |
56 | } | 55 | } |
57 | return self.habits[self.focus].view_mode(); | 56 | return self.habits[self.focus].inner_data_ref().view_mode(); |
58 | } | 57 | } |
59 | 58 | ||
60 | pub fn set_mode(&mut self, mode: ViewMode) { | 59 | pub fn set_mode(&mut self, mode: ViewMode) { |
61 | if !self.habits.is_empty() { | 60 | if !self.habits.is_empty() { |
62 | self.habits[self.focus].set_view_mode(mode); | 61 | self.habits[self.focus] |
62 | .inner_data_mut_ref() | ||
63 | .set_view_mode(mode); | ||
63 | } | 64 | } |
64 | } | 65 | } |
65 | 66 | ||
66 | pub fn set_view_month_offset(&mut self, offset: u32) { | 67 | pub fn sift_backward(&mut self) { |
67 | self.view_month_offset = offset; | 68 | self.cursor.month_backward(); |
68 | for v in self.habits.iter_mut() { | 69 | for v in self.habits.iter_mut() { |
69 | v.set_view_month_offset(offset); | 70 | v.inner_data_mut_ref().cursor.month_backward(); |
70 | } | 71 | } |
71 | } | 72 | } |
72 | 73 | ||
73 | pub fn sift_backward(&mut self) { | 74 | pub fn sift_forward(&mut self) { |
74 | self.view_month_offset += 1; | 75 | self.cursor.month_forward(); |
75 | for v in self.habits.iter_mut() { | 76 | for v in self.habits.iter_mut() { |
76 | v.set_view_month_offset(self.view_month_offset); | 77 | v.inner_data_mut_ref().cursor.month_forward(); |
77 | } | 78 | } |
78 | } | 79 | } |
79 | 80 | ||
80 | pub fn sift_forward(&mut self) { | 81 | pub fn reset_cursor(&mut self) { |
81 | if self.view_month_offset > 0 { | 82 | self.cursor.reset(); |
82 | self.view_month_offset -= 1; | 83 | for v in self.habits.iter_mut() { |
83 | for v in self.habits.iter_mut() { | 84 | v.inner_data_mut_ref().cursor.reset(); |
84 | v.set_view_month_offset(self.view_month_offset); | ||
85 | } | ||
86 | } | 85 | } |
87 | } | 86 | } |
88 | 87 | ||
89 | pub fn move_cursor(&mut self, d: Absolute) { | 88 | pub fn move_cursor(&mut self, d: Absolute) { |
90 | self.cursor.do_move(d); | 89 | self.cursor.small_seek(d); |
91 | for v in self.habits.iter_mut() { | 90 | for v in self.habits.iter_mut() { |
92 | v.move_cursor(d); | 91 | v.inner_data_mut_ref().move_cursor(d); |
93 | } | 92 | } |
94 | } | 93 | } |
95 | 94 | ||
@@ -133,11 +132,12 @@ impl App { | |||
133 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); | 132 | let total = self.habits.iter().map(|h| h.goal()).sum::<u32>(); |
134 | let completed = total - remaining; | 133 | let completed = total - remaining; |
135 | 134 | ||
136 | let timestamp = if self.view_month_offset == 0 { | 135 | let timestamp = if self.cursor.0 == today { |
137 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) | 136 | format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) |
138 | } else { | 137 | } else { |
139 | let months = self.view_month_offset; | 138 | let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days(); |
140 | format!("{}", format!("{} month(s) ago", months),) | 139 | let plural = if since == 1 { "" } else { "s" }; |
140 | format!("{} ({} day{} ago)", self.cursor.0, since, plural) | ||
141 | }; | 141 | }; |
142 | 142 | ||
143 | StatusLine { | 143 | StatusLine { |
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs index 0ec47f1..c369d8f 100644 --- a/src/app/impl_view.rs +++ b/src/app/impl_view.rs | |||
@@ -96,19 +96,19 @@ impl View for App { | |||
96 | return EventResult::Consumed(None); | 96 | return EventResult::Consumed(None); |
97 | } | 97 | } |
98 | 98 | ||
99 | Event::Char('w') => { | 99 | Event::Char('K') => { |
100 | self.move_cursor(Absolute::Up); | 100 | self.move_cursor(Absolute::Up); |
101 | return EventResult::Consumed(None); | 101 | return EventResult::Consumed(None); |
102 | } | 102 | } |
103 | Event::Char('a') => { | 103 | Event::Char('H') => { |
104 | self.move_cursor(Absolute::Left); | 104 | self.move_cursor(Absolute::Left); |
105 | return EventResult::Consumed(None); | 105 | return EventResult::Consumed(None); |
106 | } | 106 | } |
107 | Event::Char('s') => { | 107 | Event::Char('J') => { |
108 | self.move_cursor(Absolute::Down); | 108 | self.move_cursor(Absolute::Down); |
109 | return EventResult::Consumed(None); | 109 | return EventResult::Consumed(None); |
110 | } | 110 | } |
111 | Event::Char('d') => { | 111 | Event::Char('L') => { |
112 | self.move_cursor(Absolute::Right); | 112 | self.move_cursor(Absolute::Right); |
113 | return EventResult::Consumed(None); | 113 | return EventResult::Consumed(None); |
114 | } | 114 | } |
@@ -117,7 +117,7 @@ impl View for App { | |||
117 | if self.habits.is_empty() { | 117 | if self.habits.is_empty() { |
118 | return EventResult::Consumed(None); | 118 | return EventResult::Consumed(None); |
119 | } | 119 | } |
120 | if self.habits[self.focus].view_mode() == ViewMode::Week { | 120 | if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week { |
121 | self.set_mode(ViewMode::Day) | 121 | self.set_mode(ViewMode::Day) |
122 | } else { | 122 | } else { |
123 | self.set_mode(ViewMode::Week) | 123 | self.set_mode(ViewMode::Week) |
@@ -126,14 +126,15 @@ impl View for App { | |||
126 | } | 126 | } |
127 | Event::Char('V') => { | 127 | Event::Char('V') => { |
128 | for habit in self.habits.iter_mut() { | 128 | for habit in self.habits.iter_mut() { |
129 | habit.set_view_mode(ViewMode::Week); | 129 | habit.inner_data_mut_ref().set_view_mode(ViewMode::Week); |
130 | } | 130 | } |
131 | return EventResult::Consumed(None); | 131 | return EventResult::Consumed(None); |
132 | } | 132 | } |
133 | Event::Key(Key::Esc) => { | 133 | Event::Key(Key::Esc) => { |
134 | for habit in self.habits.iter_mut() { | 134 | for habit in self.habits.iter_mut() { |
135 | habit.set_view_mode(ViewMode::Day); | 135 | habit.inner_data_mut_ref().set_view_mode(ViewMode::Day); |
136 | } | 136 | } |
137 | self.reset_cursor(); | ||
137 | return EventResult::Consumed(None); | 138 | return EventResult::Consumed(None); |
138 | } | 139 | } |
139 | 140 | ||
@@ -149,7 +150,7 @@ impl View for App { | |||
149 | return EventResult::Consumed(None); | 150 | return EventResult::Consumed(None); |
150 | } | 151 | } |
151 | Event::Char('}') => { | 152 | Event::Char('}') => { |
152 | self.set_view_month_offset(0); | 153 | self.reset_cursor(); |
153 | return EventResult::Consumed(None); | 154 | return EventResult::Consumed(None); |
154 | } | 155 | } |
155 | Event::CtrlChar('l') => { | 156 | Event::CtrlChar('l') => { |
@@ -159,14 +160,12 @@ impl View for App { | |||
159 | } | 160 | } |
160 | 161 | ||
161 | /* Every keybind that is not caught by App trickles | 162 | /* Every keybind that is not caught by App trickles |
162 | * down to the focused habit. We sift back to today | 163 | * down to the focused habit. |
163 | * before performing any action, "refocusing" the cursor | ||
164 | * */ | 164 | * */ |
165 | _ => { | 165 | _ => { |
166 | if self.habits.is_empty() { | 166 | if self.habits.is_empty() { |
167 | return EventResult::Ignored; | 167 | return EventResult::Ignored; |
168 | } | 168 | } |
169 | self.set_view_month_offset(0); | ||
170 | self.habits[self.focus].on_event(e) | 169 | self.habits[self.focus].on_event(e) |
171 | } | 170 | } |
172 | } | 171 | } |
diff --git a/src/app/mod.rs b/src/app/mod.rs index 9432f0d..726a656 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs | |||
@@ -21,7 +21,6 @@ pub struct App { | |||
21 | _file_watcher: RecommendedWatcher, | 21 | _file_watcher: RecommendedWatcher, |
22 | file_event_recv: Receiver<DebouncedEvent>, | 22 | file_event_recv: Receiver<DebouncedEvent>, |
23 | focus: usize, | 23 | focus: usize, |
24 | view_month_offset: u32, | ||
25 | cursor: Cursor, | 24 | cursor: Cursor, |
26 | message: Message, | 25 | message: Message, |
27 | } | 26 | } |
diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 7fe6fd9..da64ece 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | use std::default::Default; | ||
2 | 3 | ||
3 | use chrono::NaiveDate; | 4 | use chrono::NaiveDate; |
4 | use cursive::direction::Absolute; | ||
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | 6 | ||
7 | use crate::app::Cursor; | ||
8 | use crate::habit::prelude::default_auto; | 7 | use crate::habit::prelude::default_auto; |
9 | use crate::habit::traits::Habit; | 8 | use crate::habit::traits::Habit; |
10 | use crate::habit::{TrackEvent, ViewMode}; | 9 | use crate::habit::{InnerData, TrackEvent}; |
11 | use crate::CONFIGURATION; | 10 | use crate::CONFIGURATION; |
12 | 11 | ||
13 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] | 12 | #[derive(Copy, Clone, Debug, Serialize, Deserialize)] |
@@ -44,13 +43,7 @@ pub struct Bit { | |||
44 | auto: bool, | 43 | auto: bool, |
45 | 44 | ||
46 | #[serde(skip)] | 45 | #[serde(skip)] |
47 | view_month_offset: u32, | 46 | inner_data: InnerData, |
48 | |||
49 | #[serde(skip)] | ||
50 | cursor: Cursor, | ||
51 | |||
52 | #[serde(skip)] | ||
53 | view_mode: ViewMode, | ||
54 | } | 47 | } |
55 | 48 | ||
56 | impl Bit { | 49 | impl Bit { |
@@ -60,9 +53,7 @@ impl Bit { | |||
60 | stats: HashMap::new(), | 53 | stats: HashMap::new(), |
61 | goal: CustomBool(true), | 54 | goal: CustomBool(true), |
62 | auto, | 55 | auto, |
63 | view_month_offset: 0, | 56 | inner_data: Default::default(), |
64 | cursor: Cursor::new(), | ||
65 | view_mode: ViewMode::Day, | ||
66 | }; | 57 | }; |
67 | } | 58 | } |
68 | } | 59 | } |
@@ -124,23 +115,11 @@ impl Habit for Bit { | |||
124 | } | 115 | } |
125 | } | 116 | } |
126 | } | 117 | } |
127 | fn set_view_month_offset(&mut self, offset: u32) { | 118 | fn inner_data_ref(&self) -> &InnerData { |
128 | self.view_month_offset = offset; | 119 | &self.inner_data |
129 | } | ||
130 | fn view_month_offset(&self) -> u32 { | ||
131 | self.view_month_offset | ||
132 | } | ||
133 | fn move_cursor(&mut self, d: Absolute) { | ||
134 | self.cursor.do_move(d); | ||
135 | } | ||
136 | fn cursor(&self) -> Cursor { | ||
137 | self.cursor | ||
138 | } | ||
139 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
140 | self.view_mode = mode; | ||
141 | } | 120 | } |
142 | fn view_mode(&self) -> ViewMode { | 121 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
143 | self.view_mode | 122 | &mut self.inner_data |
144 | } | 123 | } |
145 | fn is_auto(&self) -> bool { | 124 | fn is_auto(&self) -> bool { |
146 | self.auto | 125 | self.auto |
diff --git a/src/habit/count.rs b/src/habit/count.rs index b14354c..09fd399 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs | |||
@@ -1,13 +1,12 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | use std::default::Default; | ||
2 | 3 | ||
3 | use chrono::NaiveDate; | 4 | use chrono::NaiveDate; |
4 | use cursive::direction::Absolute; | ||
5 | use serde::{Deserialize, Serialize}; | 5 | use serde::{Deserialize, Serialize}; |
6 | 6 | ||
7 | use crate::app::Cursor; | ||
8 | use crate::habit::prelude::default_auto; | 7 | use crate::habit::prelude::default_auto; |
9 | use crate::habit::traits::Habit; | 8 | use crate::habit::traits::Habit; |
10 | use crate::habit::{TrackEvent, ViewMode}; | 9 | use crate::habit::{InnerData, TrackEvent}; |
11 | 10 | ||
12 | #[derive(Debug, Serialize, Deserialize)] | 11 | #[derive(Debug, Serialize, Deserialize)] |
13 | pub struct Count { | 12 | pub struct Count { |
@@ -19,13 +18,7 @@ pub struct Count { | |||
19 | auto: bool, | 18 | auto: bool, |
20 | 19 | ||
21 | #[serde(skip)] | 20 | #[serde(skip)] |
22 | view_month_offset: u32, | 21 | inner_data: InnerData, |
23 | |||
24 | #[serde(skip)] | ||
25 | cursor: Cursor, | ||
26 | |||
27 | #[serde(skip)] | ||
28 | view_mode: ViewMode, | ||
29 | } | 22 | } |
30 | 23 | ||
31 | impl Count { | 24 | impl Count { |
@@ -35,9 +28,7 @@ impl Count { | |||
35 | stats: HashMap::new(), | 28 | stats: HashMap::new(), |
36 | goal, | 29 | goal, |
37 | auto, | 30 | auto, |
38 | view_month_offset: 0, | 31 | inner_data: Default::default(), |
39 | cursor: Cursor::new(), | ||
40 | view_mode: ViewMode::Day, | ||
41 | }; | 32 | }; |
42 | } | 33 | } |
43 | } | 34 | } |
@@ -101,23 +92,11 @@ impl Habit for Count { | |||
101 | }; | 92 | }; |
102 | } | 93 | } |
103 | } | 94 | } |
104 | fn set_view_month_offset(&mut self, offset: u32) { | 95 | fn inner_data_ref(&self) -> &InnerData { |
105 | self.view_month_offset = offset; | 96 | &self.inner_data |
106 | } | ||
107 | fn view_month_offset(&self) -> u32 { | ||
108 | self.view_month_offset | ||
109 | } | ||
110 | fn move_cursor(&mut self, d: Absolute) { | ||
111 | self.cursor.do_move(d); | ||
112 | } | ||
113 | fn cursor(&self) -> Cursor { | ||
114 | self.cursor | ||
115 | } | ||
116 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
117 | self.view_mode = mode; | ||
118 | } | 97 | } |
119 | fn view_mode(&self) -> ViewMode { | 98 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
120 | self.view_mode | 99 | &mut self.inner_data |
121 | } | 100 | } |
122 | fn is_auto(&self) -> bool { | 101 | fn is_auto(&self) -> bool { |
123 | self.auto | 102 | self.auto |
diff --git a/src/habit/mod.rs b/src/habit/mod.rs index 75e734a..d51abe5 100644 --- a/src/habit/mod.rs +++ b/src/habit/mod.rs | |||
@@ -1,3 +1,5 @@ | |||
1 | use std::default::Default; | ||
2 | |||
1 | mod traits; | 3 | mod traits; |
2 | pub use traits::{Habit, HabitWrapper}; | 4 | pub use traits::{Habit, HabitWrapper}; |
3 | 5 | ||
@@ -9,3 +11,28 @@ pub use bit::Bit; | |||
9 | 11 | ||
10 | mod prelude; | 12 | mod prelude; |
11 | pub use prelude::{TrackEvent, ViewMode}; | 13 | pub use prelude::{TrackEvent, ViewMode}; |
14 | |||
15 | use crate::app::Cursor; | ||
16 | |||
17 | use cursive::direction::Absolute; | ||
18 | |||
19 | #[derive(Debug, Default)] | ||
20 | pub struct InnerData { | ||
21 | pub cursor: Cursor, | ||
22 | pub view_mode: ViewMode, | ||
23 | } | ||
24 | |||
25 | impl InnerData { | ||
26 | pub fn move_cursor(&mut self, d: Absolute) { | ||
27 | self.cursor.small_seek(d); | ||
28 | } | ||
29 | pub fn cursor(&self) -> Cursor { | ||
30 | self.cursor | ||
31 | } | ||
32 | pub fn set_view_mode(&mut self, mode: ViewMode) { | ||
33 | self.view_mode = mode; | ||
34 | } | ||
35 | pub fn view_mode(&self) -> ViewMode { | ||
36 | self.view_mode | ||
37 | } | ||
38 | } | ||
diff --git a/src/habit/traits.rs b/src/habit/traits.rs index 289fd95..24d941d 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs | |||
@@ -1,58 +1,45 @@ | |||
1 | use chrono::NaiveDate; | 1 | use chrono::NaiveDate; |
2 | use cursive::direction::{Absolute, Direction}; | 2 | use cursive::direction::Direction; |
3 | use cursive::event::{Event, EventResult}; | 3 | use cursive::event::{Event, EventResult}; |
4 | use cursive::{Printer, Vec2}; | 4 | use cursive::{Printer, Vec2}; |
5 | 5 | ||
6 | use typetag; | 6 | use typetag; |
7 | 7 | ||
8 | use crate::app::Cursor; | 8 | use crate::habit::{Bit, Count, InnerData, TrackEvent}; |
9 | use crate::habit::{Bit, Count, TrackEvent, ViewMode}; | ||
10 | use crate::views::ShadowView; | 9 | use crate::views::ShadowView; |
11 | 10 | ||
12 | pub trait Habit { | 11 | pub trait Habit { |
13 | type HabitType; | 12 | type HabitType; |
14 | 13 | ||
15 | fn set_name(&mut self, name: impl AsRef<str>); | ||
16 | fn set_goal(&mut self, goal: Self::HabitType); | ||
17 | fn name(&self) -> String; | ||
18 | fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; | 14 | fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; |
15 | fn goal(&self) -> u32; | ||
19 | fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); | 16 | fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); |
17 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | ||
18 | fn name(&self) -> String; | ||
20 | fn reached_goal(&self, date: NaiveDate) -> bool; | 19 | fn reached_goal(&self, date: NaiveDate) -> bool; |
21 | fn remaining(&self, date: NaiveDate) -> u32; | 20 | fn remaining(&self, date: NaiveDate) -> u32; |
22 | fn goal(&self) -> u32; | 21 | fn set_goal(&mut self, goal: Self::HabitType); |
23 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | 22 | fn set_name(&mut self, name: impl AsRef<str>); |
24 | |||
25 | fn set_view_month_offset(&mut self, offset: u32); | ||
26 | fn view_month_offset(&self) -> u32; | ||
27 | |||
28 | fn move_cursor(&mut self, d: Absolute); | ||
29 | fn cursor(&self) -> Cursor; | ||
30 | 23 | ||
31 | fn set_view_mode(&mut self, mode: ViewMode); | 24 | fn inner_data_ref(&self) -> &InnerData; |
32 | fn view_mode(&self) -> ViewMode; | 25 | fn inner_data_mut_ref(&mut self) -> &mut InnerData; |
33 | 26 | ||
34 | fn is_auto(&self) -> bool; | 27 | fn is_auto(&self) -> bool; |
35 | } | 28 | } |
36 | 29 | ||
37 | #[typetag::serde(tag = "type")] | 30 | #[typetag::serde(tag = "type")] |
38 | pub trait HabitWrapper: erased_serde::Serialize { | 31 | pub trait HabitWrapper: erased_serde::Serialize { |
39 | fn remaining(&self, date: NaiveDate) -> u32; | 32 | fn draw(&self, printer: &Printer); |
40 | fn goal(&self) -> u32; | 33 | fn goal(&self) -> u32; |
41 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); | 34 | fn modify(&mut self, date: NaiveDate, event: TrackEvent); |
42 | fn draw(&self, printer: &Printer); | 35 | fn name(&self) -> String; |
43 | fn on_event(&mut self, event: Event) -> EventResult; | 36 | fn on_event(&mut self, event: Event) -> EventResult; |
37 | fn remaining(&self, date: NaiveDate) -> u32; | ||
44 | fn required_size(&mut self, _: Vec2) -> Vec2; | 38 | fn required_size(&mut self, _: Vec2) -> Vec2; |
45 | fn take_focus(&mut self, _: Direction) -> bool; | 39 | fn take_focus(&mut self, _: Direction) -> bool; |
46 | fn name(&self) -> String; | ||
47 | 40 | ||
48 | fn set_view_month_offset(&mut self, offset: u32); | 41 | fn inner_data_ref(&self) -> &InnerData; |
49 | fn view_month_offset(&self) -> u32; | 42 | fn inner_data_mut_ref(&mut self) -> &mut InnerData; |
50 | |||
51 | fn move_cursor(&mut self, d: Absolute); | ||
52 | fn cursor(&self) -> Cursor; | ||
53 | |||
54 | fn set_view_mode(&mut self, mode: ViewMode); | ||
55 | fn view_mode(&self) -> ViewMode; | ||
56 | 43 | ||
57 | fn is_auto(&self) -> bool; | 44 | fn is_auto(&self) -> bool; |
58 | } | 45 | } |
@@ -88,23 +75,11 @@ macro_rules! auto_habit_impl { | |||
88 | fn name(&self) -> String { | 75 | fn name(&self) -> String { |
89 | Habit::name(self) | 76 | Habit::name(self) |
90 | } | 77 | } |
91 | fn set_view_month_offset(&mut self, offset: u32) { | 78 | fn inner_data_ref(&self) -> &InnerData { |
92 | Habit::set_view_month_offset(self, offset) | 79 | Habit::inner_data_ref(self) |
93 | } | ||
94 | fn view_month_offset(&self) -> u32 { | ||
95 | Habit::view_month_offset(self) | ||
96 | } | ||
97 | fn move_cursor(&mut self, d: Absolute) { | ||
98 | Habit::move_cursor(self, d) | ||
99 | } | ||
100 | fn cursor(&self) -> Cursor { | ||
101 | Habit::cursor(self) | ||
102 | } | ||
103 | fn set_view_mode(&mut self, mode: ViewMode) { | ||
104 | Habit::set_view_mode(self, mode) | ||
105 | } | 80 | } |
106 | fn view_mode(&self) -> ViewMode { | 81 | fn inner_data_mut_ref(&mut self) -> &mut InnerData { |
107 | Habit::view_mode(self) | 82 | Habit::inner_data_mut_ref(self) |
108 | } | 83 | } |
109 | fn is_auto(&self) -> bool { | 84 | fn is_auto(&self) -> bool { |
110 | Habit::is_auto(self) | 85 | Habit::is_auto(self) |
diff --git a/src/keybinds.rs b/src/keybinds.rs new file mode 100644 index 0000000..1b9c234 --- /dev/null +++ b/src/keybinds.rs | |||
@@ -0,0 +1,146 @@ | |||
1 | use std::convert::From; | ||
2 | |||
3 | use cursive::event::Event as CursiveEvent; | ||
4 | use serde::ser; | ||
5 | use serde::{self, Deserialize, Serialize, Serializer}; | ||
6 | |||
7 | #[derive(Debug, PartialEq)] | ||
8 | struct Event(CursiveEvent); | ||
9 | |||
10 | macro_rules! event { | ||
11 | ($thing:expr) => { | ||
12 | Event { 0: $thing }; | ||
13 | }; | ||
14 | } | ||
15 | |||
16 | impl<T> From<T> for Event | ||
17 | where | ||
18 | T: AsRef<str>, | ||
19 | { | ||
20 | fn from(key: T) -> Self { | ||
21 | let key = key.as_ref(); | ||
22 | if key.len() == 1 { | ||
23 | // single key | ||
24 | return event!(CursiveEvent::Char(key.chars().nth(0).unwrap())); | ||
25 | } else if (key.starts_with("c-") || key.starts_with("C-")) && key.len() == 3 { | ||
26 | // ctrl-key | ||
27 | return event!(CursiveEvent::CtrlChar(key.chars().nth(2).unwrap())); | ||
28 | } else { | ||
29 | panic!( | ||
30 | r"Invalid keybind in configuration! | ||
31 | (I intend to handle this error gracefully in the near future)" | ||
32 | ); | ||
33 | } | ||
34 | } | ||
35 | } | ||
36 | |||
37 | enum Bind { | ||
38 | Char(char), | ||
39 | CtrlChar(char), | ||
40 | AltChar(char), | ||
41 | } | ||
42 | |||
43 | impl Serialize for Bind { | ||
44 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
45 | where | ||
46 | S: Serializer, | ||
47 | { | ||
48 | match self { | ||
49 | Bind::Char(c) => serializer.serialize_newtype_variant("bind", 0, "regular", &c), | ||
50 | Bind::CtrlChar(c) => serializer.serialize_newtype_variant("bind", 0, "ctrl", &c), | ||
51 | Bind::AltChar(c) => serializer.serialize_newtype_variant("bind", 0, "alt", &c), | ||
52 | } | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl Deserialize for Bind { | ||
57 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
58 | where | ||
59 | D: Deserializer<'de>, | ||
60 | { | ||
61 | eprintln!("hell = {:#?}", hell); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | impl From<Bind> for CursiveEvent { | ||
66 | fn from(key: Bind) -> Self { | ||
67 | match key { | ||
68 | Bind::Char(c) => CursiveEvent::Char(c), | ||
69 | Bind::CtrlChar(c) => CursiveEvent::Char(c), | ||
70 | Bind::AltChar(c) => CursiveEvent::AltChar(c), | ||
71 | } | ||
72 | } | ||
73 | } | ||
74 | |||
75 | #[derive(Serialize, Deserialize)] | ||
76 | pub struct KeyBinds { | ||
77 | grid: Movement, | ||
78 | cursor: Movement, | ||
79 | week_mode: Bind, | ||
80 | global_week_mode: Bind, | ||
81 | } | ||
82 | |||
83 | #[derive(Serialize, Deserialize)] | ||
84 | pub struct Movement { | ||
85 | up: Bind, | ||
86 | down: Bind, | ||
87 | left: Bind, | ||
88 | right: Bind, | ||
89 | } | ||
90 | |||
91 | impl Movement { | ||
92 | pub fn new(left: char, down: char, up: char, right: char) -> Self { | ||
93 | return Movement { | ||
94 | up: Bind::Char(up), | ||
95 | down: Bind::Char(down), | ||
96 | left: Bind::Char(left), | ||
97 | right: Bind::Char(right), | ||
98 | }; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | impl std::default::Default for KeyBinds { | ||
103 | fn default() -> Self { | ||
104 | let grid = Movement::new('h', 'j', 'k', 'l'); | ||
105 | let cursor = Movement::new('H', 'J', 'K', 'L'); | ||
106 | return KeyBinds { | ||
107 | grid, | ||
108 | cursor, | ||
109 | week_mode: Bind::Char('v'), | ||
110 | global_week_mode: Bind::Char('V'), | ||
111 | }; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | #[cfg(test)] | ||
116 | mod tests { | ||
117 | use super::*; | ||
118 | |||
119 | #[test] | ||
120 | fn normal_keybind() { | ||
121 | let bind = "X"; | ||
122 | let expected = CursiveEvent::Char('X'); | ||
123 | assert_eq!(Event::from(bind), event!(expected)); | ||
124 | } | ||
125 | |||
126 | #[test] | ||
127 | fn control_keybind() { | ||
128 | let bind = "C-x"; | ||
129 | let expected = CursiveEvent::CtrlChar('x'); | ||
130 | assert_eq!(Event::from(bind), event!(expected)); | ||
131 | } | ||
132 | |||
133 | #[test] | ||
134 | fn lower_case_control_keybind() { | ||
135 | let bind = "c-x"; | ||
136 | let expected = CursiveEvent::CtrlChar('x'); | ||
137 | assert_eq!(Event::from(bind), event!(expected)); | ||
138 | } | ||
139 | |||
140 | #[test] | ||
141 | #[should_panic] | ||
142 | fn very_long_and_wrong_keybind() { | ||
143 | let bind = "alksdjfalkjdf"; | ||
144 | Event::from(bind); | ||
145 | } | ||
146 | } | ||
diff --git a/src/theme.rs b/src/theme.rs index 7ae9efc..879584c 100644 --- a/src/theme.rs +++ b/src/theme.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use cursive::theme::Color::{self, *}; | 1 | use cursive::theme::Color::{self, *}; |
2 | use cursive::theme::PaletteColor::*; | 2 | use cursive::theme::PaletteColor::*; |
3 | use cursive::theme::{BorderStyle, ColorStyle, Palette, Style, Theme}; | 3 | use cursive::theme::{BorderStyle, Palette, Theme}; |
4 | 4 | ||
5 | pub fn pallete_gen() -> Palette { | 5 | pub fn pallete_gen() -> Palette { |
6 | let mut p = Palette::default(); | 6 | let mut p = Palette::default(); |
diff --git a/src/utils.rs b/src/utils.rs index 2453aa6..f5a25c8 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -102,12 +102,12 @@ pub fn load_configuration_file() -> AppConfig { | |||
102 | if let Ok(ref mut f) = File::open(&cf) { | 102 | if let Ok(ref mut f) = File::open(&cf) { |
103 | let mut j = String::new(); | 103 | let mut j = String::new(); |
104 | f.read_to_string(&mut j); | 104 | f.read_to_string(&mut j); |
105 | return toml::from_str(&j).unwrap(); | 105 | return toml::from_str(&j).unwrap_or_else(|e| panic!("Invalid config file: `{}`", e)); |
106 | } else { | 106 | } else { |
107 | if let Ok(dc) = toml::to_string(&AppConfig::default()) { | 107 | if let Ok(dc) = toml::to_string(&AppConfig::default()) { |
108 | match OpenOptions::new().create(true).write(true).open(&cf) { | 108 | match OpenOptions::new().create(true).write(true).open(&cf) { |
109 | Ok(ref mut file) => file.write(dc.as_bytes()).unwrap(), | 109 | Ok(ref mut file) => file.write(dc.as_bytes()).unwrap(), |
110 | Err(_) => 0, | 110 | Err(_) => panic!("Unable to write config file to disk!"), |
111 | }; | 111 | }; |
112 | } | 112 | } |
113 | return Default::default(); | 113 | return Default::default(); |
diff --git a/src/views.rs b/src/views.rs index a306602..b90ce2b 100644 --- a/src/views.rs +++ b/src/views.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use cursive::direction::Direction; | 1 | use cursive::direction::Direction; |
2 | use cursive::event::{Event, EventResult, Key}; | 2 | use cursive::event::{Event, EventResult, Key}; |
3 | use cursive::theme::{ColorStyle, ColorType, Effect, Style}; | 3 | use cursive::theme::{ColorStyle, Effect, Style}; |
4 | use cursive::view::View; | 4 | use cursive::view::View; |
5 | use cursive::{Printer, Vec2}; | 5 | use cursive::{Printer, Vec2}; |
6 | 6 | ||
@@ -35,13 +35,12 @@ where | |||
35 | // .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) | 35 | // .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) |
36 | // .unwrap() | 36 | // .unwrap() |
37 | // }; | 37 | // }; |
38 | let now = self.cursor().0; | 38 | let now = self.inner_data_ref().cursor().0; |
39 | let is_today = now == Local::now().naive_local().date(); | 39 | let is_today = now == Local::now().naive_local().date(); |
40 | let year = now.year(); | 40 | let year = now.year(); |
41 | let month = now.month(); | 41 | let month = now.month(); |
42 | 42 | ||
43 | let goal_reached_style = Style::from(CONFIGURATION.reached_color()); | 43 | let goal_reached_style = Style::from(CONFIGURATION.reached_color()); |
44 | let todo_style = Style::from(CONFIGURATION.todo_color()); | ||
45 | let future_style = Style::from(CONFIGURATION.inactive_color()); | 44 | let future_style = Style::from(CONFIGURATION.inactive_color()); |
46 | 45 | ||
47 | let strikethrough = Style::from(Effect::Strikethrough); | 46 | let strikethrough = Style::from(Effect::Strikethrough); |
@@ -141,7 +140,7 @@ where | |||
141 | } | 140 | } |
142 | }; | 141 | }; |
143 | 142 | ||
144 | match self.view_mode() { | 143 | match self.inner_data_ref().view_mode() { |
145 | ViewMode::Day => draw_day(printer), | 144 | ViewMode::Day => draw_day(printer), |
146 | ViewMode::Week => draw_week(printer), | 145 | ViewMode::Week => draw_week(printer), |
147 | _ => draw_day(printer), | 146 | _ => draw_day(printer), |
@@ -157,7 +156,7 @@ where | |||
157 | } | 156 | } |
158 | 157 | ||
159 | fn on_event(&mut self, e: Event) -> EventResult { | 158 | fn on_event(&mut self, e: Event) -> EventResult { |
160 | let now = self.cursor().0; | 159 | let now = self.inner_data_mut_ref().cursor().0; |
161 | if self.is_auto() { | 160 | if self.is_auto() { |
162 | return EventResult::Ignored; | 161 | return EventResult::Ignored; |
163 | } | 162 | } |