generated from PlexSheep/rs-base
feat: allow input of duration in seconds and minutes too
Some checks failed
cargo devel CI / cargo CI (push) Failing after 1m26s
Some checks failed
cargo devel CI / cargo CI (push) Failing after 1m26s
This commit is contained in:
parent
128cc3621e
commit
8c8c4f28c0
2 changed files with 21 additions and 9 deletions
|
@ -20,6 +20,7 @@ desktop = ["dep:notify-rust"]
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
|
humantime = "2.1.0"
|
||||||
libpt = { version = "0.6.0", features = ["cli"] }
|
libpt = { version = "0.6.0", features = ["cli"] }
|
||||||
notify-rust = { version = "4.11.0", optional = true }
|
notify-rust = { version = "4.11.0", optional = true }
|
||||||
ratatui = "0.27.0"
|
ratatui = "0.27.0"
|
||||||
|
|
29
src/clock.rs
29
src/clock.rs
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
use chrono::{DateTime, Local, SubsecRound, Timelike};
|
use chrono::{DateTime, Local, SubsecRound, Timelike};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use humantime;
|
||||||
use libpt::cli::args::HELP_TEMPLATE;
|
use libpt::cli::args::HELP_TEMPLATE;
|
||||||
use libpt::cli::clap::ArgGroup;
|
use libpt::cli::clap::ArgGroup;
|
||||||
use libpt::cli::{args::VerbosityLevel, clap};
|
use libpt::cli::{args::VerbosityLevel, clap};
|
||||||
|
@ -47,7 +48,7 @@ impl Default for TimeBarLength {
|
||||||
/// Make your terminal into a big clock
|
/// Make your terminal into a big clock
|
||||||
#[derive(Parser, Debug, Clone)]
|
#[derive(Parser, Debug, Clone)]
|
||||||
#[command(help_template = HELP_TEMPLATE, author, version)]
|
#[command(help_template = HELP_TEMPLATE, author, version)]
|
||||||
#[clap(group( ArgGroup::new("timebarlen") .args(&["minute","day", "hour", "custom", "count_up"]),))]
|
#[clap(group( ArgGroup::new("timebarlen") .args(&["minute","day", "hour", "custom", "countdown"]),))]
|
||||||
#[allow(clippy::struct_excessive_bools)] // the struct is for cli parsing and we already use an
|
#[allow(clippy::struct_excessive_bools)] // the struct is for cli parsing and we already use an
|
||||||
// ArgGroup
|
// ArgGroup
|
||||||
pub struct Clock {
|
pub struct Clock {
|
||||||
|
@ -58,16 +59,23 @@ pub struct Clock {
|
||||||
pub timer: bool,
|
pub timer: bool,
|
||||||
|
|
||||||
// timebar options
|
// timebar options
|
||||||
|
/// show a time bar that tracks progress of the minute
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub minute: bool,
|
pub minute: bool,
|
||||||
|
/// show a time bar that tracks progress of the day
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub day: bool,
|
pub day: bool,
|
||||||
|
/// show a time bar that tracks progress of the hour
|
||||||
#[clap(short = 'o', long)]
|
#[clap(short = 'o', long)]
|
||||||
pub hour: bool,
|
pub hour: bool,
|
||||||
#[clap(short, long)]
|
/// show a time bar that tracks progress of a custom duration
|
||||||
pub custom: Option<i64>,
|
#[clap(short, long, value_parser = humantime::parse_duration)]
|
||||||
#[clap(short = 'u', long)]
|
pub custom: Option<std::time::Duration>,
|
||||||
pub count_up: Option<i64>,
|
/// show a time bar that tracks progress of a custom duration without resetting
|
||||||
|
#[clap(short = 'u', long, value_parser = humantime::parse_duration)]
|
||||||
|
pub countdown: Option<std::time::Duration>,
|
||||||
|
|
||||||
|
// internal variables
|
||||||
#[clap(skip)]
|
#[clap(skip)]
|
||||||
pub(crate) last_reset: Option<DateTime<Local>>,
|
pub(crate) last_reset: Option<DateTime<Local>>,
|
||||||
#[clap(skip)]
|
#[clap(skip)]
|
||||||
|
@ -134,11 +142,14 @@ impl Clock {
|
||||||
Some(TimeBarLength::Day)
|
Some(TimeBarLength::Day)
|
||||||
} else if self.hour {
|
} else if self.hour {
|
||||||
Some(TimeBarLength::Hour)
|
Some(TimeBarLength::Hour)
|
||||||
} else if self.count_up.is_some() {
|
} else if self.countdown.is_some() {
|
||||||
self.count_up.map(TimeBarLength::Countup)
|
Some(TimeBarLength::Countup(
|
||||||
|
self.countdown.unwrap().as_secs() as i64
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
// this feels weird but is the same
|
Some(TimeBarLength::Custom(
|
||||||
self.custom.map(TimeBarLength::Custom)
|
self.countdown.unwrap().as_secs() as i64
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue