From 86d4b1c206b03f419aae8f304e2d21e7f27a2430 Mon Sep 17 00:00:00 2001 From: Arpan Koirala Date: Sun, 22 Dec 2024 15:10:08 +0545 Subject: [PATCH] feat: made interval step and min interval configurable --- .config/config.json5 | 4 ++++ src/app.rs | 22 ++++++++++++---------- src/components/interval.rs | 13 +++++++------ src/config.rs | 11 +++++++++++ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/.config/config.json5 b/.config/config.json5 index b51c85b..a6f35ee 100644 --- a/.config/config.json5 +++ b/.config/config.json5 @@ -70,5 +70,9 @@ "search_highlight": "black on yellow", "readonly": "bold yellow" } + }, + "general": { + "min_interval_ms": 500, + "interval_step_ms": 500 } } diff --git a/src/app.rs b/src/app.rs index 82f0689..9656eed 100644 --- a/src/app.rs +++ b/src/app.rs @@ -284,7 +284,8 @@ impl App { } Action::Quit => self.should_quit = true, Action::IncreaseInterval => { - self.runtime_config.interval += Duration::milliseconds(500); + self.runtime_config.interval += + Duration::milliseconds(self.config.general.interval_step_ms); self.store.set_runtime_config(StoreRuntimeConfig { interval: self.runtime_config.interval.num_milliseconds() as u64, @@ -292,17 +293,18 @@ impl App { })?; } Action::DecreaseInterval => { - let new_interval = - self.runtime_config.interval - Duration::milliseconds(500); + let min_interval = + Duration::milliseconds(self.config.general.min_interval_ms); + let step = Duration::milliseconds(self.config.general.interval_step_ms); - if new_interval >= chrono::Duration::milliseconds(500) { - self.runtime_config.interval = new_interval; + let new_interval = (self.runtime_config.interval - step).max(min_interval); - self.store.set_runtime_config(StoreRuntimeConfig { - interval: self.runtime_config.interval.num_milliseconds() as u64, - command: self.runtime_config.command.join(" "), - })?; - } + self.runtime_config.interval = new_interval; + + self.store.set_runtime_config(StoreRuntimeConfig { + interval: new_interval.num_milliseconds() as u64, + command: self.runtime_config.command.join(" "), + })?; } Action::Suspend => self.should_suspend = true, Action::Resume => self.should_suspend = false, diff --git a/src/components/interval.rs b/src/components/interval.rs index 4c7b070..e6ae4fc 100644 --- a/src/components/interval.rs +++ b/src/components/interval.rs @@ -1,5 +1,6 @@ use std::{collections::HashMap, time::Duration}; +use chrono::Duration as ChronoDuration; use color_eyre::eyre::Result; use crossterm::event::{KeyCode, KeyEvent}; use ratatui::{prelude::*, widgets::*}; @@ -28,15 +29,15 @@ impl Interval { } pub fn increase_interval(&mut self) { - self.runtime_config.interval += chrono::Duration::milliseconds(500); + self.runtime_config.interval += + chrono::Duration::milliseconds(self.config.general.interval_step_ms); } pub fn decrease_interval(&mut self) { - let new_interval = self.runtime_config.interval - chrono::Duration::milliseconds(500); - - if new_interval >= chrono::Duration::milliseconds(500) { - self.runtime_config.interval = new_interval; - } + let min_interval = ChronoDuration::milliseconds(self.config.general.min_interval_ms); + let step = ChronoDuration::milliseconds(self.config.general.interval_step_ms); + let new_interval = (self.runtime_config.interval - step).max(min_interval); + self.runtime_config.interval = new_interval; } } diff --git a/src/config.rs b/src/config.rs index 0865f1a..7c7365d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -46,6 +46,10 @@ pub struct General { pub skip_empty_diffs: Option, #[serde(default)] pub disable_mouse: Option, + #[serde(default)] + pub min_interval_ms: i64, + #[serde(default)] + pub interval_step_ms: i64, } impl From for General { @@ -56,6 +60,7 @@ impl From for General { shell_options: value.shell_options, skip_empty_diffs: value.skip_empty_diffs, disable_mouse: value.disable_mouse, + ..Default::default() } } } @@ -167,6 +172,12 @@ impl Config { if self.general.disable_mouse.is_none() { self.general.disable_mouse = default_config.general.disable_mouse; } + if self.general.min_interval_ms == 0 { + self.general.min_interval_ms = default_config.general.min_interval_ms; + } + if self.general.interval_step_ms == 0 { + self.general.interval_step_ms = default_config.general.interval_step_ms; + } } pub fn get_style(&self, style: &str) -> Style {