Skip to content

Commit

Permalink
Merge pull request #5 from rameloni/main
Browse files Browse the repository at this point in the history
Add automatic lockscreen option when the timer runs out
  • Loading branch information
jkallio authored Mar 26, 2024
2 parents 6cce30b + 1324be0 commit 2143bda
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.idea
9 changes: 8 additions & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "pomodoro-cli"
version = "1.2.4"
version = "1.2.5"
authors = ["Jussi Kallio <jkallio@gmail.com>"]
license = "MIT"
description = "A simple command line Pomodoro timer."
Expand All @@ -23,7 +23,7 @@ dirs = "5.0.1"
clap = { version = "4.4.16", features = ["derive", "cargo"] }
rodio = "0.17.3"
crossterm = "0.27.0"

lock = "0.1.0"

[profile.release]
strip = true # Automatically strip symbols from the binary.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ $ cargo install pomodoro-cli
- [x] Play alarm sound when the Timer is finished
- [x] Easy Waybar integration
- [x] Customize notification icon and alarm sound
- [x] Allow lock screen when the timer ran out

# Usage

Expand All @@ -47,6 +48,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)
- `--lock-screen` Wait for the timer to finish and lock the screen once the timer is finished (default: disabled)

### Start/Stop the timer

Expand Down
32 changes: 31 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::utils::*;
use crossterm::cursor::{MoveToColumn, MoveToPreviousLine};
use crossterm::execute;
use crossterm::terminal::{Clear, ClearType};
use lock::FailureReason;
use notify_rust::{Notification, Timeout};
use rodio::{Decoder, OutputStream, Sink};
use std::thread;
Expand All @@ -22,6 +23,7 @@ pub fn run(args: &Cli) -> AppResult<()> {
notify,
wait,
resume,
lock_screen,
} => {
start_timer(
parse_duration(duration.clone()),
Expand All @@ -30,8 +32,9 @@ pub fn run(args: &Cli) -> AppResult<()> {
*silent,
*notify,
*resume,
*lock_screen,
)?;
if *wait {
if *wait || *lock_screen {
wait_for_timer()?;
}
}
Expand Down Expand Up @@ -60,6 +63,7 @@ pub fn start_timer(
silent: bool,
notify: bool,
resume: bool,
lock_screen: bool,
) -> AppResult<()> {
let mut timer_info = TimerInfo::from_file_or_default()?;
if timer_info.is_running() && add.is_some() {
Expand All @@ -75,6 +79,7 @@ pub fn start_timer(
timer_info.message = timer_info.message.clone();
timer_info.silent = timer_info.silent || silent;
timer_info.notify = timer_info.notify || notify;
timer_info.lock_screen = timer_info.lock_screen || lock_screen;
timer_info.state = TimerState::Running;
} else {
// Start a new timer
Expand All @@ -87,6 +92,7 @@ pub fn start_timer(
timer_info.silent = silent;
timer_info.notify = notify;
timer_info.state = TimerState::Running;
timer_info.lock_screen = lock_screen;
}
timer_info.write_to_file()?;
Ok(())
Expand All @@ -103,6 +109,7 @@ pub fn pause_timer() -> AppResult<()> {
timer_info.silent,
timer_info.notify,
true,
timer_info.lock_screen,
)?;
} else if timer_info.is_running() {
let now = chrono::Utc::now().timestamp();
Expand All @@ -121,6 +128,23 @@ pub fn stop_timer() -> AppResult<()> {
Ok(())
}

/// Lock the screen.
fn lock_screen() -> AppResult<()> {
println!("Locking screen...");

lock::lock().map_err(|fail| {
AppError::new(match fail {
FailureReason::CannotExecute => "Cannot execute the lock command.",
FailureReason::LinuxCommandNotFound => {
"Linux command not found. The following commands are supported\
\n- xdg-screensaver\
\n- gnome-screensaver\
\n- dm-tool"
}
})
})
}

/// Trigger the alarm sound and/or the system notification.
pub fn trigger_alarm(timer_info: &TimerInfo) -> AppResult<()> {
println!("Time is up!");
Expand Down Expand Up @@ -155,6 +179,12 @@ pub fn trigger_alarm(timer_info: &TimerInfo) -> AppResult<()> {
sink.sleep_until_end();
sink.clear();
}

// Now check if the lock screen option is enabled
if timer_info.lock_screen {
lock_screen()?;
}

return Ok(());
}

Expand Down
7 changes: 7 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ pub enum SubCommand {

#[arg(long, default_value_t = false, help = "Resume paused timer")]
resume: bool,

#[arg(
long,
default_value_t = false,
help = "Lock the screen when the timer finishes"
)]
lock_screen: bool,
},
/// Stop the timer
Stop,
Expand Down
2 changes: 2 additions & 0 deletions src/timer_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct TimerInfo {
pub silent: bool,
pub notify: bool,
pub wait: bool,
pub lock_screen: bool,
}

#[derive(Serialize)]
Expand All @@ -50,6 +51,7 @@ impl Default for TimerInfo {
silent: false,
notify: false,
wait: false,
lock_screen: false,
}
}
}
Expand Down

0 comments on commit 2143bda

Please sign in to comment.