Skip to content

Commit

Permalink
determine memory unit from --scale-summary-mem
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluemangoo committed Jan 15, 2025
1 parent b58b2ba commit d535605
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
48 changes: 26 additions & 22 deletions src/uu/top/src/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::picker::{sysinfo, systemstat};
use bytesize::ByteSize;
use clap::ArgMatches;
use systemstat::Platform;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
use {
Expand Down Expand Up @@ -34,21 +35,19 @@ pub(crate) fn cpu_load() -> CPULoad {
}
}

pub(crate) fn header() -> String {
pub(crate) fn header(arg: &ArgMatches) -> String {
format!(
"top - {time} {uptime}, {user}, {load_average}\n\
{task}\n\
{cpu}\n\
{memory}\n\
{swap}",
{memory}",
time = chrono::Local::now().format("%H:%M:%S"),
uptime = uptime(),
user = user(),
load_average = load_average(),
task = task(),
cpu = cpu(),
memory = memory(),
swap = swap(),
memory = memory(arg),
)
}

Expand Down Expand Up @@ -103,7 +102,7 @@ fn load_average() -> String {
}

#[cfg(target_os = "windows")]
fn load_average() -> String{
fn load_average() -> String {
todo()
}

Expand Down Expand Up @@ -203,30 +202,35 @@ fn cpu() -> String {
todo()
}

fn memory() -> String {
fn memory(arg: &ArgMatches) -> String {
let binding = sysinfo().read().unwrap();
//TODO: unit from argument
let unit = bytesize::MIB;
let (unit, unit_name) = match arg.get_one::<String>("scale-summary-mem") {
Some(scale) => match scale.as_str() {
"k" => (bytesize::KIB, "KiB"),
"m" => (bytesize::MIB, "MiB"),
"g" => (bytesize::GIB, "GiB"),
"t" => (bytesize::TIB, "TiB"),
"p" => (bytesize::PIB, "PiB"),
"e" => (1_152_921_504_606_846_976, "EiB"),
_ => (bytesize::MIB, "MiB"),
},
None => {
println!("none");
(bytesize::MIB, "MiB")
}
};

format!(
"MiB Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache",
"{unit_name} Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache\n\
{unit_name} Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
format_memory(binding.total_memory(), unit),
format_memory(binding.free_memory(), unit),
format_memory(binding.used_memory(), unit),
format_memory(binding.total_memory() - binding.free_memory(), unit),
)
}

fn swap() -> String {
let binding = sysinfo().read().unwrap();
//TODO: unit from argument
let unit = bytesize::MIB;

format!(
"MiB Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
format_memory(binding.available_memory() - binding.free_memory(), unit),
format_memory(binding.total_swap(), unit),
format_memory(binding.free_swap(), unit),
format_memory(binding.used_swap(), unit),
format_memory(binding.total_memory() - binding.free_memory(), unit),
format_memory(binding.available_memory(), unit),
unit_name = unit_name
)
}
4 changes: 2 additions & 2 deletions src/uu/top/src/top.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
table
};

println!("{}", header());
println!("{}", header(&matches));
println!("\n");

let cutter = {
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn uu_app() -> Command {
// arg!(-b --"batch-mode" "run in non-interactive batch mode"),
// arg!(-c --"cmdline-toggle" "reverse last remembered 'c' state"),
// arg!(-d --delay <SECS> "iterative delay as SECS [.TENTHS]"),
// arg!(-E --"scale-summary-mem" <SCALE> "set mem as: k,m,g,t,p,e for SCALE"),
arg!(-E --"scale-summary-mem" <SCALE> "set mem as: k,m,g,t,p,e for SCALE"),
// arg!(-e --"scale-task-mem" <SCALE> "set mem with: k,m,g,t,p for SCALE"),
// arg!(-H --"threads-show" "show tasks plus all their threads"),
// arg!(-i --"idle-toggle" "reverse last remembered 'i' state"),
Expand Down

0 comments on commit d535605

Please sign in to comment.