aboutsummaryrefslogtreecommitdiff
path: root/src/views.rs
blob: 5b9ec207a9f73cc87813f753727021c60bce4c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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<bool>,
    true_chr: char,
    false_chr: char,
    future_chr: char,

    view_width: u32,
    view_height: u32,
    // color config
}

impl BitView {
    pub fn new(habit: Habit<bool>) -> 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,
        }
    }
}