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:
@@ -119,17 +119,13 @@ impl Peer {
|
|||||||
let username = message.read_string();
|
let username = message.read_string();
|
||||||
let connection_type = message.read_string().parse().unwrap();
|
let connection_type = message.read_string().parse().unwrap();
|
||||||
|
|
||||||
let mut ip: Vec<i32> = vec![];
|
let ip = [
|
||||||
for _ in 0..4 {
|
message.read_int8(),
|
||||||
ip.push(message.read_int8().into());
|
message.read_int8(),
|
||||||
}
|
message.read_int8(),
|
||||||
let host: String = format!(
|
message.read_int8(),
|
||||||
"{}.{}.{}.{}",
|
];
|
||||||
ip[3].abs(),
|
let host = format!("{}.{}.{}.{}", ip[3], ip[2], ip[1], ip[0]);
|
||||||
ip[2].abs(),
|
|
||||||
ip[1].abs(),
|
|
||||||
ip[0].abs()
|
|
||||||
);
|
|
||||||
|
|
||||||
let (port, token, privileged, unknown, obfuscated_port) = (
|
let (port, token, privileged, unknown, obfuscated_port) = (
|
||||||
message.read_int32(),
|
message.read_int32(),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::models::FileDisplayData;
|
use crate::models::FileDisplayData;
|
||||||
use crate::ui::{
|
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,
|
format_shortcuts_styled, get_bitrate, get_spinner_char, header_style,
|
||||||
highlight_style, primary_style, success_style, warning_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 is_selected = self.selected_indices.contains(&original_idx);
|
||||||
let checkbox = if is_selected { "[✓]" } else { "[ ]" };
|
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 {
|
let speed_str = if speed_mb > 0.0 {
|
||||||
format!("{:.1} MB/s", speed_mb)
|
format!("{:.1} MB/s", speed_mb)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use crate::models::DownloadEntry;
|
use crate::models::DownloadEntry;
|
||||||
use crate::ui::{
|
use crate::ui::{
|
||||||
COLOR_PRIMARY, HIGHLIGHT_SYMBOL, border_style, border_type, error_style,
|
BYTES_PER_MB, COLOR_PRIMARY, HIGHLIGHT_SYMBOL, border_style, border_type,
|
||||||
format_bytes_progress, format_progress_bar, format_shortcuts_styled,
|
error_style, format_bytes_progress, format_progress_bar,
|
||||||
format_speed, header_style, highlight_style, inactive_style, info_style,
|
format_shortcuts_styled, format_speed, header_style, highlight_style,
|
||||||
primary_style, warning_style,
|
inactive_style, info_style, primary_style, warning_style,
|
||||||
};
|
};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
@@ -381,7 +381,7 @@ pub fn render_download_stats(
|
|||||||
})
|
})
|
||||||
.map(|d| d.download.speed_bytes_per_sec())
|
.map(|d| d.download.speed_bytes_per_sec())
|
||||||
.sum();
|
.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()
|
let block = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use crate::models::FileDisplayData;
|
use crate::models::FileDisplayData;
|
||||||
use crate::ui::{
|
use crate::ui::{
|
||||||
HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes, header_style,
|
BYTES_PER_MB, HIGHLIGHT_SYMBOL, border_style, border_type, format_bytes,
|
||||||
highlight_style,
|
header_style, highlight_style,
|
||||||
};
|
};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
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_str = if file.speed > 0 {
|
||||||
let speed_mb =
|
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)
|
format!("{} MB/s", speed_mb)
|
||||||
} else {
|
} else {
|
||||||
"-".to_string()
|
"-".to_string()
|
||||||
|
|||||||
+15
-15
@@ -5,28 +5,29 @@ use ratatui::{
|
|||||||
|
|
||||||
use crate::ui::COLOR_PRIMARY;
|
use crate::ui::COLOR_PRIMARY;
|
||||||
|
|
||||||
|
pub const BYTES_PER_MB: f64 = 1_048_576.0;
|
||||||
|
|
||||||
pub fn format_bytes(bytes: u64) -> String {
|
pub fn format_bytes(bytes: u64) -> String {
|
||||||
let mb = bytes as f64 / 1_048_576.0;
|
let mb = bytes as f64 / BYTES_PER_MB;
|
||||||
format!("{:.1} MB", mb)
|
format!("{mb:.1} MB")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_bytes_progress(downloaded: u64, total: u64) -> Line<'static> {
|
pub fn format_bytes_progress(downloaded: u64, total: u64) -> Line<'static> {
|
||||||
let downloaded_mb = downloaded as f64 / 1_048_576.0;
|
let downloaded_mb = downloaded as f64 / BYTES_PER_MB;
|
||||||
let total_mb = total as f64 / 1_048_576.0;
|
let total_mb = total as f64 / BYTES_PER_MB;
|
||||||
|
|
||||||
let mut spans = vec![];
|
Line::from(vec![
|
||||||
spans.push(Span::styled(
|
Span::styled(
|
||||||
format!("{:.1}/{:.1}", downloaded_mb, total_mb),
|
format!("{downloaded_mb:.1}/{total_mb:.1}"),
|
||||||
Style::default().fg(COLOR_PRIMARY),
|
Style::default().fg(COLOR_PRIMARY),
|
||||||
));
|
),
|
||||||
spans.push(Span::raw(" MB"));
|
Span::raw(" MB"),
|
||||||
|
])
|
||||||
Line::from(spans)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_speed(speed_bytes_per_sec: f64) -> String {
|
pub fn format_speed(speed_bytes_per_sec: f64) -> String {
|
||||||
let mb = speed_bytes_per_sec / 1_048_576.0;
|
let mb = speed_bytes_per_sec / BYTES_PER_MB;
|
||||||
format!("{:.1} MB/s", mb)
|
format!("{mb:.1} MB/s")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_bitrate(
|
pub fn get_bitrate(
|
||||||
@@ -35,7 +36,6 @@ pub fn get_bitrate(
|
|||||||
attribs.get(&0).copied()
|
attribs.get(&0).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spinner animation chars
|
|
||||||
const SPINNER_CHARS: [&str; 10] =
|
const SPINNER_CHARS: [&str; 10] =
|
||||||
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user