From d33e960c57fda3fa1c916ad765e32a673524d55e Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 10 Feb 2020 20:56:22 +0530 Subject: move structs out --- src/habit.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++ src/views.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/habit.rs create mode 100644 src/views.rs (limited to 'src') diff --git a/src/habit.rs b/src/habit.rs new file mode 100644 index 0000000..4f05217 --- /dev/null +++ b/src/habit.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; + +use chrono::NaiveDate; + +#[derive(Debug)] +pub struct Habit { + name: String, + stats: HashMap, +} + +impl Habit +where + T: Copy, +{ + pub fn new(name: &str) -> Habit { + return Habit { + name: name.to_owned(), + stats: HashMap::new(), + }; + } + + pub fn get_name(&self) -> String { + return self.name.to_owned(); + } + + pub fn get_by_date(&self, date: NaiveDate) -> Option<&T> { + self.stats.get(&date) + } + + pub fn insert_entry(&mut self, date: NaiveDate, val: T) { + *self.stats.entry(date).or_insert(val) = val; + } +} + +impl Habit { + pub fn toggle(&mut self, date: NaiveDate) { + if let Some(v) = self.stats.get_mut(&date) { + *v ^= true + } else { + self.insert_entry(date, true); + } + } +} + +impl Habit { + pub fn increment(&mut self, date: NaiveDate) { + if let Some(v) = self.stats.get_mut(&date) { + *v += 1 + } + } + pub fn decrement(&mut self, date: NaiveDate) { + if let Some(v) = self.stats.get_mut(&date) { + if *v >= 1 { + *v -= 1; + } else { + *v = 0; + }; + } + } + pub fn set(&mut self, date: NaiveDate, val: u32) { + *self.stats.entry(date).or_insert(val) = val; + } +} diff --git a/src/views.rs b/src/views.rs new file mode 100644 index 0000000..5b9ec20 --- /dev/null +++ b/src/views.rs @@ -0,0 +1,80 @@ +use cursive::direction::Direction; +use cursive::event::{Event, EventResult, Key}; +use cursive::view::View; +use cursive::{Printer, Vec2}; + +use chrono::prelude::*; +use chrono::{Local, NaiveDate}; + +use crate::habit::Habit; + +pub struct BitView { + habit: Habit, + true_chr: char, + false_chr: char, + future_chr: char, + + view_width: u32, + view_height: u32, + // color config +} + +impl BitView { + pub fn new(habit: Habit) -> Self { + return BitView { + habit, + true_chr: 'x', + false_chr: 'o', + future_chr: '.', + view_width: 21, + view_height: 9, + }; + } + pub fn get_title(&self) -> String { + return self.habit.get_name().to_owned(); + } +} + +impl View for BitView { + fn draw(&self, printer: &Printer) { + let now = Local::now(); + let year = now.year(); + let month = now.month(); + + for i in 1..=31 { + let day = NaiveDate::from_ymd_opt(year, month, i); + if let Some(d) = day { + let day_status = self.habit.get_by_date(d).unwrap_or(&false); + let coords = ((i % 7) * 3, i / 7 + 2); + + if d <= now.naive_utc().date() { + if *day_status { + printer.print(coords, &format!("{:^3}", self.true_chr)) + } else { + printer.print(coords, &format!("{:^3}", self.false_chr)) + } + } else { + printer.print(coords, &format!("{:^3}", self.future_chr)) + } + } + } + } + + fn required_size(&mut self, _: Vec2) -> Vec2 { + (20, 9).into() + } + + fn take_focus(&mut self, _: Direction) -> bool { + true + } + + fn on_event(&mut self, e: Event) -> EventResult { + match e { + Event::Key(Key::Enter) => { + self.habit.toggle(Local::now().naive_utc().date()); + return EventResult::Consumed(None); + } + _ => return EventResult::Ignored, + } + } +} -- cgit v1.2.3