Added memory module
This commit is contained in:
parent
9b79fcd904
commit
8bad155f15
70
src/bar_modules/bar_module_memory.rs
Normal file
70
src/bar_modules/bar_module_memory.rs
Normal file
@ -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::<f32>().unwrap();
|
||||
let free_mem :f32 = caps.get(2).unwrap().as_str().parse::<f32>().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) {
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -54,6 +54,7 @@ impl Unibar {
|
||||
// Set up a list of all modules to be used
|
||||
let bar_modules_enabled: Vec<Box<dyn bar_modules::BarModuleActions>> = 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<Box<dyn bar_modules::BarModuleDebug>> = 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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user