feat: better durations and allow stopping pomodoro

man-page
Moritz Böhme 2023-07-29 12:52:50 +02:00
parent 103c6d779e
commit 0c954962cc
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
7 changed files with 81 additions and 49 deletions

7
Cargo.lock generated
View File

@ -562,6 +562,12 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "indexmap"
version = "2.0.0"
@ -1117,6 +1123,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"humantime",
"notify-rust",
"serde",
"serde_cbor",

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.71"
clap = { version = "4.3.4", features = ["derive"] }
humantime = "2.1.0"
notify-rust = "4.8.0"
serde = { version = "1.0.164", features = ["derive"] }
serde_cbor = "0.11.2"

View File

@ -3,6 +3,7 @@ use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
use std::time::Duration;
#[derive(Debug, Parser)]
#[command(name = "timers")]
@ -24,22 +25,29 @@ pub enum Command {
},
Add {
name: String,
duration_seconds: u64,
duration: humantime::Duration,
},
List,
Remove {
name: String,
},
Pomodoro {
#[clap(default_value_t = 25)]
work_minutes: u64,
#[clap(default_value_t = 5)]
pause_minutes: u64,
#[clap(default_value_t = 10)]
long_pause_minutes: u64,
#[command(subcommand)]
Pomodoro(PomodoroCommand)
}
#[derive(Debug, Subcommand)]
pub enum PomodoroCommand {
Start {
#[clap(default_value_t = Duration::from_secs(25 * 60).into())]
work: humantime::Duration,
#[clap(default_value_t = Duration::from_secs(5 * 60).into())]
pause: humantime::Duration,
#[clap(default_value_t = Duration::from_secs(10 * 60).into())]
long_pause: humantime::Duration,
#[clap(default_value_t = 3)]
pauses_till_long: u64,
}
},
Stop,
}
fn get_stream(socket_path: &String) -> Result<UnixStream> {

View File

@ -16,12 +16,13 @@ pub enum Command {
Add(Box<str>, Duration),
Remove(Box<str>),
List,
Pomodoro {
PomodoroStart {
work: Duration,
pause: Duration,
long_pause: Duration,
pauses_till_long: u64,
},
PomodoroStop
}
#[derive(Debug, Serialize, Deserialize)]
@ -100,7 +101,14 @@ impl Daemon {
if self.notify {
match Notification::new()
.summary("󰀠 Timers")
.body(format!("Started timer {} for {:?}", &name, duration).as_str())
.body(
format!(
"Started timer {} for {}",
&name,
humantime::format_duration(duration)
)
.as_str(),
)
.show()
{
Ok(_) => println!("Sent notification sucessfully."),
@ -120,7 +128,7 @@ impl Daemon {
.retain(|other| other.name.as_ref() != name.as_ref());
Ok(Answer::Ok)
}
Command::Pomodoro {
Command::PomodoroStart {
work,
pause,
long_pause,
@ -137,6 +145,10 @@ impl Daemon {
self.pomodoro = Some(Pomodoro::new(work, pause, long_pause, pauses_till_long));
Ok(Answer::Ok)
}
Command::PomodoroStop => {
self.pomodoro = None;
Ok(Answer::Ok)
},
}
}

View File

@ -3,34 +3,34 @@ pub mod daemon;
pub mod pomodoro;
pub mod timer;
use std::time::Duration;
use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon};
use anyhow::Result;
use clap::Parser;
use cli::PomodoroCommand;
fn main() -> Result<()> {
let args = Cli::parse();
let daemon_command = match args.command {
CliCommand::Daemon { notify } => return Daemon::new(args.socket, notify)?.run(),
CliCommand::Add {
name,
duration_seconds,
} => DaemonCommand::Add(name.into_boxed_str(), Duration::from_secs(duration_seconds)),
CliCommand::Add { name, duration } => {
DaemonCommand::Add(name.into_boxed_str(), duration.into())
}
CliCommand::List => DaemonCommand::List,
CliCommand::Remove { name } => DaemonCommand::Remove(name.into_boxed_str()),
CliCommand::Pomodoro {
work_minutes,
pause_minutes,
long_pause_minutes,
CliCommand::Pomodoro(pomodoro) => match pomodoro {
PomodoroCommand::Start {
work,
pause,
long_pause,
pauses_till_long,
} => DaemonCommand::Pomodoro{
work: Duration::from_secs(work_minutes * 60),
pause: Duration::from_secs(pause_minutes * 60),
long_pause: Duration::from_secs(long_pause_minutes * 60),
pauses_till_long
} => DaemonCommand::PomodoroStart {
work: work.into(),
pause: pause.into(),
long_pause: long_pause.into(),
pauses_till_long,
},
PomodoroCommand::Stop => DaemonCommand::PomodoroStop,
},
};
send_command(&args.socket, daemon_command)

View File

@ -19,12 +19,12 @@ impl Display for Pomodoro {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Pomodoro ({:?}, {:?}, {:?}) currently {} with {:?} remaining.",
self.work,
self.pause,
self.long_pause,
"Pomodoro ({}, {}, {}) currently {} with {} remaining.",
humantime::format_duration(self.work),
humantime::format_duration(self.pause),
humantime::format_duration(self.long_pause),
self.status,
self.timer.remaining()
humantime::format_duration(self.timer.remaining())
)
}
}
@ -33,13 +33,15 @@ impl Display for Pomodoro {
enum Status {
Working,
Pausing,
LongPause,
}
impl Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Working => write!(f, "working"),
Status::Pausing => write!(f, "pausing"),
Status::Working => write!(f, "pomodoro work"),
Status::Pausing => write!(f, "pomodoro pause"),
Status::LongPause => write!(f, "pomodoro long pause"),
}
}
}
@ -65,7 +67,6 @@ impl Pomodoro {
pub fn handle_expiration(&mut self, notify: bool) {
self.timer.handle_expiration(notify);
let duration = match self.status {
Status::Pausing => self.work,
Status::Working => {
if self.pauses == self.pauses_till_long {
self.long_pause
@ -73,13 +74,14 @@ impl Pomodoro {
self.pause
}
}
_ => self.work,
};
self.status = match self.status {
Status::Working => {
self.pauses += 1;
Status::Pausing
}
Status::Pausing => Status::Working,
_ => Status::Working,
};
self.timer = Timer::new(self.status.to_string().into_boxed_str(), duration);
}

View File

@ -44,9 +44,9 @@ impl Display for Timer {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(
f,
"{} has {}s remaining.",
"{} has {} remaining.",
self.name,
self.remaining().as_secs()
humantime::format_duration(self.remaining())
)
}
}
@ -64,8 +64,10 @@ impl Timer {
Instant::now() - self.start > self.duration
}
/// Returns the remaining duration rounded to seconds of this [`Timer`].
pub fn remaining(&self) -> Duration {
self.duration - (Instant::now() - self.start)
let exact = self.duration - (Instant::now() - self.start);
Duration::from_secs(exact.as_secs())
}
pub fn handle_expiration(&self, notify: bool) {