From 227606752d175b66a477a89d8fe79d2ec96b3267 Mon Sep 17 00:00:00 2001 From: Wildan M Date: Tue, 16 Jun 2026 14:48:13 +0700 Subject: [PATCH] Use builtin bytes format string --- gui/src/main.rs | 23 ++--------------------- src/bin/installer_tui.rs | 23 ++--------------------- src/config/file.rs | 6 +----- src/installer.rs | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 48 deletions(-) diff --git a/gui/src/main.rs b/gui/src/main.rs index 7338cab789..4a77f02a7d 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -34,25 +34,6 @@ fn main() -> iced::Result { app::run::(settings, ()) } -const KIB: u64 = 1024; -const MIB: u64 = 1024 * KIB; -const GIB: u64 = 1024 * MIB; -const TIB: u64 = 1024 * GIB; - -fn format_size(size: u64) -> String { - if size >= 4 * TIB { - format!("{:.1} TiB", size as f64 / TIB as f64) - } else if size >= GIB { - format!("{:.1} GiB", size as f64 / GIB as f64) - } else if size >= MIB { - format!("{:.1} MiB", size as f64 / MIB as f64) - } else if size >= KIB { - format!("{:.1} KiB", size as f64 / KIB as f64) - } else { - format!("{} B", size) - } -} - fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> anyhow::Result<()> { if let Some(parent) = dest.parent() { // Parent may be a symlink @@ -325,7 +306,7 @@ fn install(disk_path: String, password_opt: Option, m .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; // Install files - let mut buf = vec![0; 4 * MIB as usize]; + let mut buf = vec![0; 4096 * 1024]; for (i, name) in files.iter().enumerate() { progress = (i * 100) / files.len(); message!("Copy {} [{}/{}]", name, i, files.len()); @@ -577,7 +558,7 @@ impl Application for Window { Message::DiskChoose ), space::horizontal(), - text(format_size(*disk_size)), + text(redox_installer::format_bytes(*disk_size)), ] .into(), ); diff --git a/src/bin/installer_tui.rs b/src/bin/installer_tui.rs index 2739983c41..a9094a5fd7 100644 --- a/src/bin/installer_tui.rs +++ b/src/bin/installer_tui.rs @@ -74,25 +74,6 @@ fn disk_paths(paths: &mut Vec<(PathBuf, u64)>) { } } -const KIB: u64 = 1024; -const MIB: u64 = 1024 * KIB; -const GIB: u64 = 1024 * MIB; -const TIB: u64 = 1024 * GIB; - -fn format_size(size: u64) -> String { - if size >= 4 * TIB { - format!("{:.1} TiB", size as f64 / TIB as f64) - } else if size >= GIB { - format!("{:.1} GiB", size as f64 / GIB as f64) - } else if size >= MIB { - format!("{:.1} MiB", size as f64 / MIB as f64) - } else if size >= KIB { - format!("{:.1} KiB", size as f64 / KIB as f64) - } else { - format!("{} B", size) - } -} - fn copy_file(src: &Path, dest: &Path, buf: &mut [u8]) -> Result<()> { if let Some(parent) = dest.parent() { // Parent may be a symlink @@ -220,7 +201,7 @@ fn choose_disk() -> PathBuf { "\x1B[1m{}\x1B[0m: {}: {}", i + 1, path.display(), - format_size(*size) + redox_installer::format_bytes(*size) ); } @@ -361,7 +342,7 @@ fn main() { files.dedup(); // Install files - let mut buf = vec![0; 4 * MIB as usize]; + let mut buf = vec![0; 4096 * 1024]; for (i, name) in files.iter().enumerate() { eprintln!("copy {} [{}/{}]", name, i, files.len()); diff --git a/src/config/file.rs b/src/config/file.rs index 5d4783b6ba..3a61cad194 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -66,11 +66,7 @@ impl Display for FileConfig { write!(f, " chown=yes")?; } } else { - write!( - f, - " size={}B", - arg_parser::to_human_readable_string(self.data.len() as u64) - )?; + write!(f, " size={}", crate::format_bytes(self.data.len() as u64))?; if self.postinstall { write!(f, "!")?; } diff --git a/src/installer.rs b/src/installer.rs index 3580e9d6a2..db2697213b 100644 --- a/src/installer.rs +++ b/src/installer.rs @@ -13,7 +13,9 @@ use crate::disk_wrapper::DiskWrapper; use std::{ cell::RefCell, collections::BTreeMap, - env, fs, + env, + fmt::Write as WriteFmt, + fs, io::{self, Seek, SeekFrom, Write}, path::{Path, PathBuf}, process, @@ -853,3 +855,32 @@ fn install_inner(config: Config, output: &Path) -> Result<()> { pub fn install(config: Config, output: impl AsRef) -> Result<()> { install_inner(config, output.as_ref()) } + +/// Convert bytes into readable string +pub fn format_bytes(len: u64) -> String { + const GB: u64 = 1024 * 1024 * 1024; + const MB: u64 = 1024 * 1024; + const KB: u64 = 1024; + + if len > GB { + format_bytes_inner(len, GB, "GB") + } else if len > MB { + format_bytes_inner(len, MB, "MB") + } else if len > KB { + format_bytes_inner(len, KB, "KB") + } else { + format!("{len} B") + } +} + +fn format_bytes_inner(len: u64, divisor: u64, suffix: &'static str) -> String { + let mut s = format!("{}", len / divisor); + if s.len() == 1 { + let _ = write!(s, ".{:02}", (len % divisor) / (divisor / 100)); + } else if s.len() == 2 { + let _ = write!(s, ".{:01}", (len % divisor) / (divisor / 10)); + } + + let _ = write!(s, " {suffix}"); + s +}