jobber 0.1.2-alpha

Minimalistic console work time tracker
use crate::{
    commands::{Cli, Config, Result, edit::edit_worker},
    jobber::JobberCreate,
    print_tip,
    views::WorkerView,
};
use clap::Parser;
use color_print::cprintln;
use inquire::Confirm;

/// Create a new worker.
#[derive(Parser, Debug)]
pub(crate) struct CreateWorker {
    /// Workers's acronym
    id: String,
}

impl CreateWorker {
    pub(crate) fn run(&self, cli: &Cli) -> Result<()> {
        let config = Config::load()?;
        let mut database = cli.open_database()?;
        loop {
            if !database.create_worker(&self.id, edit_worker)? {
                cprintln!("Nothing changed.");
                return Ok(());
            }
            println!();
            println!(
                "{}",
                WorkerView::new(&self.id, &config, &database, cli.ansi())?
            );
            match Confirm::new("Is this information correct?")
                .with_default(true)
                .with_help_message("cancel create with ESC")
                .prompt_skippable()?
            {
                Some(true) => {
                    cprintln!("Successfully edited worker.");
                    print_tip!(cli);
                    print_tip!(cli, "Use <s>jobber show worker</> to see the worker data.");
                    print_tip!(
                        cli,
                        "Use <s>jobber create job</> to add a job for this worker."
                    );
                    print_tip!(cli, "Use <s>jobber undo</> to undo the change.");
                    cli.close_database(database)?;
                    return Ok(());
                }
                Some(false) => (),
                None => {
                    cprintln!("Database remained unchanged.");
                    return Ok(());
                }
            }
        }
    }
}