Change the default behavior for hours to 0 if discount is set
This commit is contained in:
parent
7a6aa2489b
commit
b6d1c0ea0c
5 changed files with 22 additions and 11 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -384,7 +384,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "time-track"
|
||||
version = "2.1.0"
|
||||
version = "2.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "time-track"
|
||||
version = "2.1.0"
|
||||
version = "2.1.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ use clap::Parser;
|
|||
#[command(author, version, about, long_about = None)]
|
||||
pub struct Args {
|
||||
/// How many hours you intend to work (sums with `minutes`)
|
||||
#[arg(long, default_value_t = 8)]
|
||||
pub hours: i64,
|
||||
/// Usually defaults to 8 hours, but if discount is set then it defaults to 0
|
||||
#[arg(long)]
|
||||
pub hours: Option<i64>,
|
||||
|
||||
/// How many minutes you intend to work (sums with `hours`)
|
||||
#[arg(long, default_value_t = 0)]
|
||||
|
|
|
|||
20
src/main.rs
20
src/main.rs
|
|
@ -11,6 +11,21 @@ use args::Args;
|
|||
fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
let stdin = io::stdin();
|
||||
let hours = args.hours.unwrap_or(
|
||||
if args.discount {
|
||||
0
|
||||
} else {
|
||||
8
|
||||
}
|
||||
);
|
||||
|
||||
let target_minutes = if args.discount {
|
||||
(8 * 60) - ((hours * 60) + args.minutes)
|
||||
} else {
|
||||
hours * 60 + args.minutes
|
||||
};
|
||||
let (hrs, mins) = time::to_hrs_minutes(target_minutes);
|
||||
println!("Working for {}", time::show_time(hrs, mins));
|
||||
println!("Input times one per line. Send an EOF character to finish inputting...");
|
||||
let mut lines: Vec<String> = vec![];
|
||||
for line in stdin.lock().lines() {
|
||||
|
|
@ -42,11 +57,6 @@ fn main() -> Result<()> {
|
|||
total_minutes += (now - remaining).num_minutes();
|
||||
}
|
||||
|
||||
let target_minutes = if args.discount {
|
||||
(8 * 60) - (args.hours * 60 + args.minutes)
|
||||
} else {
|
||||
args.hours * 60 + args.minutes
|
||||
};
|
||||
println!("{}", time::get_charaterized_time_remaining(total_minutes, target_minutes));
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ pub fn from_stream<'a>(reference_date: &DateTime<Local>, stream: impl Iterator<I
|
|||
return Ok(durations);
|
||||
}
|
||||
|
||||
fn show_time(hours: i64, minutes: i64) -> String {
|
||||
pub fn show_time(hours: i64, minutes: i64) -> String {
|
||||
let pluralized_hours = match hours {
|
||||
1 => "1 hour".to_string(),
|
||||
_ => format!("{hours} hours"),
|
||||
|
|
@ -48,7 +48,7 @@ fn show_time(hours: i64, minutes: i64) -> String {
|
|||
return format!("{pluralized_hours} and {pluralized_minutes}");
|
||||
}
|
||||
|
||||
fn to_hrs_minutes(total_minutes: i64) -> (i64, i64) {
|
||||
pub fn to_hrs_minutes(total_minutes: i64) -> (i64, i64) {
|
||||
let minutes = total_minutes % 60;
|
||||
let hours = total_minutes / 60;
|
||||
(hours, minutes)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue