use std::path::PathBuf;

use directories::BaseDirs;
use nickel_lang_core::repl::rustyline_frontend;

use crate::{color_opt_from_clap, global::GlobalContext};

#[derive(clap::Parser, Debug)]
pub struct ReplCommand {
    #[arg(long)]
    pub history_file: Option<PathBuf>,
}

impl ReplCommand {
    pub fn run(self, ctxt: &mut GlobalContext) {
        let histfile = self
            .history_file
            .or_else(|| BaseDirs::new().map(|bd| bd.home_dir().join(".nickel_history")));
        println!("Nickel {}", env!("CARGO_PKG_VERSION"));
        println!("Type \":help\" for help, or \":exit\" to exit");
        ctxt.reporter.report_result(rustyline_frontend::repl(
            histfile,
            color_opt_from_clap(ctxt.opts.color),
        ));
    }
}
