Skip to content

Commit

Permalink
feat: support for increasing and decreasing interval
Browse files Browse the repository at this point in the history
  • Loading branch information
arpandaze committed Dec 20, 2024
1 parent 1b7493d commit dbbec39
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 11 deletions.
2 changes: 2 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
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,
}
28 changes: 22 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,22 @@ 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(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) => {
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
8 changes: 8 additions & 0 deletions src/components/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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,
}

0 comments on commit dbbec39

Please sign in to comment.