Add a prediction mode
This commit is contained in:
parent
6b300b3d0a
commit
98d5fbe232
2 changed files with 71 additions and 11 deletions
42
src/main.rs
42
src/main.rs
|
|
@ -4,16 +4,17 @@ use chrono::{NaiveTime, Duration, Local};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use atty::Stream;
|
use atty::Stream;
|
||||||
|
|
||||||
|
mod modes;
|
||||||
|
use modes::Modes;
|
||||||
|
|
||||||
/// A simple program to track time spans
|
/// A simple program to track time spans
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// When passed the script will collect time spans live. Close a span by pressing ENTER and
|
/// Specify how the program should behave
|
||||||
/// finish the calulation by passing in an EOF character
|
#[clap(value_enum, default_value_t = Modes::default())]
|
||||||
#[arg(short, long, default_value_t = false)]
|
mode: Modes,
|
||||||
live: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_time(time_str: &str) -> NaiveTime {
|
fn parse_time(time_str: &str) -> NaiveTime {
|
||||||
NaiveTime::parse_from_str(time_str, "%H:%M")
|
NaiveTime::parse_from_str(time_str, "%H:%M")
|
||||||
.expect(&format!("Failed to parse time from {time_str}"))
|
.expect(&format!("Failed to parse time from {time_str}"))
|
||||||
|
|
@ -75,18 +76,37 @@ fn live_spans() -> Vec<Duration> {
|
||||||
return durations;
|
return durations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_prediction() -> NaiveTime {
|
||||||
|
println!("Calculating an end time prediction...\nWhen did you start?\n");
|
||||||
|
let mut start_time = String::new();
|
||||||
|
let _ = stdin().read_line(&mut start_time);
|
||||||
|
let start = parse_time(start_time.trim());
|
||||||
|
println!("Started at {0}", start.format("%H:%M"));
|
||||||
|
println!("How many minutes were you on break?\n");
|
||||||
|
let mut break_time = String::new();
|
||||||
|
let _ = stdin().read_line(&mut break_time);
|
||||||
|
let break_duration = break_time.trim().parse::<i64>().unwrap();
|
||||||
|
let work_time = Duration::hours(8) + Duration::minutes(break_duration);
|
||||||
|
return start + work_time;
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let is_terminal = atty::is(Stream::Stdin);
|
let is_terminal = atty::is(Stream::Stdin);
|
||||||
|
|
||||||
if args.live && !is_terminal {
|
if !is_terminal && !args.mode.supports_piped_input() {
|
||||||
panic!("Cannot run live mode on piped input");
|
panic!("Cannot run {0} mode on piped input", args.mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
let durations = if args.live {
|
if args.mode == Modes::Prediction {
|
||||||
live_spans()
|
println!("Your work will end at {}", get_prediction());
|
||||||
} else {
|
return;
|
||||||
all_at_once(is_terminal)
|
}
|
||||||
|
|
||||||
|
let durations = match args.mode {
|
||||||
|
Modes::TimeTable => all_at_once(is_terminal),
|
||||||
|
Modes::Live => live_spans(),
|
||||||
|
Modes::Prediction => panic!("Should have already returned before this"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let total_minutes: i64 = durations.iter().map(|d| { d.num_minutes() }).sum();
|
let total_minutes: i64 = durations.iter().map(|d| { d.num_minutes() }).sum();
|
||||||
|
|
|
||||||
40
src/modes.rs
Normal file
40
src/modes.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
use clap::ValueEnum;
|
||||||
|
|
||||||
|
#[derive(ValueEnum, Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
pub enum Modes {
|
||||||
|
/// Perform calculations by treating stdin as a stream of time pairs separated by newlines
|
||||||
|
TimeTable,
|
||||||
|
/// Perform live tracking of the user's time until an EOF character is received.
|
||||||
|
Live,
|
||||||
|
/// Predict a user's day end time (assuming a 9 hour work day) based on the user's start time
|
||||||
|
/// and the duration of a user's break.
|
||||||
|
Prediction,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Modes {
|
||||||
|
/// Determine whether or not the variant supports piped input vs being used as a CLI tool
|
||||||
|
/// directly by a human
|
||||||
|
pub fn supports_piped_input(&self) -> bool {
|
||||||
|
match self {
|
||||||
|
Modes::TimeTable => true,
|
||||||
|
Modes::Live => false,
|
||||||
|
Modes::Prediction => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Default for Modes {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::TimeTable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Modes {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.to_possible_value()
|
||||||
|
.expect("no values are skipped")
|
||||||
|
.get_name()
|
||||||
|
.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue