Skip to content

Commit

Permalink
Table formatting logic overhaul (#305)
Browse files Browse the repository at this point in the history
* Table formatting logic overhaul

- Columns now auto-expand and auto-shrink proportionally
- Data column selection logic is now set per-table
- Necessary boilerplate added to allow tables with more (or fewer) columns in the future

* Better naming: `TableLayout` -> `DisplayLayout`

* Fix clippy complaints

* Optimise layout cutoff widths

- These values are pretty much arbitrary. I'm open to further optimising them in the future.

* Updated test snapshots to match new layout settings

* Remove unnecessary logging

* Correct `debug_fn` impl for `column_selector`

* Further optimise bandwidth column display

* Update test snapshots

* Layout width preset minor adjustment
  • Loading branch information
cyqsimon authored Oct 19, 2023
1 parent d9cc84b commit 0c4987a
Show file tree
Hide file tree
Showing 40 changed files with 624 additions and 515 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ log = "0.4.20"
simplelog = "0.12.1"
clap-verbosity-flag = "2.0.1"
derivative = "2.2.0"
itertools = "0.11.0"

[target.'cfg(target_os="windows")'.dependencies]
netstat2 = "0.9.1"
Expand All @@ -53,7 +54,6 @@ insta = "1.34.0"
pnet_base = "0.34.0"
packet-builder = { version = "0.7.0", git = "https://github.com/cyqsimon/packet_builder.git", branch = "patch-update" }
rstest = "0.18.2"
itertools = "0.11.0"

[target.'cfg(target_os="windows")'.build-dependencies]
anyhow = "1.0.75"
Expand Down
28 changes: 13 additions & 15 deletions src/display/components/display_bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@ use std::fmt;

pub struct DisplayBandwidth {
pub bandwidth: f64,
pub as_rate: bool,
}

impl fmt::Display for DisplayBandwidth {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let suffix = if self.as_rate { "ps" } else { "" };
if self.bandwidth > 999_999_999_999.0 {
// 1024 * 1024 * 1024 * 1024
write!(f, "{:.2}TiB{suffix}", self.bandwidth / 1_099_511_627_776.0,)
} else if self.bandwidth > 999_999_999.0 {
// 1024 * 1024 * 1024
write!(f, "{:.2}GiB{suffix}", self.bandwidth / 1_073_741_824.0)
} else if self.bandwidth > 999_999.0 {
// 1024 * 1024
write!(f, "{:.2}MiB{suffix}", self.bandwidth / 1_048_576.0)
} else if self.bandwidth > 999.0 {
write!(f, "{:.2}KiB{suffix}", self.bandwidth / 1024.0)
// see https://github.com/rust-lang/rust/issues/41620
let (div, suffix) = if self.bandwidth >= 1e12 {
(1_099_511_627_776.0, "TiB")
} else if self.bandwidth >= 1e9 {
(1_073_741_824.0, "GiB")
} else if self.bandwidth >= 1e6 {
(1_048_576.0, "MiB")
} else if self.bandwidth >= 1e3 {
(1024.0, "KiB")
} else {
write!(f, "{}B{suffix}", self.bandwidth)
}
(1.0, "B")
};

write!(f, "{:.2}{suffix}", self.bandwidth / div)
}
}
26 changes: 13 additions & 13 deletions src/display/components/header_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ impl<'a> HeaderDetails<'a> {
}

fn bandwidth_string(&self) -> String {
let c_mode = self.state.cumulative_mode;
format!(
" Total Up / Down: {} / {}{}",
DisplayBandwidth {
bandwidth: self.state.total_bytes_uploaded as f64,
as_rate: !c_mode,
},
DisplayBandwidth {
bandwidth: self.state.total_bytes_downloaded as f64,
as_rate: !c_mode,
},
if self.paused { " [PAUSED]" } else { "" }
)
let t = if self.state.cumulative_mode {
"Data"
} else {
"Rate"
};
let up = DisplayBandwidth {
bandwidth: self.state.total_bytes_uploaded as f64,
};
let down = DisplayBandwidth {
bandwidth: self.state.total_bytes_downloaded as f64,
};
let paused = if self.paused { " [PAUSED]" } else { "" };
format!(" Total {t} (Up / Down): {up} / {down}{paused}")
}

fn render_elapsed_time(
Expand Down
2 changes: 1 addition & 1 deletion src/display/components/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn top_app_and_bottom_split(rect: Rect) -> (Rect, Rect, Rect) {

pub struct Layout<'a> {
pub header: HeaderDetails<'a>,
pub children: Vec<Table<'a>>,
pub children: Vec<Table>,
pub footer: HelpText,
}

Expand Down
Loading

0 comments on commit 0c4987a

Please sign in to comment.