use std::str::FromStr;
use clap::Parser;
use crate::commands::*;
use microcad_driver::prelude as mu;
#[derive(Parser)]
#[command(version, about, long_about = None)]
pub struct Cli {
#[arg(short = 'T', long, default_value = "false", action = clap::ArgAction::SetTrue)]
pub(crate) time: bool,
#[arg(short = 'C', long = "config")]
config_path: Option<std::path::PathBuf>,
#[arg(short, action = clap::ArgAction::Count)]
pub(crate) verbose: u8,
#[command(subcommand)]
command: Commands,
#[clap(skip)]
pub config: std::rc::Rc<mu::Config>,
}
impl Cli {
pub fn new() -> miette::Result<Self> {
let mut cli = Self::parse();
#[cfg(not(debug_assertions))]
mu::install_std()?;
if let Some(config_path) = &cli.config_path {
cli.config = std::rc::Rc::new(mu::Config::load(config_path)?);
}
Ok(cli)
}
pub fn compile_parameters(
&self,
resolution: &str,
) -> miette::Result<microcad_driver::commands::CompileParameters> {
use microcad_driver::commands::compile::*;
Ok(CompileParameters {
resolve: ResolveParameters {
search_paths: self.config.search_paths.clone(),
},
render: RenderParameters::from_str(resolution)?.with_empty_cache(),
})
}
pub fn print_diagnostics(&self, diag: &impl mu::traits::PrintDiagnostics) {
eprintln!(
"{}",
diag.diagnostics_string(&mu::PrintDiagnosticsParameters {
color: true,
unicode: true
})
);
}
pub fn run(&self) -> miette::Result<()> {
let start = std::time::Instant::now();
match &self.command {
Commands::Check(check) => {
check.run(self)?;
}
Commands::Export(export) => {
export.run(self)?;
}
Commands::Format(format) => {
format.run(self)?;
}
Commands::Create(create) => {
create.run(self)?;
}
Commands::Watch(watch) => {
watch.run(self)?;
}
Commands::Completions(completions) => {
completions.run(self)?;
}
Commands::Doc(doc) => {
doc.run(self)?;
}
}
if self.time {
eprintln!(
"Overall Time : {}",
Self::time_to_string(&start.elapsed())
);
}
Ok(())
}
pub(super) fn time_to_string(duration: &std::time::Duration) -> String {
use num_format::{Locale, ToFormattedString};
format!(
"{:>8}µs",
duration.as_micros().to_formatted_string(&Locale::en)
)
}
}