aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-02-26 06:56:25 +0000
committerAkshay <[email protected]>2020-02-26 06:56:25 +0000
commitce5f500998f05fac13c6472681eedb7702055a7f (patch)
tree272305db84f52ff958f03e7f0b908bde455b7fb7 /src/main.rs
parent242aa78d1f593a42f82cc32b4cc84eb003c3bbe7 (diff)
add App struct
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs78
1 files changed, 76 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 2b70033..a9ea9b1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,9 +1,14 @@
1#![allow(unused_must_use)] 1#![allow(unused_must_use)]
2 2
3use chrono::NaiveDate; 3use chrono::NaiveDate;
4use std::f64;
4 5
6use cursive::direction::Direction;
7use cursive::event::{Event, EventResult, Key};
8use cursive::view::View;
5use cursive::views::{Dialog, EditView, LinearLayout, ListView, SelectView}; 9use cursive::views::{Dialog, EditView, LinearLayout, ListView, SelectView};
6use cursive::Cursive; 10use cursive::Cursive;
11use cursive::{Printer, Vec2};
7 12
8mod habit; 13mod habit;
9use crate::habit::{Habit, HabitTrait, HabitType}; 14use crate::habit::{Habit, HabitTrait, HabitType};
@@ -13,9 +18,78 @@ use crate::views::habitview::HabitView;
13 18
14mod theme; 19mod theme;
15 20
21#[derive(PartialEq)]
22enum ViewMode {
23 Day,
24 Month,
25 Year,
26}
27
16struct App { 28struct App {
17 habits: Vec<Habit>, 29 habits: Vec<HabitView>,
18 status: String, 30 status: String,
31 view_mode: ViewMode,
32 focus: usize,
33
34 padding: usize,
35}
36
37impl App {
38 fn new() -> Self {
39 return App {
40 habits: vec![],
41 status: "".to_string(),
42 view_mode: ViewMode::Day,
43 focus: 0,
44 padding: 2,
45 };
46 }
47 fn add_habit(&mut self, h: HabitView) -> &mut Self {
48 self.habits.push(h);
49 return self;
50 }
51 fn set_mode(&mut self, set_mode: ViewMode) -> &mut Self {
52 if set_mode != self.view_mode {
53 self.view_mode = set_mode
54 }
55 return self;
56 }
57}
58
59impl View for App {
60 fn draw(&self, printer: &Printer) {
61 let grid_width = 3;
62 let width = {
63 if self.habits.len() > 0 {
64 grid_width * self.habits[0].get_size().x
65 } else {
66 0
67 }
68 };
69 let height = {
70 if self.habits.len() > 0 {
71 (self.habits[0].get_size().y as f64 / grid_width as f64).ceil() as usize
72 } else {
73 0
74 }
75 };
76 let mut offset = Vec2::new(width + self.padding, height + self.padding);
77 for (idx, i) in self.habits.iter().enumerate() {
78 i.draw(&printer.offset(offset).focused(self.focus == idx));
79 offset = offset.saturating_add(i.get_size());
80 }
81 }
82 fn required_size(&mut self, _: Vec2) -> Vec2 {
83 todo!()
84 }
85
86 fn take_focus(&mut self, _: Direction) -> bool {
87 todo!()
88 }
89
90 fn on_event(&mut self, e: Event) -> EventResult {
91 todo!()
92 }
19} 93}
20 94
21fn main() { 95fn main() {
@@ -28,7 +102,7 @@ fn main() {
28 gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 14), HabitType::Count(10)); 102 gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 14), HabitType::Count(10));
29 gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 15), HabitType::Count(11)); 103 gymming.insert_entry(NaiveDate::from_ymd(2020, 2, 15), HabitType::Count(11));
30 104
31 let mut reading = Habit::new("read", HabitType::Count(5)); 105 let mut reading = Habit::new("read", HabitType::Bit(true));
32 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 11), HabitType::Bit(true)); 106 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 11), HabitType::Bit(true));
33 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 12), HabitType::Bit(false)); 107 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 12), HabitType::Bit(false));
34 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 13), HabitType::Bit(true)); 108 reading.insert_entry(NaiveDate::from_ymd(2020, 2, 13), HabitType::Bit(true));