Skip to content

Commit

Permalink
Print final build summary even when not using SuperConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Jan 25, 2024
1 parent 4e50412 commit 565b45e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
10 changes: 10 additions & 0 deletions crates/brioche/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -138,6 +139,15 @@ async fn build(args: BuildArgs) -> anyhow::Result<ExitCode> {

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}");

Expand Down
50 changes: 31 additions & 19 deletions crates/brioche/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)),
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -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(),
};
Expand Down Expand Up @@ -695,9 +699,10 @@ pub struct JobId(usize);

#[derive(Debug, Clone)]
pub struct Reporter {
tx: tokio::sync::mpsc::UnboundedSender<ReportEvent>,
start: std::time::Instant,
num_jobs: Arc<AtomicUsize>,
is_evaluating: Arc<AtomicBool>,
next_id: Arc<AtomicUsize>,
tx: tokio::sync::mpsc::UnboundedSender<ReportEvent>,
}

impl Reporter {
Expand All @@ -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);

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 565b45e

Please sign in to comment.