Skip to content

Commit 82b3fbd

Browse files
committed
top: impl mem info
1 parent 655dc91 commit 82b3fbd

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/uu/top/src/header.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use crate::picker::sysinfo;
77
use crate::platform::*;
8-
use crate::{CpuGraphMode, CpuValueMode, Settings};
8+
use crate::{CpuGraphMode, CpuValueMode, MemoryGraphMode, Settings};
99
use bytesize::ByteSize;
1010
use uu_vmstat::CpuLoad;
1111
use uu_w::get_formatted_uptime_procps;
@@ -31,7 +31,11 @@ pub(crate) fn header(settings: &Settings) -> String {
3131
)
3232
};
3333

34-
let memory_line = memory(settings.scale_summary_mem.as_ref());
34+
let memory_line = if settings.memory_graph_mode == MemoryGraphMode::Hide {
35+
String::new()
36+
} else {
37+
memory(settings)
38+
};
3539

3640
format!("{uptime_line}{task_and_cpu}{memory_line}")
3741
}
@@ -168,9 +172,9 @@ fn cpu_line(tag: &str, cpu_load: &CpuLoad, settings: &Settings) -> String {
168172
format!("%{tag:<6}: {:>5.1}/{:<5.1}", cpu_load.user, cpu_load.system)
169173
}
170174

171-
fn memory(scale_summary_mem: Option<&String>) -> String {
175+
fn memory(settings: &Settings) -> String {
172176
let binding = sysinfo().read().unwrap();
173-
let (unit, unit_name) = match scale_summary_mem {
177+
let (unit, unit_name) = match settings.scale_summary_mem.as_ref() {
174178
Some(scale) => match scale.as_str() {
175179
"k" => (bytesize::KIB, "KiB"),
176180
"m" => (bytesize::MIB, "MiB"),
@@ -183,17 +187,29 @@ fn memory(scale_summary_mem: Option<&String>) -> String {
183187
None => (bytesize::MIB, "MiB"),
184188
};
185189

190+
if settings.memory_graph_mode == MemoryGraphMode::Sum {
191+
return format!(
192+
"{unit_name} Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache\n\
193+
{unit_name} Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
194+
format_memory(binding.total_memory(), unit),
195+
format_memory(binding.free_memory(), unit),
196+
format_memory(binding.used_memory(), unit),
197+
format_memory(binding.available_memory() - binding.free_memory(), unit),
198+
format_memory(binding.total_swap(), unit),
199+
format_memory(binding.free_swap(), unit),
200+
format_memory(binding.used_swap(), unit),
201+
format_memory(binding.available_memory(), unit),
202+
unit_name = unit_name
203+
);
204+
}
205+
206+
// TODO: render colored bar chart or block chart
186207
format!(
187-
"{unit_name} Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache\n\
188-
{unit_name} Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem",
189-
format_memory(binding.total_memory(), unit),
190-
format_memory(binding.free_memory(), unit),
208+
"GiB Mem : {:>5.1}/{:<5.1}\n\
209+
GiB Swap : {:>5.1}/{:<5.1}",
191210
format_memory(binding.used_memory(), unit),
192-
format_memory(binding.available_memory() - binding.free_memory(), unit),
193-
format_memory(binding.total_swap(), unit),
194-
format_memory(binding.free_swap(), unit),
211+
format_memory(binding.total_memory(), unit),
195212
format_memory(binding.used_swap(), unit),
196-
format_memory(binding.available_memory(), unit),
197-
unit_name = unit_name
213+
format_memory(binding.total_swap(), unit)
198214
)
199215
}

src/uu/top/src/top.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,24 @@ enum CpuValueMode {
4949
Sum,
5050
}
5151

52+
#[allow(unused)]
53+
#[derive(Debug, Default, PartialEq)]
54+
enum MemoryGraphMode {
55+
#[default]
56+
Block,
57+
Bar,
58+
Sum,
59+
Hide,
60+
}
61+
5262
#[derive(Debug)]
5363
pub(crate) struct Settings {
5464
// batch:bool
5565
filter: Option<Filter>,
5666
width: Option<usize>,
5767
cpu_graph_mode: CpuGraphMode,
5868
cpu_value_mode: CpuValueMode,
69+
memory_graph_mode: MemoryGraphMode,
5970
scale_summary_mem: Option<String>,
6071
}
6172

@@ -68,6 +79,7 @@ impl Settings {
6879
filter: None,
6980
cpu_graph_mode: CpuGraphMode::default(),
7081
cpu_value_mode: CpuValueMode::default(),
82+
memory_graph_mode: MemoryGraphMode::default(),
7183
scale_summary_mem: matches.get_one::<String>("scale-summary-mem").cloned(),
7284
}
7385
}

0 commit comments

Comments
 (0)