Skip to content

Commit

Permalink
Add --add argument for start command to add more time to a running timer
Browse files Browse the repository at this point in the history
  • Loading branch information
jkallio committed Jan 20, 2024
1 parent 011d2b2 commit ef7a94f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pomodoro-cli"
version = "1.2.1"
version = "1.2.2"
authors = ["Jussi Kallio <jkallio@gmail.com>"]
license = "MIT"
description = "A simple command line Pomodoro timer."
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Pomodoro timer is a simple timer that helps you to stay focused on your tasks.

### Download binary

- [pomodoro cli (v.1.2.1)](https://github.com/jkallio/pomodoro-cli/releases/tag/v1.2.1)
- [pomodoro cli (latest release)](https://github.com/jkallio/pomodoro-cli/releases/latest)

### Cargo

Expand All @@ -43,6 +43,7 @@ Options for `start`:
- `--notify` Triggers system notification when the timer is finished (default: disabled)
- `--silent` Do not play alarm sound when the timer is finished (default: enabled)
- `--wait` Wait for the timer to finish (default: disabled)
- `--add` Add more time to a running timer instead of starting a new timer (default: disabled)

### Start/Stop the timer

Expand All @@ -63,8 +64,8 @@ $ pomodoro-cli pause
### Add more time to a running timer

```bash
# Call start again while the timer is running to add more time to the timer
$ pomodoro-cli start --duration 5m
# Add 10 minutes to the timer (instead of starting a new timer)
$ pomodoro-cli start -d 10 --add
```

### Query the timer status
Expand Down
19 changes: 13 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ pub fn run(args: &Cli) -> AppResult<()> {
silent,
notify,
wait,
add,
} => {
start_timer(parse_duration(duration.clone()), *silent, *notify)?;
start_timer(parse_duration(duration.clone()), *silent, *notify, *add)?;
if *wait {
wait_for_timer()?;
}
Expand All @@ -38,15 +39,16 @@ pub fn run(args: &Cli) -> AppResult<()> {
}

/// Start the timer. If the timer is already running, the duration is added to the current duration.
pub fn start_timer(duration: i64, silent: bool, notify: bool) -> AppResult<()> {
pub fn start_timer(duration: i64, silent: bool, notify: bool, add: bool) -> AppResult<()> {
let mut timer_info = TimerInfo::from_file_or_default()?;
if timer_info.is_running() {
if add && timer_info.is_running() {
timer_info.duration += duration;
} else {
let now = chrono::Utc::now().timestamp() + 1;
let elapsed = timer_info.pause_time - timer_info.start_time;
timer_info.duration = duration - elapsed;
timer_info.start_time = chrono::Utc::now().timestamp();
timer_info.pause_time = timer_info.start_time;
timer_info.start_time = now;
timer_info.pause_time = now;
timer_info.silent = silent;
timer_info.notify = notify;
timer_info.state = TimerState::Running;
Expand All @@ -64,7 +66,12 @@ pub fn pause_timer() -> AppResult<()> {
timer_info.state = TimerState::Paused;
timer_info.write_to_file()?;
} else {
start_timer(timer_info.duration, timer_info.silent, timer_info.notify)?;
start_timer(
timer_info.duration,
timer_info.silent,
timer_info.notify,
false,
)?;
}
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum SubCommand {

#[arg(long, default_value_t = false, help = "Wait for the timer to finish")]
wait: bool,

#[arg(long, default_value_t = false, help = "Add more time to the timer")]
add: bool,
},
/// Stop the timer
Stop,
Expand Down

0 comments on commit ef7a94f

Please sign in to comment.