Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions src/runtime/cli/filter_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,21 @@ impl<'a> State<'a> {
}
}

fn write_done(draw_buf: &mut Vec<u8>, proc: &ProcessInfo) -> crate::Result<()> {
if let Some(end) = proc.end_time {
let duration = end.duration_since(proc.start_time);
let ms = duration.as_nanos() as f64 / 1_000_000.0;
if ms > 1000.0 {
write!(draw_buf, fmt!("<cyan>Done in {:.2} s<r>\n"), ms / 1_000.0)?;
} else {
write!(draw_buf, fmt!("<cyan>Done in {:.0} ms<r>\n"), ms)?;
}
} else {
draw_buf.extend_from_slice(fmt!("<cyan>Done<r>\n").as_bytes());
}
Ok(())
}

fn redraw(&mut self, is_abort: bool) -> crate::Result<()> {
if !self.pretty_output {
return Ok(());
Expand All @@ -519,6 +534,23 @@ impl<'a> State<'a> {
// Reshaped for borrowck — iterating handles by index since draw_buf is also &mut self.
for idx in 0..self.handles.len() {
let handle = &self.handles[idx];
// a successful script with no output collapses to a single line
if handle.buffer.is_empty() {
if let Some(proc) = &handle.process {
if let Status::Exited(exited) = &proc.status {
if exited.code == 0 {
write!(
&mut self.draw_buf,
fmt!("<b>{s}<r> {s} <d>·<r> "),
bstr::BStr::new(&handle.config.package_name),
bstr::BStr::new(&handle.config.script_name),
)?;
Self::write_done(&mut self.draw_buf, proc)?;
continue;
}
}
}
}
// normally we truncate the output to 10 lines, but on abort we print everything to aid debugging
let elide_lines = if is_abort {
None
Expand Down Expand Up @@ -566,26 +598,7 @@ impl<'a> State<'a> {
}
Status::Exited(exited) => {
if exited.code == 0 {
if let Some(end) = proc.end_time {
let duration = end.duration_since(proc.start_time);
let ms = duration.as_nanos() as f64 / 1_000_000.0;
if ms > 1000.0 {
write!(
&mut self.draw_buf,
fmt!("<cyan>Done in {:.2} s<r>\n"),
ms / 1_000.0,
)?;
} else {
write!(
&mut self.draw_buf,
fmt!("<cyan>Done in {:.0} ms<r>\n"),
ms,
)?;
}
} else {
self.draw_buf
.extend_from_slice(fmt!("<cyan>Done<r>\n").as_bytes());
}
Self::write_done(&mut self.draw_buf, proc)?;
} else {
write!(
&mut self.draw_buf,
Expand Down
35 changes: 30 additions & 5 deletions test/cli/run/filter-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,19 @@ describe("bun", () => {
expect(exitCode).toBe(0);
});

// The terminal renderer is TTY-only on Windows (see runElideLinesTest).
test.skipIf(isWindows)("terminal output reports how long a successful script took", () => {
using dir = tempDir("filter-done-in", {
// The terminal renderer repaints the whole display on every event, wrapped
// in synchronized-update markers. The last frame is the final state; strip
// the ANSI escapes so assertions read like the rendered text.
function lastTerminalFrame(stdoutval: string): string {
const frames = stdoutval.split("\x1b[?2026l").filter(f => f.length > 0);
return frames[frames.length - 1].replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, "");
}

function runTerminalRenderTest(script: string): { frame: string; exitCode: number } {
using dir = tempDir("filter-terminal-render", {
packages: {
dep0: {
"package.json": JSON.stringify({ name: "dep0", scripts: { script: "exit 0" } }),
"package.json": JSON.stringify({ name: "dep0", scripts: { script } }),
},
},
"package.json": JSON.stringify({ name: "ws", workspaces: ["packages/*"] }),
Expand All @@ -667,10 +674,28 @@ describe("bun", () => {
stderr: "pipe",
});

expect(stdout.toString()).toMatch(/Done in (?:\d+ ms|\d+\.\d{2} s)/);
return { frame: lastTerminalFrame(stdout.toString()), exitCode };
}

// The terminal renderer is TTY-only on Windows (see runElideLinesTest).
test.skipIf(isWindows)("a successful script with no output collapses to a single line", () => {
const { frame, exitCode } = runTerminalRenderTest("exit 0");
expect(frame).toMatch(/^dep0 script · Done in (?:\d+ ms|\d+\.\d{2} s)\n$/);
expect(exitCode).toBe(0);
});

test.skipIf(isWindows)("a successful script with output keeps the expanded form", () => {
const { frame, exitCode } = runTerminalRenderTest("echo chatty_line");
expect(frame).toMatch(/^dep0 script \$ echo chatty_line\n│ chatty_line\n└─ Done in (?:\d+ ms|\d+\.\d{2} s)\n$/);
expect(exitCode).toBe(0);
});

test.skipIf(isWindows)("a failed script with no output keeps the expanded form", () => {
const { frame, exitCode } = runTerminalRenderTest("exit 7");
expect(frame).toMatch(/^dep0 script \$ exit 7\n└─ Exited with code 7\n$/);
expect(exitCode).toBe(7);
});

test("self-referential directory symlink in a workspace does not loop", () => {
using dir = tempDir("filter-symlink-loop", {
packages: {
Expand Down
Loading