Skip to content

Commit

Permalink
feat: made interval step and min interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
arpandaze committed Dec 22, 2024
1 parent b04641f commit 86d4b1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .config/config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@
"search_highlight": "black on yellow",
"readonly": "bold yellow"
}
},
"general": {
"min_interval_ms": 500,
"interval_step_ms": 500
}
}
22 changes: 12 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,27 @@ impl<S: Store> App<S> {
}
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,
command: self.runtime_config.command.join(" "),
})?;
}
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,
Expand Down
13 changes: 7 additions & 6 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 Down Expand Up @@ -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;
}
}

Expand Down
11 changes: 11 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

0 comments on commit 86d4b1c

Please sign in to comment.