mirror of
				https://github.com/NotAShelf/microfetch.git
				synced 2025-11-03 23:32:19 +00:00 
			
		
		
		
	fix: changed memory calculation to parse meminfo
This commit is contained in:
		
					parent
					
						
							
								927f6077b4
							
						
					
				
			
			
				commit
				
					
						3047cfb51c
					
				
			
		
					 2 changed files with 32 additions and 16 deletions
				
			
		| 
						 | 
					@ -11,7 +11,6 @@ use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_userna
 | 
				
			||||||
use crate::uptime::get_current;
 | 
					use crate::uptime::get_current;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use color_eyre::Report;
 | 
					use color_eyre::Report;
 | 
				
			||||||
use nix::sys::sysinfo::sysinfo;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() -> Result<(), Report> {
 | 
					fn main() -> Result<(), Report> {
 | 
				
			||||||
    color_eyre::install()?;
 | 
					    color_eyre::install()?;
 | 
				
			||||||
| 
						 | 
					@ -23,7 +22,7 @@ fn main() -> Result<(), Report> {
 | 
				
			||||||
        shell: get_shell()?,
 | 
					        shell: get_shell()?,
 | 
				
			||||||
        uptime: get_current()?,
 | 
					        uptime: get_current()?,
 | 
				
			||||||
        window_manager: get_desktop_info()?,
 | 
					        window_manager: get_desktop_info()?,
 | 
				
			||||||
        memory_usage: get_memory_usage(sysinfo()?),
 | 
					        memory_usage: get_memory_usage()?,
 | 
				
			||||||
        storage: get_root_disk_usage()?,
 | 
					        storage: get_root_disk_usage()?,
 | 
				
			||||||
        colors: print_dots()?,
 | 
					        colors: print_dots()?,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,11 +1,11 @@
 | 
				
			||||||
use color_eyre::Result;
 | 
					use color_eyre::Result;
 | 
				
			||||||
use nix::sys::statvfs::statvfs;
 | 
					use nix::sys::statvfs::statvfs;
 | 
				
			||||||
use nix::sys::sysinfo::SysInfo;
 | 
					use std::fs::File;
 | 
				
			||||||
 | 
					use std::io::{self, BufRead};
 | 
				
			||||||
use std::env;
 | 
					use std::path::Path;
 | 
				
			||||||
use std::io::{self};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
 | 
					use crate::colors::{CYAN, GREEN, RED, RESET, YELLOW};
 | 
				
			||||||
 | 
					use std::env;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_username_and_hostname() -> Result<String, io::Error> {
 | 
					pub fn get_username_and_hostname() -> Result<String, io::Error> {
 | 
				
			||||||
    let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string());
 | 
					    let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string());
 | 
				
			||||||
| 
						 | 
					@ -40,21 +40,38 @@ pub fn get_root_disk_usage() -> Result<String, io::Error> {
 | 
				
			||||||
    ))
 | 
					    ))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn get_memory_usage(info: SysInfo) -> String {
 | 
					pub fn get_memory_usage() -> Result<String, io::Error> {
 | 
				
			||||||
    #[inline(always)]
 | 
					    #[inline(always)]
 | 
				
			||||||
    fn parse_memory_info(info: SysInfo) -> (f64, f64) {
 | 
					    fn parse_memory_info() -> Result<(f64, f64), io::Error> {
 | 
				
			||||||
        let total_memory_kb = (info.ram_total() / 1024) as f64;
 | 
					        let path = Path::new("/proc/meminfo");
 | 
				
			||||||
        let available_memory_kb = (info.ram_unused() / 1024) as f64;
 | 
					        let file = File::open(&path)?;
 | 
				
			||||||
 | 
					        let reader = io::BufReader::new(file);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let total_memory_gb = total_memory_kb / (1024.0 * 1024.0);
 | 
					        let mut meminfo = std::collections::HashMap::new();
 | 
				
			||||||
        let available_memory_gb = available_memory_kb / (1024.0 * 1024.0);
 | 
					 | 
				
			||||||
        let used_memory_gb = total_memory_gb - available_memory_gb;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        (used_memory_gb, total_memory_gb)
 | 
					        for line in reader.lines() {
 | 
				
			||||||
 | 
					            let line = line?;
 | 
				
			||||||
 | 
					            let mut parts = line.split_whitespace();
 | 
				
			||||||
 | 
					            if let (Some(key), Some(value), Some(_)) = (parts.next(), parts.next(), parts.next()) {
 | 
				
			||||||
 | 
					                let key = key.trim_end_matches(':');
 | 
				
			||||||
 | 
					                let value: u64 = value.parse().unwrap_or(0);
 | 
				
			||||||
 | 
					                meminfo.insert(key.to_string(), value);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let total_memory = meminfo["MemTotal"];
 | 
				
			||||||
 | 
					        let used_memory = total_memory - meminfo["MemAvailable"];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let used_memory_gb = used_memory as f64 / (1024.0 * 1024.0);
 | 
				
			||||||
 | 
					        let total_memory_gb = total_memory as f64 / (1024.0 * 1024.0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok((used_memory_gb, total_memory_gb))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let (used_memory, total_memory) = parse_memory_info(info);
 | 
					    let (used_memory, total_memory) = parse_memory_info()?;
 | 
				
			||||||
    let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
 | 
					    let percentage_used = (used_memory / total_memory * 100.0).round() as u64;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    format!("{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})")
 | 
					    Ok(format!(
 | 
				
			||||||
 | 
					        "{used_memory:.2} GiB / {total_memory:.2} GiB ({CYAN}{percentage_used}%{RESET})"
 | 
				
			||||||
 | 
					    ))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue