use anyhow::Result;
use clap::Parser;
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
use std::time::Duration;
use xert::cli::{Cli, Commands};
use xert::scaffold::{self, ProgressHandler};
struct CliProgressHandler {
pb: ProgressBar,
}
impl CliProgressHandler {
fn new() -> Self {
let spinner_style = ProgressStyle::default_spinner()
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", ""])
.template("{spinner:.blue} {msg}")
.unwrap();
let pb = ProgressBar::new_spinner();
pb.set_style(spinner_style);
pb.enable_steady_tick(Duration::from_millis(80));
Self { pb }
}
}
impl ProgressHandler for CliProgressHandler {
fn start_phase(&self, phase: &str) {
self.pb.set_message(phase.to_string());
}
fn finish_phase(&self, phase: &str) {
self.pb.println(format!("{} {}", "✓".green(), phase));
}
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Commands::Create { template, name } => {
let handler = CliProgressHandler::new();
let base_template: xert::Template = (*template).into();
if let Err(e) = scaffold::create_project(&base_template, name, &handler).await {
eprintln!("\n{} {}", "Error:".red().bold(), e);
std::process::exit(1);
}
handler.pb.finish_and_clear();
println!("\nSuccess! Created {} at ./{}", name.green().bold(), name);
println!("inside that directory, you can run:\n");
println!(
" {} to start the development server",
"cargo run".cyan()
);
println!(" {} to check for errors\n", "cargo check".cyan());
println!("Get started by typing:\n");
println!(" cd {}", name.cyan());
println!(" cargo run\n");
}
}
Ok(())
}