diff --git a/src/uu/top/src/header.rs b/src/uu/top/src/header.rs index 4c0d2b0..ed92e76 100644 --- a/src/uu/top/src/header.rs +++ b/src/uu/top/src/header.rs @@ -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 { @@ -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), ) } @@ -103,7 +102,7 @@ fn load_average() -> String { } #[cfg(target_os = "windows")] -fn load_average() -> String{ +fn load_average() -> String { todo() } @@ -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::("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 ) } diff --git a/src/uu/top/src/top.rs b/src/uu/top/src/top.rs index b9e8c37..421a2c6 100644 --- a/src/uu/top/src/top.rs +++ b/src/uu/top/src/top.rs @@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { table }; - println!("{}", header()); + println!("{}", header(&matches)); println!("\n"); let cutter = { @@ -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 "iterative delay as SECS [.TENTHS]"), - // arg!(-E --"scale-summary-mem" "set mem as: k,m,g,t,p,e for SCALE"), + arg!(-E --"scale-summary-mem" "set mem as: k,m,g,t,p,e for SCALE"), // arg!(-e --"scale-task-mem" "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"),