refactor: centralize BYTES_PER_MB and tidy peer IP parsing

- Add pub const BYTES_PER_MB to ui/utils.rs and replace 7 uses of the
  bare 1_048_576.0 literal across the UI modules. Also pick up
  uninlined-format-args cleanup in the same file while we're here.
- Replace Vec<i32> IP-byte accumulator in Peer::new_from_message with a
  [u8; 4]; the .abs() calls on u8.into() were no-ops on always-positive
  values. Network byte order ([3].[2].[1].[0]) preserved; tests cover
  the wire format.
This commit is contained in:
michel
2026-04-26 17:20:45 +02:00
parent e0218b2433
commit 9d6a33da34
5 changed files with 32 additions and 36 deletions
+15 -15
View File
@@ -5,28 +5,29 @@ use ratatui::{
use crate::ui::COLOR_PRIMARY;
pub const BYTES_PER_MB: f64 = 1_048_576.0;
pub fn format_bytes(bytes: u64) -> String {
let mb = bytes as f64 / 1_048_576.0;
format!("{:.1} MB", mb)
let mb = bytes as f64 / BYTES_PER_MB;
format!("{mb:.1} MB")
}
pub fn format_bytes_progress(downloaded: u64, total: u64) -> Line<'static> {
let downloaded_mb = downloaded as f64 / 1_048_576.0;
let total_mb = total as f64 / 1_048_576.0;
let downloaded_mb = downloaded as f64 / BYTES_PER_MB;
let total_mb = total as f64 / BYTES_PER_MB;
let mut spans = vec![];
spans.push(Span::styled(
format!("{:.1}/{:.1}", downloaded_mb, total_mb),
Style::default().fg(COLOR_PRIMARY),
));
spans.push(Span::raw(" MB"));
Line::from(spans)
Line::from(vec![
Span::styled(
format!("{downloaded_mb:.1}/{total_mb:.1}"),
Style::default().fg(COLOR_PRIMARY),
),
Span::raw(" MB"),
])
}
pub fn format_speed(speed_bytes_per_sec: f64) -> String {
let mb = speed_bytes_per_sec / 1_048_576.0;
format!("{:.1} MB/s", mb)
let mb = speed_bytes_per_sec / BYTES_PER_MB;
format!("{mb:.1} MB/s")
}
pub fn get_bitrate(
@@ -35,7 +36,6 @@ pub fn get_bitrate(
attribs.get(&0).copied()
}
// Spinner animation chars
const SPINNER_CHARS: [&str; 10] =
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];