Skip to content

Commit

Permalink
add new options
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Jun 24, 2023
1 parent 8834b30 commit 7f1beb9
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 21 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added option `preset` to get preconfigured timer. Available options: `pomodoro`, `52/17`.
- Added preset option `preset` to get preconfigured timer. Available options: `pomodoro`, `52/17`.
- Added preset option `cycles-count` to control how the timer loops. `0` means infinite, whereas any integer makes the timer stop automatically after n loops.
- Added preset option `timer-precision` to customize the timer format. Available options: `second`, `minute` (default), `hour`.

### Changed

Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dialoguer = "0.10.3"
dirs = "5.0.0"
env_logger = "0.10.0"
log = "0.4.17"
pimalaya-process = "0.0.1"
pimalaya-time = "0.0.1"
pimalaya-process = "=0.0.2"
pimalaya-time = "=0.0.2"
serde = "1.0.159"
toml = "0.7.3"
29 changes: 27 additions & 2 deletions src/client/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use anyhow::Result;
use pimalaya_time::{Client, TimerState};

use crate::{PresetConfig, TimerPrecision};

pub fn start(client: &dyn Client) -> Result<()> {
client.start()?;
Ok(())
}

pub fn get(client: &dyn Client) -> Result<()> {
pub fn get(preset: &PresetConfig, client: &dyn Client) -> Result<()> {
let timer = client.get()?;
let cycle = &timer.cycle.name;

Expand All @@ -16,7 +18,30 @@ pub fn get(client: &dyn Client) -> Result<()> {
TimerState::Running if timer.cycle.duration < 60 => {
println!("[{cycle}] {}s", timer.cycle.duration)
}
TimerState::Running => println!("[{cycle}] {}min", timer.cycle.duration / 60),
TimerState::Running if timer.cycle.duration < 3600 => match preset.timer_precision {
TimerPrecision::Second => println!(
"[{cycle}] {}min {}s",
timer.cycle.duration / 60,
timer.cycle.duration % 60
),
TimerPrecision::Minute | TimerPrecision::Hour => {
println!("[{cycle}] {}min", timer.cycle.duration / 60,)
}
},
TimerState::Running => match preset.timer_precision {
TimerPrecision::Second => println!(
"[{cycle}] {}h {}min {}s",
timer.cycle.duration / 3600,
(timer.cycle.duration % 3600) / 60,
(timer.cycle.duration % 3600) % 60,
),
TimerPrecision::Minute => println!(
"[{cycle}] {}h {}min",
timer.cycle.duration / 3600,
(timer.cycle.duration % 3600) / 60,
),
TimerPrecision::Hour => println!("[{cycle}] {}h", timer.cycle.duration / 3600),
},
};

Ok(())
Expand Down
30 changes: 21 additions & 9 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use anyhow::{anyhow, Context, Result};
use dirs::{config_dir, home_dir};
use log::{debug, trace};
use log::debug;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::PathBuf};
use toml;
Expand Down Expand Up @@ -33,7 +33,7 @@ impl Config {
None => return Err(anyhow!("cannot find config file")),
};

trace!("config: {:#?}", config);
debug!("comodoro config: {:#?}", config);
Ok(config)
}

Expand All @@ -42,9 +42,9 @@ impl Config {
pub fn get_preset(&self, name: &str) -> Result<PresetConfig> {
self.presets
.iter()
.find_map(|preset| {
if preset.0 == name {
Some(preset.1)
.find_map(|(preset_name, preset)| {
if preset_name == name {
Some(preset)
} else {
None
}
Expand Down Expand Up @@ -124,8 +124,11 @@ mod tests {
),
tcp: None,
hooks: HashMap::default(),
cycles_count: Default::default(),
timer_precision: Default::default(),
}
)])
)]),
..Default::default()
}
);
}
Expand All @@ -146,8 +149,11 @@ mod tests {
preset_or_cycles: PresetKindOrCyclesConfig::Preset(PresetKind::Preset52_17),
tcp: None,
hooks: HashMap::default(),
cycles_count: Default::default(),
timer_precision: Default::default(),
}
)])
)]),
..Default::default()
}
);
}
Expand Down Expand Up @@ -179,8 +185,11 @@ mod tests {
tcp: None,
// FIXME: preset is also captured by hooks, serde bug?
hooks: HashMap::default(),
cycles_count: Default::default(),
timer_precision: Default::default(),
}
)])
)]),
..Default::default()
}
);
}
Expand Down Expand Up @@ -211,8 +220,11 @@ mod tests {
(String::from("on-timer-start"), String::from("hook-1")),
(String::from("on-server-stop"), String::from("hook-2"))
]),
cycles_count: Default::default(),
timer_precision: Default::default(),
}
)])
)]),
..Default::default()
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub mod server;
pub use config::Config;
#[cfg(any(feature = "tcp-binder", feature = "tcp-client"))]
pub use config::TcpConfig;
pub use preset::{PresetConfig, PresetKind, PresetKindOrCyclesConfig};
pub use preset::{PresetConfig, PresetKind, PresetKindOrCyclesConfig, TimerPrecision};
pub use protocol::*;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn main() -> Result<()> {
Some(client::args::Cmd::Get(preset, protocol)) => {
let preset = config.get_preset(preset)?;
let client = protocol.to_client(&preset)?;
return client::handlers::get(client.as_ref());
return client::handlers::get(&preset, client.as_ref());
}
Some(client::args::Cmd::Pause(preset, protocol)) => {
let preset = config.get_preset(preset)?;
Expand Down
22 changes: 22 additions & 0 deletions src/preset/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ pub struct PresetConfig {
pub tcp: Option<TcpConfig>,
#[serde(flatten)]
pub hooks: HashMap<String, String>,
#[serde(default)]
pub cycles_count: usize,
#[serde(default)]
pub timer_precision: TimerPrecision,
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TimerPrecision {
#[serde(alias = "seconds")]
#[serde(alias = "sec")]
#[serde(alias = "s")]
Second,
#[default]
#[serde(alias = "minutes")]
#[serde(alias = "mins")]
#[serde(alias = "min")]
#[serde(alias = "m")]
Minute,
#[serde(alias = "hours")]
#[serde(alias = "h")]
Hour,
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Protocol {

impl Protocol {
pub fn to_server(config: &PresetConfig, protocols: Vec<&Protocol>) -> Result<Server> {
let mut server = ServerBuilder::new();
let mut server = ServerBuilder::new().with_cycles_count(config.cycles_count);

match &config.preset_or_cycles {
PresetKindOrCyclesConfig::Preset(PresetKind::PresetPomodoro) => {
Expand Down

0 comments on commit 7f1beb9

Please sign in to comment.