Compact terminal output on narrow terminals
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent ce49529 · Browse files · 3 files·+18 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+18 added−8 removed
- other:+18 added−8 removed
Provenance
- Commit
e8cdbf2- Tree
1133900- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ce49529
3 files changed+18 added−8 removed
modified:src/output/report.rs+16 added-8 removed
1-use std::io::{self, Write};1+use std::io::{self, IsTerminal, Write};2 3 use anyhow::{ensure, Result};4 use colored::Colorize;5 5 unmodified lines11 // independently printed benchmarks keep the same layout.12 const MIN_VALUE_WIDTH: usize = 7;13 const MIN_UNIT_WIDTH: usize = 3;14 const MIN_CHANGE_WIDTH: usize = 14;15+// Leave room for long metric labels and comparison factors in the full layout.16+const COMPACT_WIDTH_THRESHOLD: u16 = 110;17 18 /// Print a completed benchmark immediately, comparing every metric with benchmark 1.19 pub fn print(20 number: usize,2 unmodified lines23 metrics: &[MetricSelection],24 skip_unavailable_metrics: bool,25 reference: Option<&BenchmarkResult>,26 ) -> Result<()> {27+ // Only adapt interactive output; redirected reports keep their usual layout.28+ let compact = io::stdout().is_terminal()29+ && terminal_size::terminal_size()30+ .is_some_and(|(terminal_size::Width(width), _)| width < COMPACT_WIDTH_THRESHOLD);31 let comparison = reference.is_some();32 let multiple_runs = measurements.len() > 1;33 let value_columns = 1..if multiple_runs { 5 } else { 2 };34 let mut header = vec![27 unmodified lines62 .filter(|stats| stats.mean != 0.0)63 .unwrap_or(&summary)64 .mean;65 let unit = selection.display_unit(scale_mean);60- let mut row = vec![61- selection.metric.label().to_owned(),62- unit.format_value(summary.mean),63- ];66+ let label = if compact && unit.symbol != "count" {67+ format!("{} [{}]", selection.metric.label(), unit.symbol)68+ } else {69+ selection.metric.label().to_owned()70+ };71+ let mut row = vec![label, unit.format_value(summary.mean)];72 if let Some(stddev) = summary.stddev {73 row.push(unit.format_value(stddev));74 row.push(unit.format_value(summary.min));75 row.push(unit.format_value(summary.max));39 unmodified lines115 .unwrap();116 if column == 0 {117 width.max(metric_width)118 } else if value_columns.contains(&column) {111- width.max(MIN_VALUE_WIDTH)119+ width.max(if compact { 5 } else { MIN_VALUE_WIDTH })120 } else {113- width.max(MIN_CHANGE_WIDTH)121+ width.max(if compact { 7 } else { MIN_CHANGE_WIDTH })122 }123 })124 .collect();125 let mut stdout = io::stdout().lock();28 unmodified lines154 _ => " ",155 });156 }157 let padding = " ".repeat(widths[column] - value.chars().count());150- let has_unit = value_columns.contains(&column);158+ let has_unit = !compact && value_columns.contains(&column);159 let text = if has_unit && !unit.is_empty() {160 format!("{value} {unit}")161 } else {162 value.clone()43 unmodified linesmodified:Cargo.lock+1 added
400 unmodified lines401 "serde",402 "serde_json",403 "shell-words",404 "tempfile",405+ "terminal_size",406 "thiserror",407 "uom",408 "windows-sys 0.61.2",409 ]802 unmodified linesmodified:Cargo.toml+1 added
24 unmodified lines25262728293031323356 unmodified lines24 unmodified linesserde_json = "1.0"rust_decimal = "1.43"rand = "0.10"shell-words = "1.1"terminal_size = "0.4"thiserror = "2.0"anyhow = "1.0"uom = { version = "0.36.0", default-features = false, features = ["std", "si", "f64"] }humantime = "2"56 unmodified lines