- 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.
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
use ratatui::{
|
|
style::Style,
|
|
text::{Line, Span},
|
|
};
|
|
|
|
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 / BYTES_PER_MB;
|
|
format!("{mb:.1} MB")
|
|
}
|
|
|
|
pub fn format_bytes_progress(downloaded: u64, total: u64) -> Line<'static> {
|
|
let downloaded_mb = downloaded as f64 / BYTES_PER_MB;
|
|
let total_mb = total as f64 / BYTES_PER_MB;
|
|
|
|
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 / BYTES_PER_MB;
|
|
format!("{mb:.1} MB/s")
|
|
}
|
|
|
|
pub fn get_bitrate(
|
|
attribs: &std::collections::HashMap<u32, u32>,
|
|
) -> Option<u32> {
|
|
attribs.get(&0).copied()
|
|
}
|
|
|
|
const SPINNER_CHARS: [&str; 10] =
|
|
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
|
|
pub fn get_spinner_char(state: usize) -> &'static str {
|
|
SPINNER_CHARS[state % SPINNER_CHARS.len()]
|
|
}
|