Skip to content

Commit

Permalink
Add check on stopping on different days than starting
Browse files Browse the repository at this point in the history
  • Loading branch information
KuabeM committed Jan 14, 2024
1 parent 2a14ca7 commit 8afad76
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::{
io::{BufReader, Read, Write},
};

use crate::cli_input::YesNo;
use crate::errors::*;

use crate::storage::WorkStorage;
Expand Down Expand Up @@ -178,7 +179,24 @@ impl TimeBalance {
));
}
let breaks = self.accumulate_breaks();
let duration = time
let stop = if start.naive_local().date() != time.naive_local().date() {
println!(
"You started working on {}, do you really want to stop today? [y/N]",
start.format("%D.%M")
);
match YesNo::wait_for_decision()? {
YesNo::Yes => time,
YesNo::No => {
let time_stamp = time.time();
let date = start.naive_utc().date();
let stop = date.and_time(time_stamp);
stop.and_utc()
}
}
} else {
time
};
let duration = stop
.signed_duration_since(start)
.checked_sub(&breaks)
.ok_or_else(|| usage_err!("Your break was longer than your work"))?;
Expand Down
39 changes: 39 additions & 0 deletions src/cli_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/// Parsing and printing from and to cli.
use std::str::FromStr;

use crate::errors::*;

#[derive(Debug)]
pub enum YesNo {
Yes,
No,
}

impl FromStr for YesNo {
type Err = color_eyre::eyre::Error;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let lower = input.trim().to_lowercase();
match lower.as_str() {
"yes" | "y" => Ok(Self::Yes),
"no" | "n" => Ok(Self::No),
e => Err(eyre!("Failed to parse {} into 'yes' or 'no'", e)),
}
}
}

impl YesNo {
pub fn wait_for_decision() -> Result<Self> {
let yes = loop {
let mut input = String::new();
std::io::stdin()
.read_line(&mut input)
.wrap_err("Failed to read line from stdin")?;
if let Ok(yn) = crate::cli_input::YesNo::from_str(&input) {
log::trace!("Parsed {:?}", yn);
break yn;
}
};
Ok(yes)
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pub mod errors;

mod balance;
mod cli_input;
pub mod commands;
pub mod delta;
pub mod month;
Expand Down

0 comments on commit 8afad76

Please sign in to comment.