humansize

This commit is contained in:
Ryan Ragona
2025-04-27 08:35:21 -07:00
parent e96055be36
commit 66a2e970f7
3 changed files with 22 additions and 22 deletions

16
codex-rs/Cargo.lock generated
View File

@@ -590,6 +590,7 @@ dependencies = [
"codex-exec",
"codex-repl",
"dirs",
"humansize",
"libc",
"names",
"nix 0.27.1",
@@ -1472,6 +1473,15 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humansize"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
dependencies = [
"libm",
]
[[package]]
name = "hyper"
version = "1.6.0"
@@ -1906,6 +1916,12 @@ version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "libm"
version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9627da5196e5d8ed0b0495e61e518847578da83483c37288316d9b2e03a7f72"
[[package]]
name = "libredox"
version = "0.1.3"

View File

@@ -39,6 +39,7 @@ rand = "0.9.1"
# Re-use the codex-exec library for its CLI definition
codex_exec = { package = "codex-exec", path = "../exec" }
codex_repl = { package = "codex-repl", path = "../repl" }
humansize = "2.1.3"
[dev-dependencies]
tempfile = "3"

View File

@@ -17,31 +17,12 @@ use chrono::SecondsFormat;
use clap::Args;
use clap::Parser;
use clap::Subcommand;
// Platform-specific imports
#[cfg(unix)]
use codex_repl as _;
use petname::Generator;
use petname::Petnames;
use serde::Serialize;
/// A human-friendly representation of a byte count (e.g. 1.4M).
pub fn human_bytes(b: u64) -> String {
const KB: f64 = 1024.0;
const MB: f64 = KB * 1024.0;
const GB: f64 = MB * 1024.0;
let f = b as f64;
if f >= GB {
format!("{:.1}G", f / GB)
} else if f >= MB {
format!("{:.1}M", f / MB)
} else if f >= KB {
format!("{:.1}K", f / KB)
} else {
format!("{}B", b)
}
}
#[cfg(unix)]
use codex_repl as _;
#[derive(Parser)]
#[command(
@@ -397,6 +378,8 @@ impl ListCmd {
let mut sys = sysinfo::System::new();
sys.refresh_processes();
let bytes_formatter = humansize::make_format(humansize::DECIMAL);
let rows: Vec<StatusRow> = metas
.into_iter()
.enumerate()
@@ -413,7 +396,7 @@ impl ListCmd {
let (out, err) = if let Some(p) = &paths {
let osz = std::fs::metadata(&p.stdout).map(|m| m.len()).unwrap_or(0);
let esz = std::fs::metadata(&p.stderr).map(|m| m.len()).unwrap_or(0);
(human_bytes(osz), human_bytes(esz))
(bytes_formatter(osz), bytes_formatter(esz))
} else {
("-".into(), "-".into())
};