fix: align the result tables and size the IP column to content

- compute the IP column width from the widest address instead of a
  fixed 39 (which left a big gap for IPv4-only output)
- left-align the latency and speed columns so the header labels line up
  with their values, instead of right-aligning into a wide field
- keep the colored speed as the last column so it needs no width padding
This commit is contained in:
chuan
2026-06-24 01:51:51 +08:00
Unverified
parent 02b33a3e6d
commit 4126f78682
+14 -6
View File
@@ -179,10 +179,11 @@ pub(crate) fn read_export_rows(path: &Path) -> Result<Vec<ExportRow>> {
pub(crate) fn print_table<T: Ranked>(results: &[T]) {
eprintln!();
eprintln!("{}", style(format!(" Fastest {} IPs", results.len())).bold().underlined());
eprintln!(" {:>3} {:<39} {:>9}", "#", "IP", "Latency");
let ip_w = ip_col_width(results.iter().map(|r| r.ip()));
eprintln!(" {:>3} {:<ip_w$} {}", "#", "IP", "Latency");
for (i, r) in results.iter().enumerate() {
let ms = r.latency().as_secs_f64() * 1000.0;
let latency = format!("{ms:>6.1} ms");
let latency = format!("{ms:.1} ms");
let colored = if ms < 150.0 {
style(latency).green()
} else if ms < 250.0 {
@@ -190,18 +191,25 @@ pub(crate) fn print_table<T: Ranked>(results: &[T]) {
} else {
style(latency).red()
};
eprintln!(" {:>3} {:<39} {}", i + 1, r.ip().to_string(), colored);
eprintln!(" {:>3} {:<ip_w$} {}", i + 1, r.ip().to_string(), colored);
}
eprintln!();
}
/// Width for the IP column: the widest address present, but at least "IP".
fn ip_col_width(ips: impl Iterator<Item = IpAddr>) -> usize {
ips.map(|ip| ip.to_string().len()).max().unwrap_or(0).max(2)
}
/// Speed-stage table: IP, latency, speed.
pub(crate) fn print_speed_table(results: &[speed::SpeedResult]) {
eprintln!();
eprintln!("{}", style(format!(" Top {} nodes", results.len())).bold().underlined());
eprintln!(" {:>3} {:<39} {:>9} {:>10}", "#", "IP", "Latency", "Speed");
let ip_w = ip_col_width(results.iter().map(|r| r.ip));
eprintln!(" {:>3} {:<ip_w$} {:<9} {}", "#", "IP", "Latency", "Speed");
for (i, r) in results.iter().enumerate() {
let ms = r.latency.as_secs_f64() * 1000.0;
let latency = format!("{:.0} ms", r.latency.as_secs_f64() * 1000.0);
// Speed is the last column, so it can stay colored without padding.
let speed = format!("{:.2} MB/s", r.speed_mbs);
let colored = if r.speed_mbs >= 2.5 {
style(speed).green()
@@ -210,7 +218,7 @@ pub(crate) fn print_speed_table(results: &[speed::SpeedResult]) {
} else {
style(speed).red()
};
eprintln!(" {:>3} {:<39} {:>6.0} ms {}", i + 1, r.ip.to_string(), ms, colored);
eprintln!(" {:>3} {:<ip_w$} {:<9} {}", i + 1, r.ip.to_string(), latency, colored);
}
eprintln!();
}