Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for increasing and decreasing interval #168

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"</>": "EnterSearchMode",
"<t>": "SwitchNoTitle",
"<?>": "ShowHelp",
"<+>": "IncreaseInterval",
"<->": "DecreaseInterval",
},
"Search": {
"<Ctrl-d>": "Quit", // Another way to quit
Expand Down Expand Up @@ -68,5 +70,9 @@
"search_highlight": "black on yellow",
"readonly": "bold yellow"
}
},
"general": {
"min_interval_ms": 500,
"interval_step_ms": 500
}
}
2 changes: 2 additions & 0 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ pub enum Action {
UpdateLatestHistoryCount,
ShowHelp,
ExitHelp,
IncreaseInterval,
DecreaseInterval,
}
35 changes: 29 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ impl<S: Store> App<S> {
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(' ')
Expand All @@ -76,10 +74,12 @@ impl<S: Store> App<S> {
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
};
Expand Down Expand Up @@ -283,6 +283,29 @@ impl<S: Store> App<S> {
self.last_tick_key_events.drain(..);
}
Action::Quit => self.should_quit = true,
Action::IncreaseInterval => {
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,
command: self.runtime_config.command.join(" "),
})?;
}
Action::DecreaseInterval => {
let min_interval =
Duration::milliseconds(self.config.general.min_interval_ms);
let step = Duration::milliseconds(self.config.general.interval_step_ms);

let new_interval = (self.runtime_config.interval - step).max(min_interval);

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,
Action::Resize(w, h) => {
Expand Down
6 changes: 6 additions & 0 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
_ => {}
}
Expand Down
13 changes: 13 additions & 0 deletions src/components/interval.rs
Original file line number Diff line number Diff line change
@@ -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::*};
Expand All @@ -26,6 +27,18 @@ impl Interval {
config: Config::new().unwrap(),
}
}

pub fn increase_interval(&mut self) {
self.runtime_config.interval +=
chrono::Duration::milliseconds(self.config.general.interval_step_ms);
}

pub fn decrease_interval(&mut self) {
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;
}
}

impl Component for Interval {
Expand Down
13 changes: 13 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct General {
pub skip_empty_diffs: Option<bool>,
#[serde(default)]
pub disable_mouse: Option<bool>,
#[serde(default)]
pub min_interval_ms: i64,
#[serde(default)]
pub interval_step_ms: i64,
}

impl From<OldGeneral> for General {
Expand All @@ -56,6 +60,7 @@ impl From<OldGeneral> for General {
shell_options: value.shell_options,
skip_empty_diffs: value.skip_empty_diffs,
disable_mouse: value.disable_mouse,
..Default::default()
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -218,6 +229,8 @@ impl From<OldConfig> 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);

Expand Down
2 changes: 2 additions & 0 deletions src/old_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Keymap {
pub scroll_page_down: Option<String>,
pub scroll_bottom_of_page: Option<String>,
pub scroll_top_of_page: Option<String>,
pub increase_interval: Option<String>,
pub decrease_interval: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Default)]
Expand Down
23 changes: 19 additions & 4 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ pub async fn run_executor<S: Store>(
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;
}
}

Expand Down Expand Up @@ -160,9 +165,19 @@ pub async fn run_executor_precise<S: Store>(
}

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;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ pub struct Record {

#[derive(Debug, Clone, Default)]
pub struct RuntimeConfig {
pub interval: String,
pub interval: u64,
pub command: String,
}
Loading