Skip to content

Commit

Permalink
add --line
Browse files Browse the repository at this point in the history
  • Loading branch information
D3V1LC0D3R authored and D3V1LC0D3R committed Apr 29, 2024
1 parent 23acca9 commit 2f582c8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 84 deletions.
181 changes: 97 additions & 84 deletions src/uu/free/src/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let seconds_flag = matches.get_one("seconds");
let seconds: f64 = seconds_flag.unwrap_or(&1.0_f64).to_owned();
let committed = matches.get_flag("committed");
let one_line = matches.get_flag("line");

let dur = Duration::from_nanos(seconds.mul(1_000_000_000.0).round() as u64);
let convert = detect_unit(&matches);
Expand Down Expand Up @@ -252,96 +253,109 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
true => humanized(x, si),
false => convert(x).to_string(),
};

if wide {
wide_header();
if one_line {
println!(
"{:8}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}",
"Mem:",
n2s(mem_info.total),
"{:8}{:>12} {:8}{:>12} {:8}{:>12} {:8}{:>12}",
"SwapUse",
n2s(mem_info.swap_used),
"CacheUse",
n2s(buff_cache + mem_info.reclaimable),
"MemUse",
n2s(used),
n2s(mem_info.free),
n2s(mem_info.shared),
n2s(buff_cache),
n2s(cache + mem_info.reclaimable),
n2s(mem_info.available),
"MemFree",
n2s(mem_info.free)
);
} else {
header();
println!(
"{:8}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}",
"Mem:",
n2s(mem_info.total),
n2s(used),
n2s(mem_info.free),
n2s(mem_info.shared),
n2s(buff_cache + mem_info.reclaimable),
n2s(mem_info.available),
)
}

if lohi {
tuf_combo(
"Low:",
mem_info.low_total,
mem_info.low_used,
mem_info.low_free.into(),
n2s,
);
tuf_combo(
"High:",
mem_info.high_total,
mem_info.high_used,
mem_info.free.into(),
n2s,
);
}

if minmax {
let l = min.as_ref().unwrap();
tuf_combo(
"MinMem:",
l.total,
l.total - l.available,
l.free.into(),
n2s,
);

let h = max.as_ref().unwrap();
tuf_combo(
"MaxMem:",
h.total,
h.total - h.available,
h.free.into(),
n2s,
);
}

tuf_combo(
"Swap:",
mem_info.swap_total,
mem_info.swap_used,
mem_info.swap_free.into(),
n2s,
);
if total {
tuf_combo(
"Total:",
mem_info.total + mem_info.swap_total,
used + mem_info.swap_used,
(mem_info.free + mem_info.swap_free).into(),
n2s,
);
}
if wide {
wide_header();
println!(
"{:8}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}",
"Mem:",
n2s(mem_info.total),
n2s(used),
n2s(mem_info.free),
n2s(mem_info.shared),
n2s(buff_cache),
n2s(cache + mem_info.reclaimable),
n2s(mem_info.available),
);
} else {
header();
println!(
"{:8}{:>12}{:>12}{:>12}{:>12}{:>12}{:>12}",
"Mem:",
n2s(mem_info.total),
n2s(used),
n2s(mem_info.free),
n2s(mem_info.shared),
n2s(buff_cache + mem_info.reclaimable),
n2s(mem_info.available),
)
}

if lohi {
tuf_combo(
"Low:",
mem_info.low_total,
mem_info.low_used,
mem_info.low_free.into(),
n2s,
);
tuf_combo(
"High:",
mem_info.high_total,
mem_info.high_used,
mem_info.free.into(),
n2s,
);
}

if minmax {
let l = min.as_ref().unwrap();
tuf_combo(
"MinMem:",
l.total,
l.total - l.available,
l.free.into(),
n2s,
);

let h = max.as_ref().unwrap();
tuf_combo(
"MaxMem:",
h.total,
h.total - h.available,
h.free.into(),
n2s,
);
}

if committed {
tuf_combo(
"Comm:",
mem_info.commit_limit,
mem_info.committed,
(mem_info.commit_limit as i128) - (mem_info.committed as i128),
"Swap:",
mem_info.swap_total,
mem_info.swap_used,
mem_info.swap_free.into(),
n2s,
);
if total {
tuf_combo(
"Total:",
mem_info.total + mem_info.swap_total,
used + mem_info.swap_used,
(mem_info.free + mem_info.swap_free).into(),
n2s,
);
}

if committed {
tuf_combo(
"Comm:",
mem_info.commit_limit,
mem_info.committed,
(mem_info.commit_limit as i128) - (mem_info.committed as i128),
n2s,
);
}
}
}
Err(e) => {
Expand Down Expand Up @@ -398,8 +412,7 @@ pub fn uu_app() -> Command {
arg!(-c --count "repeat printing N times, then exit")
.action(ArgAction::Set)
.value_parser(clap::value_parser!(u64)),
// TODO:
// arg!(-L --line "show output on a single line").action(),
arg!(-L --line "show output on a single line").action(ArgAction::SetTrue),
])
.arg(
Arg::new("wide")
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ fn test_free_committed() {
.starts_with("Comm:"))
}

#[test]
fn test_free_always_one_line() {
// -L should ignore all other parameters and always print one line
let result = new_ucmd!().arg("-hltvwL").succeeds();
let stdout = result.stdout_str().lines().collect::<Vec<&str>>();
assert_eq!(stdout.len(), 1);
assert!(stdout[0].starts_with("SwapUse"));
}

#[test]
fn test_free_column_format() {
let re_head_str = r"^ {15}total {8}used {8}free {6}shared {2}buff/cache {3}available$";
Expand Down

0 comments on commit 2f582c8

Please sign in to comment.