From dbbec398f47cf46fa14cf4f977deb42ee0abeb09 Mon Sep 17 00:00:00 2001 From: Arpan Koirala Date: Fri, 20 Dec 2024 14:15:47 +0545 Subject: [PATCH] feat: support for increasing and decreasing interval --- .config/config.json5 | 2 ++ src/action.rs | 2 ++ src/app.rs | 28 ++++++++++++++++++++++------ src/components/home.rs | 6 ++++++ src/components/interval.rs | 8 ++++++++ src/config.rs | 2 ++ src/old_config.rs | 2 ++ src/runner.rs | 23 +++++++++++++++++++---- src/store.rs | 2 +- 9 files changed, 64 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..c648f41 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,22 @@ 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 => { + 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::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..c570943 100644 --- a/src/components/interval.rs +++ b/src/components/interval.rs @@ -26,6 +26,14 @@ 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) { + self.runtime_config.interval -= chrono::Duration::milliseconds(500); + } } 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, }