From 7ed9ee2992ef15789d82ab961f525d9bb2885204 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 23 Apr 2026 17:47:30 +0300 Subject: [PATCH] benchmark: fix nixos V panic: string.repeat: count is negative: -26 (fixes #10974) --- vlib/benchmark/benchmark.v | 17 ++++++++++++++--- vlib/benchmark/benchmark_test.v | 11 +++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/vlib/benchmark/benchmark.v b/vlib/benchmark/benchmark.v index 0cd2ad264..cdc7cb653 100644 --- a/vlib/benchmark/benchmark.v +++ b/vlib/benchmark/benchmark.v @@ -4,6 +4,8 @@ import time import term import arrays +const step_label_width = 5 + pub const b_ok = term.ok_message('OK ') pub const b_fail = term.fail_message('FAIL') pub const b_skip = term.warn_message('SKIP') @@ -166,6 +168,7 @@ pub: pub fn (b &Benchmark) step_message_with_label_and_duration(label string, msg string, sduration time.Duration, opts MessageOptions) string { timed_line := b.tdiff_in_ms(msg, sduration.microseconds()) + flabel := format_step_label(label) if b.nexpected_steps > 1 { mut sprogress := '' if b.nexpected_steps < 10 { @@ -194,11 +197,19 @@ pub fn (b &Benchmark) step_message_with_label_and_duration(label string, msg str } } if opts.preparation > 0 { - return '${label:-5s} [${sprogress}] C: ${f64(opts.preparation.microseconds()) / 1_000.0:7.1F} ms, R: ${timed_line}' + return '${flabel} [${sprogress}] C: ${f64(opts.preparation.microseconds()) / 1_000.0:7.1F} ms, R: ${timed_line}' } - return '${label:-5s} [${sprogress}] ${timed_line}' + return '${flabel} [${sprogress}] ${timed_line}' + } + return '${flabel}${timed_line}' +} + +fn format_step_label(label string) string { + visible_len := term.strip_ansi(label).len + if visible_len >= step_label_width { + return label } - return '${label:-5s}${timed_line}' + return label + ' '.repeat(step_label_width - visible_len) } // step_message_with_label returns a string describing the current step using current time as duration. diff --git a/vlib/benchmark/benchmark_test.v b/vlib/benchmark/benchmark_test.v index 3871fa4ea..fec00d39c 100644 --- a/vlib/benchmark/benchmark_test.v +++ b/vlib/benchmark/benchmark_test.v @@ -1,5 +1,6 @@ import time import benchmark +import term fn test_measure() { mut b := benchmark.start() @@ -62,3 +63,13 @@ fn test_total_message() { assert !res2.contains(' Max: ') assert !res2.contains(' Avg: ') } + +fn test_step_message_handles_ansi_label_longer_than_label_width() { + mut b := benchmark.start() + ansi_fail_label := '\x1b[41m\x1b[1m\x1b[37m FAIL \x1b[39m\x1b[22m\x1b[49m' + long_cmd := 'command: /nix/store/' + 'x'.repeat(90) + '-vlang/build/source/v -o v_g -g cmd/v' + message := b.step_message_with_label(ansi_fail_label, long_cmd) + + assert term.strip_ansi(message).contains('FAIL') + assert message.contains(long_cmd) +} -- 2.39.5