aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-05-13 18:01:26 +0100
committerAkshay <[email protected]>2021-05-13 18:01:26 +0100
commita9142b54a72bac4864ae1147a96fcf6896cee3d6 (patch)
treebb7e3cf5dafecc24c09d592b58c904b8aa7e26dc /src
parentbf30b22665ef596f8627559bd8378d7ab6fbbd47 (diff)
allow custom grid_size from toml filegrid-width
Diffstat (limited to 'src')
-rw-r--r--src/app/impl_self.rs15
-rw-r--r--src/app/impl_view.rs9
-rw-r--r--src/command.rs5
-rw-r--r--src/utils.rs25
4 files changed, 33 insertions, 21 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs
index fad965a..b53cdcf 100644
--- a/src/app/impl_self.rs
+++ b/src/app/impl_self.rs
@@ -11,9 +11,10 @@ use cursive::direction::Absolute;
11use cursive::Vec2; 11use cursive::Vec2;
12use notify::{watcher, RecursiveMode, Watcher}; 12use notify::{watcher, RecursiveMode, Watcher};
13 13
14use crate::CONFIGURATION;
14use crate::command::{Command, CommandLineError, GoalKind}; 15use crate::command::{Command, CommandLineError, GoalKind};
15use crate::habit::{Bit, Count, Float, HabitWrapper, TrackEvent, ViewMode}; 16use crate::habit::{Bit, Count, Float, HabitWrapper, TrackEvent, ViewMode};
16use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; 17use crate::utils::{self, VIEW_HEIGHT, VIEW_WIDTH};
17 18
18use crate::app::{App, Cursor, Message, MessageKind, StatusLine}; 19use crate::app::{App, Cursor, Message, MessageKind, StatusLine};
19 20
@@ -105,15 +106,15 @@ impl App {
105 } 106 }
106 } 107 }
107 Absolute::Down => { 108 Absolute::Down => {
108 if self.focus + GRID_WIDTH < self.habits.len() - 1 { 109 if self.focus + CONFIGURATION.grid_size() < self.habits.len() - 1 {
109 self.focus += GRID_WIDTH; 110 self.focus += CONFIGURATION.grid_size();
110 } else { 111 } else {
111 self.focus = self.habits.len() - 1; 112 self.focus = self.habits.len() - 1;
112 } 113 }
113 } 114 }
114 Absolute::Up => { 115 Absolute::Up => {
115 if self.focus as isize - GRID_WIDTH as isize >= 0 { 116 if self.focus as isize - CONFIGURATION.grid_size() as isize >= 0 {
116 self.focus -= GRID_WIDTH; 117 self.focus -= CONFIGURATION.grid_size();
117 } else { 118 } else {
118 self.focus = 0; 119 self.focus = 0;
119 } 120 }
@@ -152,10 +153,10 @@ impl App {
152 } 153 }
153 154
154 pub fn max_size(&self) -> Vec2 { 155 pub fn max_size(&self) -> Vec2 {
155 let width = GRID_WIDTH * VIEW_WIDTH; 156 let width = CONFIGURATION.grid_size() * VIEW_WIDTH;
156 let height = { 157 let height = {
157 if !self.habits.is_empty() { 158 if !self.habits.is_empty() {
158 (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / GRID_WIDTH as f64).ceil()) 159 (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / CONFIGURATION.grid_size() as f64).ceil())
159 as usize 160 as usize
160 } else { 161 } else {
161 0 162 0
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs
index c369d8f..e4eb67a 100644
--- a/src/app/impl_view.rs
+++ b/src/app/impl_view.rs
@@ -10,15 +10,16 @@ use cursive::view::View;
10use cursive::{Printer, Vec2}; 10use cursive::{Printer, Vec2};
11use notify::DebouncedEvent; 11use notify::DebouncedEvent;
12 12
13use crate::CONFIGURATION;
13use crate::app::{App, MessageKind}; 14use crate::app::{App, MessageKind};
14use crate::habit::{HabitWrapper, ViewMode}; 15use crate::habit::{HabitWrapper, ViewMode};
15use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; 16use crate::utils::{self, VIEW_HEIGHT, VIEW_WIDTH};
16 17
17impl View for App { 18impl View for App {
18 fn draw(&self, printer: &Printer) { 19 fn draw(&self, printer: &Printer) {
19 let mut offset = Vec2::zero(); 20 let mut offset = Vec2::zero();
20 for (idx, habit) in self.habits.iter().enumerate() { 21 for (idx, habit) in self.habits.iter().enumerate() {
21 if idx >= GRID_WIDTH && idx % GRID_WIDTH == 0 { 22 if idx >= CONFIGURATION.grid_size() && idx % CONFIGURATION.grid_size() == 0 {
22 offset = offset.map_y(|y| y + VIEW_HEIGHT).map_x(|_| 0); 23 offset = offset.map_y(|y| y + VIEW_HEIGHT).map_x(|_| 0);
23 } 24 }
24 habit.draw(&printer.offset(offset).focused(self.focus == idx)); 25 habit.draw(&printer.offset(offset).focused(self.focus == idx));
@@ -41,10 +42,10 @@ impl View for App {
41 } 42 }
42 43
43 fn required_size(&mut self, _: Vec2) -> Vec2 { 44 fn required_size(&mut self, _: Vec2) -> Vec2 {
44 let width = GRID_WIDTH * (VIEW_WIDTH + 2); 45 let width = CONFIGURATION.grid_size() * (VIEW_WIDTH + 2);
45 let height = { 46 let height = {
46 if self.habits.len() > 0 { 47 if self.habits.len() > 0 {
47 (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / GRID_WIDTH as f64).ceil()) 48 (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / CONFIGURATION.grid_size() as f64).ceil())
48 as usize 49 as usize
49 } else { 50 } else {
50 0 51 0
diff --git a/src/command.rs b/src/command.rs
index bfd09d8..1b159eb 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -8,7 +8,8 @@ use cursive::views::{EditView, LinearLayout, OnEventView, TextView};
8use cursive::Cursive; 8use cursive::Cursive;
9 9
10use crate::app::App; 10use crate::app::App;
11use crate::utils::{GRID_WIDTH, VIEW_WIDTH}; 11use crate::CONFIGURATION;
12use crate::utils::VIEW_WIDTH;
12 13
13static COMMANDS: &'static [&'static str] = &[ 14static COMMANDS: &'static [&'static str] = &[
14 "add", 15 "add",
@@ -69,7 +70,7 @@ pub fn open_command_window(s: &mut Cursive) {
69 } 70 }
70 }, 71 },
71 ) 72 )
72 .fixed_width(VIEW_WIDTH * GRID_WIDTH); 73 .fixed_width(VIEW_WIDTH * CONFIGURATION.grid_size());
73 s.call_on_name("Frame", |view: &mut LinearLayout| { 74 s.call_on_name("Frame", |view: &mut LinearLayout| {
74 let mut commandline = LinearLayout::horizontal() 75 let mut commandline = LinearLayout::horizontal()
75 .child(TextView::new(":")) 76 .child(TextView::new(":"))
diff --git a/src/utils.rs b/src/utils.rs
index f5a25c8..02732c9 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -10,28 +10,34 @@ use std::path::PathBuf;
10 10
11pub const VIEW_WIDTH: usize = 25; 11pub const VIEW_WIDTH: usize = 25;
12pub const VIEW_HEIGHT: usize = 8; 12pub const VIEW_HEIGHT: usize = 8;
13pub const GRID_WIDTH: usize = 3;
14 13
15#[derive(Serialize, Deserialize)] 14#[derive(Serialize, Deserialize)]
16pub struct Characters { 15pub struct Look {
17 #[serde(default = "base_char")] 16 #[serde(default = "base_char")]
18 pub true_chr: char, 17 pub true_chr: char,
19 #[serde(default = "base_char")] 18 #[serde(default = "base_char")]
20 pub false_chr: char, 19 pub false_chr: char,
21 #[serde(default = "base_char")] 20 #[serde(default = "base_char")]
22 pub future_chr: char, 21 pub future_chr: char,
22 #[serde(default = "grid_size")]
23 pub grid_size: usize,
23} 24}
24 25
25fn base_char() -> char { 26fn base_char() -> char {
26 '·' 27 '·'
27} 28}
28 29
29impl Default for Characters { 30fn grid_size() -> usize {
31 3
32}
33
34impl Default for Look {
30 fn default() -> Self { 35 fn default() -> Self {
31 Characters { 36 Look {
32 true_chr: '·', 37 true_chr: '·',
33 false_chr: '·', 38 false_chr: '·',
34 future_chr: '·', 39 future_chr: '·',
40 grid_size: 3,
35 } 41 }
36 } 42 }
37} 43}
@@ -69,7 +75,7 @@ impl Default for Colors {
69#[derive(Serialize, Deserialize)] 75#[derive(Serialize, Deserialize)]
70pub struct AppConfig { 76pub struct AppConfig {
71 #[serde(default)] 77 #[serde(default)]
72 pub look: Characters, 78 pub look: Look,
73 79
74 #[serde(default)] 80 #[serde(default)]
75 pub colors: Colors, 81 pub colors: Colors,
@@ -87,13 +93,16 @@ impl Default for AppConfig {
87impl AppConfig { 93impl AppConfig {
88 // TODO: implement string parsing from config.json 94 // TODO: implement string parsing from config.json
89 pub fn reached_color(&self) -> Color { 95 pub fn reached_color(&self) -> Color {
90 return Color::parse(&self.colors.reached).unwrap_or(Color::Dark(BaseColor::Cyan)); 96 Color::parse(&self.colors.reached).unwrap_or(Color::Dark(BaseColor::Cyan))
91 } 97 }
92 pub fn todo_color(&self) -> Color { 98 pub fn todo_color(&self) -> Color {
93 return Color::parse(&self.colors.todo).unwrap_or(Color::Dark(BaseColor::Magenta)); 99 Color::parse(&self.colors.todo).unwrap_or(Color::Dark(BaseColor::Magenta))
94 } 100 }
95 pub fn inactive_color(&self) -> Color { 101 pub fn inactive_color(&self) -> Color {
96 return Color::parse(&self.colors.inactive).unwrap_or(Color::Light(BaseColor::Black)); 102 Color::parse(&self.colors.inactive).unwrap_or(Color::Light(BaseColor::Black))
103 }
104 pub fn grid_size(&self) -> usize {
105 self.look.grid_size
97 } 106 }
98} 107}
99 108