From 8a1dd86155823455e71ca0f73673214073a8a269 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 19 Jul 2020 14:56:39 +0530 Subject: add startup hint, fix watcher panics --- Cargo.toml | 2 -- src/app/impl_self.rs | 20 +++++--------------- src/app/impl_view.rs | 16 ++++++---------- src/app/message.rs | 12 ++++++++++++ src/theme.rs | 2 +- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4333b9b..4791c0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,6 @@ keywords = ["tracker", "event-tracker", "tui", "journal"] categories = ["date-and-time", "command-line-interface"] license = "MIT" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] serde_json = "1.0" lazy_static = "1.4.0" 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}; use crate::utils; use crate::CONFIGURATION; -use crate::app::{App, Message, MessageKind, StatusLine}; +use crate::app::{App, MessageKind, StatusLine}; impl App { pub fn new() -> Self { let (tx, rx) = channel(); let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap(); - watcher - .watch(utils::auto_habit_file(), RecursiveMode::Recursive) - .unwrap_or_else(|e| { - panic!("Unable to start file watcher: {}", e); - }); + watcher.watch(utils::auto_habit_file(), RecursiveMode::Recursive); return App { habits: vec![], focus: 0, _file_watcher: watcher, file_event_recv: rx, view_month_offset: 0, - message: Message::default(), + message: "Type :add to get started, Ctrl-L to dismiss".into(), }; } @@ -147,15 +143,9 @@ impl App { pub fn max_size(&self) -> Vec2 { let grid_width = CONFIGURATION.grid_width; - let width = { - if self.habits.len() > 0 { - grid_width * CONFIGURATION.view_width - } else { - 0 - } - }; + let width = grid_width * CONFIGURATION.view_width; let height = { - if self.habits.len() > 0 { + if !self.habits.is_empty() { (CONFIGURATION.view_height as f64 * (self.habits.len() as f64 / grid_width as f64).ceil()) 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; use cursive::direction::{Absolute, Direction}; use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{Color, Style}; +use cursive::theme::Color; use cursive::view::View; use cursive::{Printer, Vec2}; use notify::DebouncedEvent; @@ -48,23 +48,16 @@ impl View for App { let grid_width = CONFIGURATION.grid_width; let view_width = CONFIGURATION.view_width; let view_height = CONFIGURATION.view_height; - let width = { - if self.habits.len() > 0 { - grid_width * (view_width + 2) - } else { - 0 - } - }; + let width = grid_width * (view_width + 2); let height = { if self.habits.len() > 0 { (view_height as f64 * (self.habits.len() as f64 / grid_width as f64).ceil()) as usize - + 2 // to acoomodate statusline and message line } else { 0 } }; - Vec2::new(width, height) + Vec2::new(width, height + 2) } fn take_focus(&mut self, _: Direction) -> bool { @@ -89,6 +82,9 @@ impl View for App { } _ => {} }; + if self.habits.is_empty() { + return EventResult::Ignored; + } match e { Event::Key(Key::Right) | Event::Key(Key::Tab) | Event::Char('l') => { 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 for Color { } } +impl From for Message +where + T: AsRef, +{ + fn from(item: T) -> Self { + return Message { + msg: item.as_ref().to_string(), + kind: MessageKind::Info, + }; + } +} + pub struct Message { msg: String, 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 @@ use cursive::theme::Color::*; use cursive::theme::PaletteColor::*; -use cursive::theme::{BaseColor, BorderStyle, ColorStyle, Palette, Theme}; +use cursive::theme::{BaseColor, BorderStyle, Palette, Theme}; pub fn pallete_gen() -> Palette { let mut p = Palette::default(); -- cgit v1.2.3