Skip to content

Commit

Permalink
Add --resume flag for start command
Browse files Browse the repository at this point in the history
  • Loading branch information
jkallio committed Jan 20, 2024
1 parent ef7a94f commit 099ee7a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options for `start`:
- `--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)
- `--resume` Resume a paused timer (default: disabled)

### Start/Stop the timer

Expand All @@ -59,6 +60,9 @@ $ pomodoro-cli stop

# Pause the Timer (calling this command again will resume the timer)
$ pomodoro-cli pause

# Resume a paused timer
$ pomodoro-cli start --resume
```

### Add more time to a running timer
Expand Down Expand Up @@ -92,7 +96,7 @@ Add the following module to your waybar configuration:
"format": "  {}",
"exec": "pomodoro-cli status --format json",
"return-type": "json",
"on-click": "pomodoro-cli start --duration 5m --notify",
"on-click": "pomodoro-cli start --duration 5m --add --notify",
"on-click-middle": "pomodoro-cli pause",
"on-click-right": "pomodoro-cli stop",
"interval": 1
Expand Down
32 changes: 27 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ pub fn run(args: &Cli) -> AppResult<()> {
notify,
wait,
add,
resume,
} => {
start_timer(parse_duration(duration.clone()), *silent, *notify, *add)?;
start_timer(
parse_duration(duration.clone()),
*silent,
*notify,
*add,
*resume,
)?;
if *wait {
wait_for_timer()?;
}
Expand All @@ -39,14 +46,28 @@ 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, add: bool) -> AppResult<()> {
pub fn start_timer(
duration: i64,
silent: bool,
notify: bool,
add: bool,
resume: bool,
) -> AppResult<()> {
let mut timer_info = TimerInfo::from_file_or_default()?;
if add && timer_info.is_running() {
if timer_info.is_running() && add {
timer_info.duration += duration;
} else if timer_info.is_paused() && resume {
let now = chrono::Utc::now().timestamp();
let elapsed = timer_info.pause_time - timer_info.start_time;
timer_info.duration = timer_info.duration - elapsed;
timer_info.start_time = now;
timer_info.pause_time = now;
timer_info.silent = timer_info.silent || silent;
timer_info.notify = timer_info.notify || notify;
timer_info.state = TimerState::Running;
} 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.duration = duration;
timer_info.start_time = now;
timer_info.pause_time = now;
timer_info.silent = silent;
Expand All @@ -71,6 +92,7 @@ pub fn pause_timer() -> AppResult<()> {
timer_info.silent,
timer_info.notify,
false,
true,
)?;
}
Ok(())
Expand Down
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub enum SubCommand {

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

#[arg(long, default_value_t = false, help = "Resume paused timer")]
resume: bool,
},
/// Stop the timer
Stop,
Expand Down
5 changes: 5 additions & 0 deletions src/timer_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ impl TimerInfo {
self.state == TimerState::Running
}

/// Return true if the timer is in `Paused` state
pub fn is_paused(&self) -> bool {
self.state == TimerState::Paused
}

pub fn is_time_run_out(&self) -> bool {
self.get_time_left() <= 0
}
Expand Down

0 comments on commit 099ee7a

Please sign in to comment.