From d6fc5dc5bd3318699a3c5d2b3ba181a597d11f22 Mon Sep 17 00:00:00 2001 From: Miles Murgaw Date: Fri, 7 Jun 2024 20:29:55 -0400 Subject: [PATCH] feat: progress bar or println (#2489) --- packages/cli/src/builder.rs | 65 +++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/builder.rs b/packages/cli/src/builder.rs index a83d709d1..f21f17341 100644 --- a/packages/cli/src/builder.rs +++ b/packages/cli/src/builder.rs @@ -15,7 +15,7 @@ use manganis_cli_support::{AssetManifest, ManganisSupportGuard}; use std::{ env, fs::{copy, create_dir_all, File}, - io::Read, + io::{self, IsTerminal, Read}, panic, path::PathBuf, process::Command, @@ -437,18 +437,56 @@ struct CargoBuildResult { output_location: Option, } +struct Outputter { + progress_bar: Option, +} + +impl Outputter { + pub fn new() -> Self { + let stdout = io::stdout().lock(); + + let mut myself = Self { progress_bar: None }; + + if stdout.is_terminal() { + let mut pb = ProgressBar::new_spinner(); + pb.enable_steady_tick(Duration::from_millis(200)); + pb = PROGRESS_BARS.add(pb); + pb.set_style( + ProgressStyle::with_template("{spinner:.dim.bold} {wide_msg}") + .unwrap() + .tick_chars("/|\\- "), + ); + + myself.progress_bar = Some(pb); + } + + myself + } + + pub fn println(&self, msg: impl ToString) { + let msg = msg.to_string(); + if let Some(pb) = &self.progress_bar { + pb.set_message(msg) + } else { + println!("{msg}"); + } + } + + pub fn finish_with_message(&self, msg: impl ToString) { + let msg = msg.to_string(); + if let Some(pb) = &self.progress_bar { + pb.finish_with_message(msg) + } else { + println!("{msg}"); + } + } +} + fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result { let mut warning_messages: Vec = vec![]; - let mut pb = ProgressBar::new_spinner(); - pb.enable_steady_tick(Duration::from_millis(200)); - pb = PROGRESS_BARS.add(pb); - pb.set_style( - ProgressStyle::with_template("{spinner:.dim.bold} {wide_msg}") - .unwrap() - .tick_chars("/|\\- "), - ); - pb.set_message("💼 Waiting to start building the project..."); + let output = Outputter::new(); + output.println("💼 Waiting to start building the project..."); let stdout = cmd.detached().stream_stdout()?; let reader = std::io::BufReader::new(stdout); @@ -473,8 +511,7 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result { } } Message::CompilerArtifact(artifact) => { - pb.set_message(format!("⚙ Compiling {} ", artifact.package_id)); - pb.tick(); + output.println(format!("⚙ Compiling {} ", artifact.package_id)); if let Some(executable) = artifact.executable { output_location = Some(executable.into()); } @@ -484,9 +521,9 @@ fn prettier_build(cmd: subprocess::Exec) -> anyhow::Result { } Message::BuildFinished(finished) => { if finished.success { - pb.finish_with_message("👑 Build done."); + output.finish_with_message("👑 Build done."); } else { - pb.finish_with_message("❌ Build failed."); + output.finish_with_message("❌ Build failed."); return Err(anyhow::anyhow!("Build failed")); } }