Skip to content

Commit

Permalink
Add indicator to summary line if JS is still evaluating
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Jan 25, 2024
1 parent 9217f94 commit 6a6230c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
3 changes: 3 additions & 0 deletions crates/brioche/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ struct BuildArgs {

async fn build(args: BuildArgs) -> anyhow::Result<ExitCode> {
let (reporter, mut guard) = brioche::reporter::start_console_reporter()?;
reporter.set_is_evaluating(true);

let brioche = brioche::brioche::BriocheBuilder::new(reporter.clone())
.keep_temps(args.keep)
Expand Down Expand Up @@ -131,6 +132,8 @@ async fn build(args: BuildArgs) -> anyhow::Result<ExitCode> {

let artifact =
brioche::brioche::script::evaluate::evaluate(&brioche, &project, &args.export).await?;

reporter.set_is_evaluating(false);
let result = brioche::brioche::resolve::resolve(&brioche, artifact).await?;

guard.shutdown_console().await;
Expand Down
26 changes: 22 additions & 4 deletions crates/brioche/src/reporter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{
collections::HashMap,
sync::{atomic::AtomicUsize, Arc, RwLock},
sync::{
atomic::{AtomicBool, AtomicUsize},
Arc, RwLock,
},
};

use bstr::ByteSlice;
Expand All @@ -19,9 +22,12 @@ pub fn start_console_reporter() -> anyhow::Result<(Reporter, ReporterGuard)> {

let brioche_jaeger_endpoint = std::env::var("BRIOCHE_JAEGER_ENDPOINT").ok();

let is_evaluating = Arc::new(AtomicBool::new(false));

let reporter = Reporter {
next_id: Arc::new(AtomicUsize::new(0)),
tx: tx.clone(),
is_evaluating: is_evaluating.clone(),
next_id: Arc::new(AtomicUsize::new(0)),
};
let guard = ReporterGuard {
tx,
Expand All @@ -38,6 +44,7 @@ pub fn start_console_reporter() -> anyhow::Result<(Reporter, ReporterGuard)> {
Some(console) => {
let root = JobsComponent {
start: std::time::Instant::now(),
is_evaluating,
jobs,
terminal: tokio::sync::RwLock::new(termwiz::surface::Surface::new(80, 24)),
};
Expand Down Expand Up @@ -337,6 +344,7 @@ pub fn start_lsp_reporter(client: tower_lsp::Client) -> (Reporter, ReporterGuard

let reporter = Reporter {
next_id: Arc::new(AtomicUsize::new(0)),
is_evaluating: Arc::new(AtomicBool::new(false)),
tx: tx.clone(),
};
let guard = ReporterGuard {
Expand Down Expand Up @@ -402,6 +410,7 @@ pub fn start_test_reporter() -> (Reporter, ReporterGuard) {

let reporter = Reporter {
next_id: Arc::new(AtomicUsize::new(0)),
is_evaluating: Arc::new(AtomicBool::new(false)),
tx: tx.clone(),
};
let guard = ReporterGuard {
Expand Down Expand Up @@ -687,6 +696,7 @@ pub struct JobId(usize);
#[derive(Debug, Clone)]
pub struct Reporter {
tx: tokio::sync::mpsc::UnboundedSender<ReportEvent>,
is_evaluating: Arc<AtomicBool>,
next_id: Arc<AtomicUsize>,
}

Expand All @@ -695,6 +705,11 @@ impl Reporter {
let _ = self.tx.send(ReportEvent::Emit { lines });
}

pub fn set_is_evaluating(&self, is_evaluating: bool) {
self.is_evaluating
.store(is_evaluating, std::sync::atomic::Ordering::SeqCst);
}

pub fn add_job(&self, job: NewJob) -> JobId {
let id = self
.next_id
Expand Down Expand Up @@ -741,6 +756,7 @@ impl std::io::Write for ReporterWriter {

struct JobsComponent {
start: std::time::Instant,
is_evaluating: Arc<AtomicBool>,
jobs: Arc<tokio::sync::RwLock<HashMap<JobId, Job>>>,
terminal: tokio::sync::RwLock<termwiz::surface::Surface>,
}
Expand All @@ -761,6 +777,7 @@ impl superconsole::Component for JobsComponent {

let num_jobs = jobs.len();
let num_complete_jobs = complete_jobs.len();
let is_evaluating = self.is_evaluating.load(std::sync::atomic::Ordering::SeqCst);

let jobs = incomplete_jobs
.iter()
Expand Down Expand Up @@ -796,8 +813,9 @@ impl superconsole::Component for JobsComponent {

let elapsed = self.start.elapsed().human_duration();
let summary_line = format!(
"[{elapsed}] {num_complete_jobs} / {num_jobs} job{s} complete",
s = if num_jobs == 1 { "" } else { "s" }
"[{elapsed}] {num_complete_jobs} / {num_jobs}{or_more} job{s} complete",
s = if num_jobs == 1 { "" } else { "s" },
or_more = if is_evaluating { "+" } else { "" },
);
let summary_line = superconsole::Line::from_iter([summary_line.try_into().unwrap()]);

Expand Down

0 comments on commit 6a6230c

Please sign in to comment.