From 8bad155f1598ce2eb588da439c04f7c5af9e9588 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Tue, 3 Jun 2025 17:26:30 -0700 Subject: [PATCH] Added memory module --- src/bar_modules/bar_module_memory.rs | 70 ++++++++++++++++++++++++++++ src/bar_modules/mod.rs | 1 + src/main.rs | 2 + 3 files changed, 73 insertions(+) create mode 100644 src/bar_modules/bar_module_memory.rs diff --git a/src/bar_modules/bar_module_memory.rs b/src/bar_modules/bar_module_memory.rs new file mode 100644 index 0000000..77be505 --- /dev/null +++ b/src/bar_modules/bar_module_memory.rs @@ -0,0 +1,70 @@ +use std::fs::File; +use std::path::Path; +use std::io::prelude::*; +use regex::Regex; + +use crate::common; +use crate::bar_modules; + +#[derive(Clone)] +pub struct UnibarModuleMemory { + opts: common::AppOptions, + meminfo_str: String, +} + +impl UnibarModuleMemory { + + // -------------------- + pub fn new(o :common::AppOptions) -> Self { + UnibarModuleMemory { + opts: o, + meminfo_str: "".to_string(), + } + } +} + +impl bar_modules::BarModuleActions for UnibarModuleMemory { + + // -------------------- + fn generate_data(&mut self) { + let path = Path::new("/proc/meminfo"); + + // Contents of '/process/meminfo' has memory information + let mut meminfo_file = match File::open(&path) { + Err(why) => panic!("couldn't open {}: {}", path.display(), why), + Ok(file) => file, + }; + + match meminfo_file.read_to_string(&mut self.meminfo_str) { + Err(why) => panic!("couldn't read {}: {}", path.display(), why), + Ok(_) => if self.opts.debug_json { + println!("-----> meminfo - {:#?}", self.meminfo_str); + }, + }; + self.meminfo_str.pop(); + + } + + // -------------------- + // MemTotal: 7822812 kB\nMemFree: 399244 kB\nMemAvailable: 3986504 kB + fn get_content(&self) -> String { + let re = Regex::new(r"MemTotal:\s+(\d+)\s+kB\nMemFree:\s+(\d+)").unwrap(); + let caps = re.captures(self.meminfo_str.as_str()).unwrap(); + + let total_mem :f32 = caps.get(1).unwrap().as_str().parse::().unwrap(); + let free_mem :f32 = caps.get(2).unwrap().as_str().parse::().unwrap(); + return format!("{}%", ((total_mem - free_mem)/total_mem * 100.0).ceil() as i32); + } + + // -------------------- + fn get_icon(&self) -> String { + return "💾".to_string(); + } +} + +impl bar_modules::BarModuleDebug for UnibarModuleMemory { + + // -------------------- + fn post_debug(&self) { + } +} diff --git a/src/bar_modules/mod.rs b/src/bar_modules/mod.rs index facfcf6..9c364a4 100644 --- a/src/bar_modules/mod.rs +++ b/src/bar_modules/mod.rs @@ -20,3 +20,4 @@ pub trait BarModuleDebug { pub mod bar_module_weather; pub mod bar_module_music; pub mod bar_module_network; +pub mod bar_module_memory; diff --git a/src/main.rs b/src/main.rs index e8fd25e..215dd94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,6 +54,7 @@ impl Unibar { // Set up a list of all modules to be used let bar_modules_enabled: Vec> = vec! [ Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())), + Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::new(self.opts.clone())), Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::new(self.opts.clone())), Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())), ]; @@ -82,6 +83,7 @@ impl Unibar { let bar_modules_debugged: Vec> = vec! [ Box::new(bar_modules::bar_module_weather::UnibarModuleWeather::new(self.opts.clone())), Box::new(bar_modules::bar_module_music::UnibarModuleMusic::new(self.opts.clone())), + Box::new(bar_modules::bar_module_memory::UnibarModuleMemory::new(self.opts.clone())), Box::new(bar_modules::bar_module_network::UnibarModuleNetwork::new(self.opts.clone())), ]; for md in bar_modules_debugged {