use anyhow::{Result, anyhow};
use clap::Parser;
use colored::Colorize;
use time::macros::format_description;
use time::{OffsetDateTime, UtcOffset};
use crate::store::{Store, TimeInterval};
#[derive(Debug, Parser)]
#[command(help_template = crate::HELP_TEMPLATE_OPT, styles = crate::STYLES)]
pub struct CommandOut {}
impl CommandOut {
#[allow(clippy::unused_self)]
pub fn execute(self) -> Result<()> {
let store = Store::new()?;
match store.get_current_task()? {
None => {
println!("No current task");
}
Some(current) => {
let start = OffsetDateTime::from_unix_timestamp(current.start)
.map_err(|e| anyhow!("invalid start timestamp in current task: {e}"))?;
let end = OffsetDateTime::now_utc().truncate_to_second();
let interval = TimeInterval::new(¤t.task, start, end);
store.append_interval(interval)?;
store.set_last_task(¤t.task)?;
store.clear_current_task()?;
let offset = UtcOffset::current_local_offset().unwrap();
let fmt = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
let local_start = start.to_offset(offset).format(fmt).unwrap();
let local_end = end.to_offset(offset).format(fmt).unwrap();
println!(
"Punched out of {}: {local_start} - {local_end}",
current.task.green()
);
}
}
Ok(())
}
}