aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-19 10:26:39 +0100
committerAkshay <[email protected]>2020-07-19 10:26:39 +0100
commit8a1dd86155823455e71ca0f73673214073a8a269 (patch)
tree41ecaffa397d4f86bb55d57b266dd6a65e19f2e0 /src
parent9c858841c7566e79c0e05a6295e5f9bc86468d16 (diff)
add startup hint, fix watcher panics
Diffstat (limited to 'src')
-rw-r--r--src/app/impl_self.rs20
-rw-r--r--src/app/impl_view.rs16
-rw-r--r--src/app/message.rs12
-rw-r--r--src/theme.rs2
4 files changed, 24 insertions, 26 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs
index 2b115a0..744f906 100644
--- a/src/app/impl_self.rs
+++ b/src/app/impl_self.rs
@@ -16,24 +16,20 @@ use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode};
16use crate::utils; 16use crate::utils;
17use crate::CONFIGURATION; 17use crate::CONFIGURATION;
18 18
19use crate::app::{App, Message, MessageKind, StatusLine}; 19use crate::app::{App, MessageKind, StatusLine};
20 20
21impl App { 21impl App {
22 pub fn new() -> Self { 22 pub fn new() -> Self {
23 let (tx, rx) = channel(); 23 let (tx, rx) = channel();
24 let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap(); 24 let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
25 watcher 25 watcher.watch(utils::auto_habit_file(), RecursiveMode::Recursive);
26 .watch(utils::auto_habit_file(), RecursiveMode::Recursive)
27 .unwrap_or_else(|e| {
28 panic!("Unable to start file watcher: {}", e);
29 });
30 return App { 26 return App {
31 habits: vec![], 27 habits: vec![],
32 focus: 0, 28 focus: 0,
33 _file_watcher: watcher, 29 _file_watcher: watcher,
34 file_event_recv: rx, 30 file_event_recv: rx,
35 view_month_offset: 0, 31 view_month_offset: 0,
36 message: Message::default(), 32 message: "Type :add <habit-name> <goal> to get started, Ctrl-L to dismiss".into(),
37 }; 33 };
38 } 34 }
39 35
@@ -147,15 +143,9 @@ impl App {
147 143
148 pub fn max_size(&self) -> Vec2 { 144 pub fn max_size(&self) -> Vec2 {
149 let grid_width = CONFIGURATION.grid_width; 145 let grid_width = CONFIGURATION.grid_width;
150 let width = { 146 let width = grid_width * CONFIGURATION.view_width;
151 if self.habits.len() > 0 {
152 grid_width * CONFIGURATION.view_width
153 } else {
154 0
155 }
156 };
157 let height = { 147 let height = {
158 if self.habits.len() > 0 { 148 if !self.habits.is_empty() {
159 (CONFIGURATION.view_height as f64 149 (CONFIGURATION.view_height as f64
160 * (self.habits.len() as f64 / grid_width as f64).ceil()) 150 * (self.habits.len() as f64 / grid_width as f64).ceil())
161 as usize 151 as usize
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs
index 0a6bce6..892b00c 100644
--- a/src/app/impl_view.rs
+++ b/src/app/impl_view.rs
@@ -5,7 +5,7 @@ use std::path::PathBuf;
5 5
6use cursive::direction::{Absolute, Direction}; 6use cursive::direction::{Absolute, Direction};
7use cursive::event::{Event, EventResult, Key}; 7use cursive::event::{Event, EventResult, Key};
8use cursive::theme::{Color, Style}; 8use cursive::theme::Color;
9use cursive::view::View; 9use cursive::view::View;
10use cursive::{Printer, Vec2}; 10use cursive::{Printer, Vec2};
11use notify::DebouncedEvent; 11use notify::DebouncedEvent;
@@ -48,23 +48,16 @@ impl View for App {
48 let grid_width = CONFIGURATION.grid_width; 48 let grid_width = CONFIGURATION.grid_width;
49 let view_width = CONFIGURATION.view_width; 49 let view_width = CONFIGURATION.view_width;
50 let view_height = CONFIGURATION.view_height; 50 let view_height = CONFIGURATION.view_height;
51 let width = { 51 let width = grid_width * (view_width + 2);
52 if self.habits.len() > 0 {
53 grid_width * (view_width + 2)
54 } else {
55 0
56 }
57 };
58 let height = { 52 let height = {
59 if self.habits.len() > 0 { 53 if self.habits.len() > 0 {
60 (view_height as f64 * (self.habits.len() as f64 / grid_width as f64).ceil()) 54 (view_height as f64 * (self.habits.len() as f64 / grid_width as f64).ceil())
61 as usize 55 as usize
62 + 2 // to acoomodate statusline and message line
63 } else { 56 } else {
64 0 57 0
65 } 58 }
66 }; 59 };
67 Vec2::new(width, height) 60 Vec2::new(width, height + 2)
68 } 61 }
69 62
70 fn take_focus(&mut self, _: Direction) -> bool { 63 fn take_focus(&mut self, _: Direction) -> bool {
@@ -89,6 +82,9 @@ impl View for App {
89 } 82 }
90 _ => {} 83 _ => {}
91 }; 84 };
85 if self.habits.is_empty() {
86 return EventResult::Ignored;
87 }
92 match e { 88 match e {
93 Event::Key(Key::Right) | Event::Key(Key::Tab) | Event::Char('l') => { 89 Event::Key(Key::Right) | Event::Key(Key::Tab) | Event::Char('l') => {
94 self.set_focus(Absolute::Right); 90 self.set_focus(Absolute::Right);
diff --git a/src/app/message.rs b/src/app/message.rs
index 34d3293..65f0a5c 100644
--- a/src/app/message.rs
+++ b/src/app/message.rs
@@ -17,6 +17,18 @@ impl From<MessageKind> for Color {
17 } 17 }
18} 18}
19 19
20impl<T> From<T> for Message
21where
22 T: AsRef<str>,
23{
24 fn from(item: T) -> Self {
25 return Message {
26 msg: item.as_ref().to_string(),
27 kind: MessageKind::Info,
28 };
29 }
30}
31
20pub struct Message { 32pub struct Message {
21 msg: String, 33 msg: String,
22 kind: MessageKind, 34 kind: MessageKind,
diff --git a/src/theme.rs b/src/theme.rs
index f29b273..4194777 100644
--- a/src/theme.rs
+++ b/src/theme.rs
@@ -1,6 +1,6 @@
1use cursive::theme::Color::*; 1use cursive::theme::Color::*;
2use cursive::theme::PaletteColor::*; 2use cursive::theme::PaletteColor::*;
3use cursive::theme::{BaseColor, BorderStyle, ColorStyle, Palette, Theme}; 3use cursive::theme::{BaseColor, BorderStyle, Palette, Theme};
4 4
5pub fn pallete_gen() -> Palette { 5pub fn pallete_gen() -> Palette {
6 let mut p = Palette::default(); 6 let mut p = Palette::default();