Skip to content

Commit 66bbf74

Browse files
committed
top: implement --scale-summary-mem
1 parent 4674776 commit 66bbf74

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/uu/top/src/header.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::picker::{sysinfo, systemstat};
22
use bytesize::ByteSize;
3+
use clap::ArgMatches;
34
use systemstat::Platform;
45
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
56
use {
@@ -34,21 +35,19 @@ pub(crate) fn cpu_load() -> CPULoad {
3435
}
3536
}
3637

37-
pub(crate) fn header() -> String {
38+
pub(crate) fn header(arg: &ArgMatches) -> String {
3839
format!(
3940
"top - {time} {uptime}, {user}, {load_average}\n\
4041
{task}\n\
4142
{cpu}\n\
42-
{memory}\n\
43-
{swap}",
43+
{memory}",
4444
time = chrono::Local::now().format("%H:%M:%S"),
4545
uptime = uptime(),
4646
user = user(),
4747
load_average = load_average(),
4848
task = task(),
4949
cpu = cpu(),
50-
memory = memory(),
51-
swap = swap(),
50+
memory = memory(arg),
5251
)
5352
}
5453

@@ -103,7 +102,7 @@ fn load_average() -> String {
103102
}
104103

105104
#[cfg(target_os = "windows")]
106-
fn load_average() -> String{
105+
fn load_average() -> String {
107106
todo()
108107
}
109108

@@ -203,30 +202,34 @@ fn cpu() -> String {
203202
todo()
204203
}
205204

206-
fn memory() -> String {
205+
fn memory(arg: &ArgMatches) -> String {
207206
let binding = sysinfo().read().unwrap();
208-
//TODO: unit from argument
209-
let unit = bytesize::MIB;
207+
let (unit, unit_name) = match arg.get_one::<String>("scale-summary-mem") {
208+
Some(scale) => match scale.as_str() {
209+
"k" => (bytesize::KIB, "KiB"),
210+
"m" => (bytesize::MIB, "MiB"),
211+
"g" => (bytesize::GIB, "GiB"),
212+
"t" => (bytesize::TIB, "TiB"),
213+
"p" => (bytesize::PIB, "PiB"),
214+
"e" => (1_152_921_504_606_846_976, "EiB"),
215+
_ => (bytesize::MIB, "MiB"),
216+
},
217+
None => {
218+
(bytesize::MIB, "MiB")
219+
}
220+
};
210221

211222
format!(
212-
"MiB Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache",
223+
"{unit_name} Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache\n\
224+
{unit_name} Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
213225
format_memory(binding.total_memory(), unit),
214226
format_memory(binding.free_memory(), unit),
215227
format_memory(binding.used_memory(), unit),
216-
format_memory(binding.total_memory() - binding.free_memory(), unit),
217-
)
218-
}
219-
220-
fn swap() -> String {
221-
let binding = sysinfo().read().unwrap();
222-
//TODO: unit from argument
223-
let unit = bytesize::MIB;
224-
225-
format!(
226-
"MiB Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
228+
format_memory(binding.available_memory() - binding.free_memory(), unit),
227229
format_memory(binding.total_swap(), unit),
228230
format_memory(binding.free_swap(), unit),
229231
format_memory(binding.used_swap(), unit),
230-
format_memory(binding.total_memory() - binding.free_memory(), unit),
232+
format_memory(binding.available_memory(), unit),
233+
unit_name = unit_name
231234
)
232235
}

src/uu/top/src/top.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
102102
table
103103
};
104104

105-
println!("{}", header());
105+
println!("{}", header(&matches));
106106
println!("\n");
107107

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

0 commit comments

Comments
 (0)