From a9142b54a72bac4864ae1147a96fcf6896cee3d6 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 13 May 2021 22:31:26 +0530 Subject: allow custom grid_size from toml file --- src/app/impl_self.rs | 15 ++++++++------- src/app/impl_view.rs | 9 +++++---- src/command.rs | 5 +++-- src/utils.rs | 25 +++++++++++++++++-------- 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; use cursive::Vec2; use notify::{watcher, RecursiveMode, Watcher}; +use crate::CONFIGURATION; use crate::command::{Command, CommandLineError, GoalKind}; use crate::habit::{Bit, Count, Float, HabitWrapper, TrackEvent, ViewMode}; -use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; +use crate::utils::{self, VIEW_HEIGHT, VIEW_WIDTH}; use crate::app::{App, Cursor, Message, MessageKind, StatusLine}; @@ -105,15 +106,15 @@ impl App { } } Absolute::Down => { - if self.focus + GRID_WIDTH < self.habits.len() - 1 { - self.focus += GRID_WIDTH; + if self.focus + CONFIGURATION.grid_size() < self.habits.len() - 1 { + self.focus += CONFIGURATION.grid_size(); } else { self.focus = self.habits.len() - 1; } } Absolute::Up => { - if self.focus as isize - GRID_WIDTH as isize >= 0 { - self.focus -= GRID_WIDTH; + if self.focus as isize - CONFIGURATION.grid_size() as isize >= 0 { + self.focus -= CONFIGURATION.grid_size(); } else { self.focus = 0; } @@ -152,10 +153,10 @@ impl App { } pub fn max_size(&self) -> Vec2 { - let width = GRID_WIDTH * VIEW_WIDTH; + let width = CONFIGURATION.grid_size() * VIEW_WIDTH; let height = { if !self.habits.is_empty() { - (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / GRID_WIDTH as f64).ceil()) + (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / CONFIGURATION.grid_size() as f64).ceil()) as usize } else { 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; use cursive::{Printer, Vec2}; use notify::DebouncedEvent; +use crate::CONFIGURATION; use crate::app::{App, MessageKind}; use crate::habit::{HabitWrapper, ViewMode}; -use crate::utils::{self, GRID_WIDTH, VIEW_HEIGHT, VIEW_WIDTH}; +use crate::utils::{self, VIEW_HEIGHT, VIEW_WIDTH}; impl View for App { fn draw(&self, printer: &Printer) { let mut offset = Vec2::zero(); for (idx, habit) in self.habits.iter().enumerate() { - if idx >= GRID_WIDTH && idx % GRID_WIDTH == 0 { + if idx >= CONFIGURATION.grid_size() && idx % CONFIGURATION.grid_size() == 0 { offset = offset.map_y(|y| y + VIEW_HEIGHT).map_x(|_| 0); } habit.draw(&printer.offset(offset).focused(self.focus == idx)); @@ -41,10 +42,10 @@ impl View for App { } fn required_size(&mut self, _: Vec2) -> Vec2 { - let width = GRID_WIDTH * (VIEW_WIDTH + 2); + let width = CONFIGURATION.grid_size() * (VIEW_WIDTH + 2); let height = { if self.habits.len() > 0 { - (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / GRID_WIDTH as f64).ceil()) + (VIEW_HEIGHT as f64 * (self.habits.len() as f64 / CONFIGURATION.grid_size() as f64).ceil()) as usize } else { 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}; use cursive::Cursive; use crate::app::App; -use crate::utils::{GRID_WIDTH, VIEW_WIDTH}; +use crate::CONFIGURATION; +use crate::utils::VIEW_WIDTH; static COMMANDS: &'static [&'static str] = &[ "add", @@ -69,7 +70,7 @@ pub fn open_command_window(s: &mut Cursive) { } }, ) - .fixed_width(VIEW_WIDTH * GRID_WIDTH); + .fixed_width(VIEW_WIDTH * CONFIGURATION.grid_size()); s.call_on_name("Frame", |view: &mut LinearLayout| { let mut commandline = LinearLayout::horizontal() .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; pub const VIEW_WIDTH: usize = 25; pub const VIEW_HEIGHT: usize = 8; -pub const GRID_WIDTH: usize = 3; #[derive(Serialize, Deserialize)] -pub struct Characters { +pub struct Look { #[serde(default = "base_char")] pub true_chr: char, #[serde(default = "base_char")] pub false_chr: char, #[serde(default = "base_char")] pub future_chr: char, + #[serde(default = "grid_size")] + pub grid_size: usize, } fn base_char() -> char { '·' } -impl Default for Characters { +fn grid_size() -> usize { + 3 +} + +impl Default for Look { fn default() -> Self { - Characters { + Look { true_chr: '·', false_chr: '·', future_chr: '·', + grid_size: 3, } } } @@ -69,7 +75,7 @@ impl Default for Colors { #[derive(Serialize, Deserialize)] pub struct AppConfig { #[serde(default)] - pub look: Characters, + pub look: Look, #[serde(default)] pub colors: Colors, @@ -87,13 +93,16 @@ impl Default for AppConfig { impl AppConfig { // TODO: implement string parsing from config.json pub fn reached_color(&self) -> Color { - return Color::parse(&self.colors.reached).unwrap_or(Color::Dark(BaseColor::Cyan)); + Color::parse(&self.colors.reached).unwrap_or(Color::Dark(BaseColor::Cyan)) } pub fn todo_color(&self) -> Color { - return Color::parse(&self.colors.todo).unwrap_or(Color::Dark(BaseColor::Magenta)); + Color::parse(&self.colors.todo).unwrap_or(Color::Dark(BaseColor::Magenta)) } pub fn inactive_color(&self) -> Color { - return Color::parse(&self.colors.inactive).unwrap_or(Color::Light(BaseColor::Black)); + Color::parse(&self.colors.inactive).unwrap_or(Color::Light(BaseColor::Black)) + } + pub fn grid_size(&self) -> usize { + self.look.grid_size } } -- cgit v1.2.3