|
1 | 1 | use crate::picker::{sysinfo, systemstat}; |
2 | 2 | use bytesize::ByteSize; |
| 3 | +use clap::ArgMatches; |
3 | 4 | use systemstat::Platform; |
4 | 5 | #[cfg(not(any(target_os = "macos", target_os = "linux")))] |
5 | 6 | use { |
@@ -34,21 +35,19 @@ pub(crate) fn cpu_load() -> CPULoad { |
34 | 35 | } |
35 | 36 | } |
36 | 37 |
|
37 | | -pub(crate) fn header() -> String { |
| 38 | +pub(crate) fn header(arg: &ArgMatches) -> String { |
38 | 39 | format!( |
39 | 40 | "top - {time} {uptime}, {user}, {load_average}\n\ |
40 | 41 | {task}\n\ |
41 | 42 | {cpu}\n\ |
42 | | - {memory}\n\ |
43 | | - {swap}", |
| 43 | + {memory}", |
44 | 44 | time = chrono::Local::now().format("%H:%M:%S"), |
45 | 45 | uptime = uptime(), |
46 | 46 | user = user(), |
47 | 47 | load_average = load_average(), |
48 | 48 | task = task(), |
49 | 49 | cpu = cpu(), |
50 | | - memory = memory(), |
51 | | - swap = swap(), |
| 50 | + memory = memory(arg), |
52 | 51 | ) |
53 | 52 | } |
54 | 53 |
|
@@ -103,7 +102,7 @@ fn load_average() -> String { |
103 | 102 | } |
104 | 103 |
|
105 | 104 | #[cfg(target_os = "windows")] |
106 | | -fn load_average() -> String{ |
| 105 | +fn load_average() -> String { |
107 | 106 | todo() |
108 | 107 | } |
109 | 108 |
|
@@ -203,30 +202,35 @@ fn cpu() -> String { |
203 | 202 | todo() |
204 | 203 | } |
205 | 204 |
|
206 | | -fn memory() -> String { |
| 205 | +fn memory(arg: &ArgMatches) -> String { |
207 | 206 | 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 | + println!("none"); |
| 219 | + (bytesize::MIB, "MiB") |
| 220 | + } |
| 221 | + }; |
210 | 222 |
|
211 | 223 | format!( |
212 | | - "MiB Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache", |
| 224 | + "{unit_name} Mem : {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} buff/cache\n\ |
| 225 | + {unit_name} Swap: {:8.1} total, {:8.1} free, {:8.1} used, {:8.1} avail Mem", |
213 | 226 | format_memory(binding.total_memory(), unit), |
214 | 227 | format_memory(binding.free_memory(), unit), |
215 | 228 | 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", |
| 229 | + format_memory(binding.available_memory() - binding.free_memory(), unit), |
227 | 230 | format_memory(binding.total_swap(), unit), |
228 | 231 | format_memory(binding.free_swap(), unit), |
229 | 232 | format_memory(binding.used_swap(), unit), |
230 | | - format_memory(binding.total_memory() - binding.free_memory(), unit), |
| 233 | + format_memory(binding.available_memory(), unit), |
| 234 | + unit_name = unit_name |
231 | 235 | ) |
232 | 236 | } |
0 commit comments