aboutsummaryrefslogtreecommitdiff
path: root/src/views.rs
blob: ebb83b864ff3bb4d759fe2c97784b8657250faa9 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use cursive::direction::Direction;
use cursive::event::{Event, EventResult, Key};
use cursive::theme::Style;
use cursive::view::View;
use cursive::{Printer, Vec2};

use chrono::prelude::*;
use chrono::{Local, NaiveDate};

use crate::habit::{Bit, Count, Habit, TrackEvent};
use crate::CONFIGURATION;

pub trait ShadowView {
    fn draw(&self, printer: &Printer);
    fn required_size(&mut self, _: Vec2) -> Vec2;
    fn take_focus(&mut self, _: Direction) -> bool;
    fn on_event(&mut self, e: Event) -> EventResult;
}

// the only way to not rewrite each View implementation for trait
// objects of Habit is to rewrite the View trait itself.
impl<T> ShadowView for T
where
    T: Habit,
    T::HabitType: std::fmt::Display,
{
    fn draw(&self, printer: &Printer) {
        let now = Local::now();
        let year = now.year();
        let month = now.month();

        let goal_reached_style = Style::from(CONFIGURATION.reached_color);
        let todo_style = Style::from(CONFIGURATION.todo_color);
        let future_style = Style::from(CONFIGURATION.future_color);

        printer.with_style(
            if !printer.focused {
                future_style
            } else {
                goal_reached_style
            },
            |p| {
                p.print(
                    (0, 0),
                    &format!("{:width$}", self.name(), width = CONFIGURATION.view_width),
                )
            },
        );

        for i in 1..=31 {
            let day = NaiveDate::from_ymd_opt(year, month, i);
            let day_style;

            if let Some(d) = day {
                if self.reached_goal(d) {
                    day_style = goal_reached_style;
                } else {
                    day_style = todo_style;
                }
                let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into();
                if let Some(c) = self.get_by_date(d) {
                    printer.with_style(day_style, |p| {
                        p.print(coords, &format!("{:^3}", c));
                    });
                } else {
                    printer.with_style(future_style, |p| {
                        p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr));
                    });
                }
                //printer.with_style(day_style, |p| {
                //        p.print(coords, &format!("{:^3}", c));
                //    } else {
                //        p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr));
                //    }
                //});
            }
        }
    }
    fn required_size(&mut self, _: Vec2) -> Vec2 {
        (25, 6).into()
    }

    fn take_focus(&mut self, _: Direction) -> bool {
        true
    }

    fn on_event(&mut self, e: Event) -> EventResult {
        let now = Local::now().naive_utc().date();
        match e {
            Event::Key(Key::Enter) | Event::Char('n') => {
                self.modify(now, TrackEvent::Increment);
                return EventResult::Consumed(None);
            }
            Event::Key(Key::Backspace) | Event::Char('p') => {
                self.modify(now, TrackEvent::Decrement);
                return EventResult::Consumed(None);
            }
            _ => return EventResult::Ignored,
        }
    }
}

macro_rules! auto_view_impl {
    ($struct_name:ident) => {
        impl View for $struct_name {
            fn draw(&self, printer: &Printer) {
                ShadowView::draw(self, printer);
            }
            fn required_size(&mut self, x: Vec2) -> Vec2 {
                ShadowView::required_size(self, x)
            }

            fn take_focus(&mut self, d: Direction) -> bool {
                ShadowView::take_focus(self, d)
            }

            fn on_event(&mut self, e: Event) -> EventResult {
                ShadowView::on_event(self, e)
            }
        }
    };
}

auto_view_impl!(Count);
auto_view_impl!(Bit);