From 2f7f00001d04b8abb783502ce2c70d3fcd41b5c1 Mon Sep 17 00:00:00 2001 From: Tyarel8 <98483313+Tyarel8@users.noreply.github.com> Date: Wed, 13 Aug 2025 22:49:39 +0200 Subject: [PATCH] feat(std/bench) add `osc 9;4` progress bar (#16245) # Description Using `osc 9;4`, `bench` shows a progress bar or circle on supported terminals, every 10 timing rounds to not degrade performance too much. # User-Facing Changes # Tests + Formatting # After Submitting --- crates/nu-std/std/bench/mod.nu | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/nu-std/std/bench/mod.nu b/crates/nu-std/std/bench/mod.nu index 311c3a4364..7feba83d9e 100644 --- a/crates/nu-std/std/bench/mod.nu +++ b/crates/nu-std/std/bench/mod.nu @@ -84,6 +84,8 @@ export def main [ nothing -> table nothing -> string ] { + let $num_commands = ($commands | length) + let $total_rounds = $rounds * $num_commands if $setup != null { do $setup | ignore } let results = ( @@ -91,7 +93,7 @@ export def main [ let code = $x.item let idx = $x.index - let bench_num = if ($commands | length) > 1 { $" ($idx + 1)"} + let bench_num = if $num_commands > 1 { $" ($idx + 1)"} seq 1 $warmup | each {|i| if $progress { print -n $"Warmup($bench_num): ($i) / ($warmup)\r" } @@ -104,6 +106,7 @@ export def main [ let times: list = ( seq 1 $rounds | each {|i| + if $i mod 10 == 0 { print -n $'(ansi osc)9;4;1;(($idx * $rounds + $i) / $total_rounds * 100 | into int)(ansi st)' } if $progress { print -n $"Benchmark($bench_num): ($i) / ($rounds)\r" } if $prepare != null { $idx | do $prepare $idx | ignore } @@ -127,9 +130,10 @@ export def main [ ) if $cleanup != null { do $cleanup | ignore } + print -n $'(ansi osc)9;4;0(ansi st)' # One benchmark - if ($results | length) == 1 { + if $num_commands == 1 { let report = $results | first if $pretty { return $"($report.mean) +/- ($report.std)"