Use builtin bytes format string

This commit is contained in:
Wildan M
2026-06-16 14:48:13 +07:00
parent 7166bee750
commit 227606752d
4 changed files with 37 additions and 48 deletions
+2 -21
View File
@@ -34,25 +34,6 @@ fn main() -> iced::Result {
app::run::<Window>(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<F: FnMut(Message)>(disk_path: String, password_opt: Option<String>, 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(),
);
+2 -21
View File
@@ -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());
+1 -5
View File
@@ -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, "!")?;
}
+32 -1
View File
@@ -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<Path>) -> 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
}