From b04641fcba42915c5c101c2669959371dcca902d Mon Sep 17 00:00:00 2001 From: Arpan Koirala Date: Fri, 20 Dec 2024 14:34:34 +0545 Subject: [PATCH 1/2] feat: support for increasing and decreasing interval --- .config/config.json5 | 2 ++ src/action.rs | 2 ++ src/app.rs | 33 +++++++++++++++++++++++++++------ src/components/home.rs | 6 ++++++ src/components/interval.rs | 12 ++++++++++++ src/config.rs | 2 ++ src/old_config.rs | 2 ++ src/runner.rs | 23 +++++++++++++++++++---- src/store.rs | 2 +- 9 files changed, 73 insertions(+), 11 deletions(-) diff --git a/.config/config.json5 b/.config/config.json5 index d54ac6b..b51c85b 100644 --- a/.config/config.json5 +++ b/.config/config.json5 @@ -38,6 +38,8 @@ "": "EnterSearchMode", "": "SwitchNoTitle", "": "ShowHelp", + "<+>": "IncreaseInterval", + "<->": "DecreaseInterval", }, "Search": { "": "Quit", // Another way to quit diff --git a/src/action.rs b/src/action.rs index 189ad91..93818b3 100644 --- a/src/action.rs +++ b/src/action.rs @@ -80,4 +80,6 @@ pub enum Action { UpdateLatestHistoryCount, ShowHelp, ExitHelp, + IncreaseInterval, + DecreaseInterval, } diff --git a/src/app.rs b/src/app.rs index 3afe0df..82f0689 100644 --- a/src/app.rs +++ b/src/app.rs @@ -61,9 +61,7 @@ impl App { let store_runtime_config = store.get_runtime_config()?.unwrap_or_default(); RuntimeConfig { - interval: Duration::from_std(humantime::parse_duration( - &store_runtime_config.interval, - )?)?, + interval: Duration::milliseconds(store_runtime_config.interval as i64), command: store_runtime_config .command .split(' ') @@ -76,10 +74,12 @@ impl App { command: cli.command.clone(), }; - let interval = - humantime::format_duration(cli.interval.to_std().unwrap_or_default()).to_string(); + let interval = cli.interval.to_std().unwrap_or_default(); let command = cli.command.join(" "); - store.set_runtime_config(StoreRuntimeConfig { interval, command })?; + store.set_runtime_config(StoreRuntimeConfig { + interval: interval.as_millis() as u64, + command, + })?; runtime_config }; @@ -283,6 +283,27 @@ impl App { self.last_tick_key_events.drain(..); } Action::Quit => self.should_quit = true, + Action::IncreaseInterval => { + self.runtime_config.interval += Duration::milliseconds(500); + + self.store.set_runtime_config(StoreRuntimeConfig { + interval: self.runtime_config.interval.num_milliseconds() as u64, + command: self.runtime_config.command.join(" "), + })?; + } + Action::DecreaseInterval => { + let new_interval = + self.runtime_config.interval - Duration::milliseconds(500); + + if new_interval >= chrono::Duration::milliseconds(500) { + self.runtime_config.interval = new_interval; + + self.store.set_runtime_config(StoreRuntimeConfig { + interval: self.runtime_config.interval.num_milliseconds() as u64, + command: self.runtime_config.command.join(" "), + })?; + } + } Action::Suspend => self.should_suspend = true, Action::Resume => self.should_suspend = false, Action::Resize(w, h) => { diff --git a/src/components/home.rs b/src/components/home.rs index 381de70..6ed61d8 100644 --- a/src/components/home.rs +++ b/src/components/home.rs @@ -123,6 +123,12 @@ impl Component for Home { Action::SetTimemachineMode(timemachine_mode) => { self.set_timemachine_mode(timemachine_mode) } + Action::IncreaseInterval => { + self.interval_component.increase_interval(); + } + Action::DecreaseInterval => { + self.interval_component.decrease_interval(); + } Action::SetNoTitle(is_no_title) => self.is_no_title = is_no_title, _ => {} } diff --git a/src/components/interval.rs b/src/components/interval.rs index 44f5c78..4c7b070 100644 --- a/src/components/interval.rs +++ b/src/components/interval.rs @@ -26,6 +26,18 @@ impl Interval { config: Config::new().unwrap(), } } + + pub fn increase_interval(&mut self) { + self.runtime_config.interval += chrono::Duration::milliseconds(500); + } + + 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; + } + } } impl Component for Interval { diff --git a/src/config.rs b/src/config.rs index 927dccb..0865f1a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -218,6 +218,8 @@ impl From for Config { insert_keybinding(keymap.scroll_page_down, Action::ResultPageDown); insert_keybinding(keymap.scroll_top_of_page, Action::TopOfPage); insert_keybinding(keymap.scroll_bottom_of_page, Action::BottomOfPage); + insert_keybinding(keymap.increase_interval, Action::IncreaseInterval); + insert_keybinding(keymap.decrease_interval, Action::DecreaseInterval); keybindings.insert(Mode::All, all_keybindings); diff --git a/src/old_config.rs b/src/old_config.rs index c9c142b..cb3324d 100644 --- a/src/old_config.rs +++ b/src/old_config.rs @@ -41,6 +41,8 @@ pub struct Keymap { pub scroll_page_down: Option, pub scroll_bottom_of_page: Option, pub scroll_top_of_page: Option, + pub increase_interval: Option, + pub decrease_interval: Option, } #[derive(Debug, Serialize, Deserialize, Default)] diff --git a/src/runner.rs b/src/runner.rs index 7f68d60..73ee78e 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -86,7 +86,12 @@ pub async fn run_executor( eprintln!("Failed to send result: {:?}", e); } - tokio::time::sleep(runtime_config.interval.to_std().unwrap()).await; + let interval = store + .get_runtime_config()? + .map(|config| config.interval) + .unwrap_or(runtime_config.interval.num_milliseconds() as u64); + + tokio::time::sleep(std::time::Duration::from_millis(interval)).await; } } @@ -160,9 +165,19 @@ pub async fn run_executor_precise( } let elapased = chrono::Local::now().signed_duration_since(start_time); - let sleep_time = runtime_config.interval.sub(elapased); - if let Ok(sleep_time) = sleep_time.to_std() { - tokio::time::sleep(sleep_time).await; + + let interval = store + .get_runtime_config()? + .map(|config| config.interval) + .unwrap_or(runtime_config.interval.num_milliseconds() as u64); + + let interval = std::time::Duration::from_millis(interval); + + if let Ok(elapsed_std) = elapased.to_std() { + if elapsed_std < interval { + let sleep_time = interval - elapsed_std; + tokio::time::sleep(sleep_time).await; + } } } } diff --git a/src/store.rs b/src/store.rs index cb21760..a134f24 100644 --- a/src/store.rs +++ b/src/store.rs @@ -34,6 +34,6 @@ pub struct Record { #[derive(Debug, Clone, Default)] pub struct RuntimeConfig { - pub interval: String, + pub interval: u64, pub command: String, } From 86d4b1c206b03f419aae8f304e2d21e7f27a2430 Mon Sep 17 00:00:00 2001 From: Arpan Koirala Date: Sun, 22 Dec 2024 15:10:08 +0545 Subject: [PATCH 2/2] 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 {