Skip to content

Commit 42630cc

Browse files
author
0l3d
committed
change: Memory usage: reading from /proc/meminfo instead of sysinfo
1 parent aaf74af commit 42630cc

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/target
1+
/targe

src/ffetch.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ pub mod ffetch {
55
use rsbash::rash;
66
use std::env;
77
use std::{fs::read_to_string, process::Command};
8-
use sysinfo::{Disks, System};
8+
use sysinfo::{Disks};
99
use which::which;
1010
use whoami;
11-
11+
1212
lazy_static! {
1313
static ref DISPLAY_INFORMATION: Vec<DisplayInfo> = DisplayInfo::all().unwrap();
1414
static ref REGEX: Regex = Regex::new(r"window id # (0x[0-9a-f]+)").unwrap();
@@ -17,7 +17,6 @@ pub mod ffetch {
1717

1818
pub fn get_kernel_version() -> String {
1919
let mut kernel_result: Vec<String> = Vec::new();
20-
2120
for line in read_to_string("/proc/version")
2221
.expect("you are not using linux (/proc/version is empty)")
2322
.lines()
@@ -26,7 +25,6 @@ pub mod ffetch {
2625
}
2726

2827
let kernel_result_full: Vec<_> = kernel_result[0].split(" ").collect();
29-
3028
kernel_result_full[2].to_string()
3129
}
3230

@@ -53,7 +51,9 @@ pub mod ffetch {
5351

5452
result_full.to_string()
5553
}
54+
/*
5655
56+
Old Code
5757
pub fn get_memory() -> String {
5858
let mut sys = System::new();
5959
sys.refresh_memory();
@@ -62,7 +62,34 @@ pub mod ffetch {
6262
6363
format!("{} / {}", used_memory, total_memory)
6464
}
65-
65+
*/
66+
67+
pub fn get_memory() -> String {
68+
let mut total_memory : u64 = 0;
69+
let mut free_memory : u64 = 0;
70+
for line in read_to_string("/proc/meminfo").expect("meminfo read error").lines() {
71+
let spline : Vec<&str> = line.split(":").collect();
72+
match spline[0] {
73+
"MemTotal" => {
74+
let total_memory_split : Vec<_>= spline[1].split("kB").collect();
75+
let total_memory_trim = total_memory_split[0].trim();
76+
let _total_memory_asint : u64 = total_memory_trim.parse().unwrap();
77+
total_memory = _total_memory_asint / 1024;
78+
}
79+
"MemAvailable" => {
80+
let free_memory_split : Vec<_> = spline[1].split("kB").collect();
81+
let free_memory_trim = free_memory_split[0].trim();
82+
let _free_memory_asint : u64 = free_memory_trim.parse().unwrap();
83+
free_memory = _free_memory_asint / 1024;
84+
}
85+
_ => {}
86+
}
87+
}
88+
let memory_usage = total_memory - free_memory;
89+
return format!("{} / {}", memory_usage, total_memory);
90+
}
91+
92+
6693
pub fn get_os_name() -> String {
6794
let mut osname_result: Vec<String> = Vec::new();
6895

0 commit comments

Comments
 (0)