Skip to content
Myr

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

3 files+18 added−8 removed

modified:src/output/report.rs+16 added-8 removed
1use std::io::{self, Write};1use std::io::{self, IsTerminal, Write};23use anyhow::{ensure, Result};4use colored::Colorize;5
5 unmodified lines
11// independently printed benchmarks keep the same layout.12const MIN_VALUE_WIDTH: usize = 7;13const MIN_UNIT_WIDTH: usize = 3;14const MIN_CHANGE_WIDTH: usize = 14;15// Leave room for long metric labels and comparison factors in the full layout.16const COMPACT_WIDTH_THRESHOLD: u16 = 110;1718/// Print a completed benchmark immediately, comparing every metric with benchmark 1.19pub fn print(20 number: usize,
2 unmodified lines
23 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 lines
62 .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 lines
115 .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 lines
154 _ => " ",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 lines
modified:Cargo.lock+1 added
400 unmodified lines
401 "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 lines
modified:Cargo.toml+1 added