Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table formatting logic overhaul #305

Merged
merged 10 commits into from
Oct 19, 2023
Merged
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
Loading