From 565b45e5a85a16552aaf3217a7f4207105705a24 Mon Sep 17 00:00:00 2001 From: Kyle Lacy Date: Wed, 24 Jan 2024 22:28:32 -0800 Subject: [PATCH] Print final build summary even when not using SuperConsole --- crates/brioche/src/main.rs | 10 +++++++ crates/brioche/src/reporter.rs | 50 +++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/crates/brioche/src/main.rs b/crates/brioche/src/main.rs index ce8cc0c..82c97e2 100644 --- a/crates/brioche/src/main.rs +++ b/crates/brioche/src/main.rs @@ -3,6 +3,7 @@ use std::{path::PathBuf, process::ExitCode}; use anyhow::Context as _; use brioche::{fs_utils, sandbox::SandboxExecutionConfig}; use clap::Parser; +use human_repr::HumanDuration; use tracing::Instrument; #[derive(Debug, Parser)] @@ -138,6 +139,15 @@ async fn build(args: BuildArgs) -> anyhow::Result { guard.shutdown_console().await; + let elapsed = reporter.elapsed().human_duration(); + let num_jobs = reporter.num_jobs(); + let jobs_message = match num_jobs { + 0 => "(no new jobs)".to_string(), + 1 => "1 job".to_string(), + n => format!("{n} jobs"), + }; + println!("Build finished, completed {jobs_message} in {elapsed}"); + let result_hash = result.value.hash(); println!("Result: {result_hash}"); diff --git a/crates/brioche/src/reporter.rs b/crates/brioche/src/reporter.rs index b031bea..0e0c981 100644 --- a/crates/brioche/src/reporter.rs +++ b/crates/brioche/src/reporter.rs @@ -22,12 +22,14 @@ pub fn start_console_reporter() -> anyhow::Result<(Reporter, ReporterGuard)> { let brioche_jaeger_endpoint = std::env::var("BRIOCHE_JAEGER_ENDPOINT").ok(); + let start = std::time::Instant::now(); let is_evaluating = Arc::new(AtomicBool::new(false)); let reporter = Reporter { - tx: tx.clone(), + start, + num_jobs: Arc::new(AtomicUsize::new(0)), is_evaluating: is_evaluating.clone(), - next_id: Arc::new(AtomicUsize::new(0)), + tx: tx.clone(), }; let guard = ReporterGuard { tx, @@ -43,7 +45,7 @@ pub fn start_console_reporter() -> anyhow::Result<(Reporter, ReporterGuard)> { let mut console = match superconsole { Some(console) => { let root = JobsComponent { - start: std::time::Instant::now(), + start, is_evaluating, jobs, terminal: tokio::sync::RwLock::new(termwiz::surface::Surface::new(80, 24)), @@ -343,7 +345,8 @@ pub fn start_lsp_reporter(client: tower_lsp::Client) -> (Reporter, ReporterGuard let (tx, _) = tokio::sync::mpsc::unbounded_channel(); let reporter = Reporter { - next_id: Arc::new(AtomicUsize::new(0)), + start: std::time::Instant::now(), + num_jobs: Arc::new(AtomicUsize::new(0)), is_evaluating: Arc::new(AtomicBool::new(false)), tx: tx.clone(), }; @@ -409,7 +412,8 @@ pub fn start_test_reporter() -> (Reporter, ReporterGuard) { }; let reporter = Reporter { - next_id: Arc::new(AtomicUsize::new(0)), + start: std::time::Instant::now(), + num_jobs: Arc::new(AtomicUsize::new(0)), is_evaluating: Arc::new(AtomicBool::new(false)), tx: tx.clone(), }; @@ -695,9 +699,10 @@ pub struct JobId(usize); #[derive(Debug, Clone)] pub struct Reporter { - tx: tokio::sync::mpsc::UnboundedSender, + start: std::time::Instant, + num_jobs: Arc, is_evaluating: Arc, - next_id: Arc, + tx: tokio::sync::mpsc::UnboundedSender, } impl Reporter { @@ -712,7 +717,7 @@ impl Reporter { pub fn add_job(&self, job: NewJob) -> JobId { let id = self - .next_id + .num_jobs .fetch_add(1, std::sync::atomic::Ordering::SeqCst); let id = JobId(id); @@ -724,6 +729,14 @@ impl Reporter { pub fn update_job(&self, id: JobId, update: UpdateJob) { let _ = self.tx.send(ReportEvent::UpdateJobState { id, update }); } + + pub fn elapsed(&self) -> std::time::Duration { + self.start.elapsed() + } + + pub fn num_jobs(&self) -> usize { + self.num_jobs.load(std::sync::atomic::Ordering::SeqCst) + } } impl tracing_subscriber::fmt::MakeWriter<'_> for Reporter { @@ -814,27 +827,26 @@ impl superconsole::Component for JobsComponent { let elapsed = self.start.elapsed().human_duration(); let summary_line = match mode { superconsole::DrawMode::Normal => { - format!( + let summary_line = format!( "[{elapsed}] {num_complete_jobs} / {num_jobs}{or_more} job{s} complete", s = if num_jobs == 1 { "" } else { "s" }, or_more = if is_evaluating { "+" } else { "" }, - ) + ); + Some(superconsole::Line::from_iter([summary_line + .try_into() + .unwrap()])) } superconsole::DrawMode::Final => { - let jobs_message = match num_jobs { - 0 => "(no new jobs)".to_string(), - 1 => "1 job".to_string(), - n => format!("{n} jobs"), - }; - format!("Build finished, completed {jobs_message} in {elapsed}") + // Don't show the summary line on the final draw. The final + // summary will be written outside the reporter, since we also + // want to show the summary when not using SuperConsole + None } }; - let summary_line = superconsole::Line::from_iter([summary_line.try_into().unwrap()]); - let lines = terminal_lines .chain(jobs_lines.into_iter().flatten()) - .chain([summary_line]) + .chain(summary_line) .collect(); Ok(lines) }