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
+7 -11
View File
@@ -119,17 +119,13 @@ impl Peer {
let username = message.read_string();
let connection_type = message.read_string().parse().unwrap();
let mut ip: Vec<i32> = vec![];
for _ in 0..4 {
ip.push(message.read_int8().into());
}
let host: String = format!(
"{}.{}.{}.{}",
ip[3].abs(),
ip[2].abs(),
ip[1].abs(),
ip[0].abs()
);
let ip = [
message.read_int8(),
message.read_int8(),
message.read_int8(),
message.read_int8(),
];
let host = format!("{}.{}.{}.{}", ip[3], ip[2], ip[1], ip[0]);
let (port, token, privileged, unknown, obfuscated_port) = (
message.read_int32(),
+2 -2
View File
@@ -1,6 +1,6 @@
use crate::models::FileDisplayData;
use crate::ui::{
HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes,
BYTES_PER_MB, HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes,
format_shortcuts_styled, get_bitrate, get_spinner_char, header_style,
highlight_style, primary_style, success_style, warning_style,
};
@@ -379,7 +379,7 @@ impl FileSelector {
let is_selected = self.selected_indices.contains(&original_idx);
let checkbox = if is_selected { "[✓]" } else { "[ ]" };
let speed_mb = item.speed as f64 / 1_048_576.0;
let speed_mb = item.speed as f64 / BYTES_PER_MB;
let speed_str = if speed_mb > 0.0 {
format!("{:.1} MB/s", speed_mb)
} else {
+5 -5
View File
@@ -1,9 +1,9 @@
use crate::models::DownloadEntry;
use crate::ui::{
COLOR_PRIMARY, HIGHLIGHT_SYMBOL, border_style, border_type, error_style,
format_bytes_progress, format_progress_bar, format_shortcuts_styled,
format_speed, header_style, highlight_style, inactive_style, info_style,
primary_style, warning_style,
BYTES_PER_MB, COLOR_PRIMARY, HIGHLIGHT_SYMBOL, border_style, border_type,
error_style, format_bytes_progress, format_progress_bar,
format_shortcuts_styled, format_speed, header_style, highlight_style,
inactive_style, info_style, primary_style, warning_style,
};
use color_eyre::Result;
use ratatui::{
@@ -381,7 +381,7 @@ pub fn render_download_stats(
})
.map(|d| d.download.speed_bytes_per_sec())
.sum();
let speed_mb = (total_speed / 1_048_576.0 * 100.0).round() / 100.0;
let speed_mb = (total_speed / BYTES_PER_MB * 100.0).round() / 100.0;
let block = Block::default()
.borders(Borders::ALL)
+3 -3
View File
@@ -1,7 +1,7 @@
use crate::models::FileDisplayData;
use crate::ui::{
HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes, header_style,
highlight_style,
BYTES_PER_MB, HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes,
header_style, highlight_style,
};
use ratatui::{
Frame,
@@ -96,7 +96,7 @@ No results. Select a search from the Searches pane [1]. Or start new search [s
let speed_str = if file.speed > 0 {
let speed_mb =
(file.speed as f64 / 1_048_576.0 * 100.0).round() / 100.0;
(file.speed as f64 / BYTES_PER_MB * 100.0).round() / 100.0;
format!("{} MB/s", speed_mb)
} else {
"-".to_string()
+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] =
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];