Added ram block
Browse files
+2 −2 src/bar/bar.rs
| @@ -40,7 +40,7 @@ impl Bar { | ||
| 40 | 40 | } |
| 41 | 41 | let font = Font::new(display, screen_num as i32, FONT)?; |
| 42 | 42 | |
| 43 | - let height = (font.height() as f32 * 1.5) as u16; | |
| 43 | + let height = (font.height() as f32 * 1.4) as u16; | |
| 44 | 44 | |
| 45 | 45 | connection.create_window( |
| 46 | 46 | COPY_DEPTH_FROM_PARENT, |
| @@ -297,7 +297,7 @@ impl Bar { | ||
| 297 | 297 | } |
| 298 | 298 | current_x_position += tag_width as i16; |
| 299 | 299 | } |
| 300 | - None | |
| 300 | + return None; | |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | pub fn needs_redraw(&self) -> bool { |
+4 −0 src/bar/blocks/mod.rs
| @@ -3,10 +3,12 @@ use std::time::Duration; | ||
| 3 | 3 | |
| 4 | 4 | mod battery; |
| 5 | 5 | mod datetime; |
| 6 | +mod ram; | |
| 6 | 7 | mod shell; |
| 7 | 8 | |
| 8 | 9 | use battery::Battery; |
| 9 | 10 | use datetime::DateTime; |
| 11 | +use ram::Ram; | |
| 10 | 12 | use shell::ShellBlock; |
| 11 | 13 | |
| 12 | 14 | pub trait Block { |
| @@ -31,6 +33,7 @@ pub enum BlockCommand { | ||
| 31 | 33 | format_discharging: &'static str, |
| 32 | 34 | format_full: &'static str, |
| 33 | 35 | }, |
| 36 | + Ram, | |
| 34 | 37 | Static(&'static str), |
| 35 | 38 | } |
| 36 | 39 | |
| @@ -60,6 +63,7 @@ impl BlockConfig { | ||
| 60 | 63 | self.interval_secs, |
| 61 | 64 | self.color, |
| 62 | 65 | )), |
| 66 | + BlockCommand::Ram => Box::new(Ram::new(self.format, self.interval_secs, self.color)), | |
| 63 | 67 | BlockCommand::Static(text) => Box::new(StaticBlock::new( |
| 64 | 68 | &format!("{}{}", self.format, text), |
| 65 | 69 | self.color, |
+79 −0 src/bar/blocks/ram.rs new file mode 100644
| @@ -0,0 +1,79 @@ | ||
| 1 | +use super::Block; | |
| 2 | +use anyhow::Result; | |
| 3 | +use std::fs; | |
| 4 | +use std::time::Duration; | |
| 5 | + | |
| 6 | +pub struct Ram { | |
| 7 | + format: String, | |
| 8 | + interval: Duration, | |
| 9 | + color: u32, | |
| 10 | +} | |
| 11 | + | |
| 12 | +impl Ram { | |
| 13 | + pub fn new(format: &str, interval_secs: u64, color: u32) -> Self { | |
| 14 | + Self { | |
| 15 | + format: format.to_string(), | |
| 16 | + interval: Duration::from_secs(interval_secs), | |
| 17 | + color, | |
| 18 | + } | |
| 19 | + } | |
| 20 | + | |
| 21 | + fn get_memory_info(&self) -> Result<(u64, u64, f32)> { | |
| 22 | + let meminfo = fs::read_to_string("/proc/meminfo")?; | |
| 23 | + let mut total: u64 = 0; | |
| 24 | + let mut available: u64 = 0; | |
| 25 | + | |
| 26 | + for line in meminfo.lines() { | |
| 27 | + if line.starts_with("MemTotal:") { | |
| 28 | + total = line | |
| 29 | + .split_whitespace() | |
| 30 | + .nth(1) | |
| 31 | + .and_then(|s| s.parse().ok()) | |
| 32 | + .unwrap_or(0) | |
| 33 | + } else if line.starts_with("MemAvailable:") { | |
| 34 | + available = line | |
| 35 | + .split_whitespace() | |
| 36 | + .nth(1) | |
| 37 | + .and_then(|s| s.parse().ok()) | |
| 38 | + .unwrap_or(0) | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + let used = total.saturating_sub(available); | |
| 43 | + let used_gb = used as f32 / 1024.0 / 1024.0; | |
| 44 | + let total_gb = total as f32 / 1024.0 / 1024.0; | |
| 45 | + let percentage = if total > 0 { | |
| 46 | + (used as f32 / total as f32) * 100.0 | |
| 47 | + } else { | |
| 48 | + 0.0 | |
| 49 | + }; | |
| 50 | + | |
| 51 | + Ok((used, total, percentage)) | |
| 52 | + } | |
| 53 | +} | |
| 54 | + | |
| 55 | +impl Block for Ram { | |
| 56 | + fn content(&mut self) -> Result<String> { | |
| 57 | + let (used, total, percentage) = self.get_memory_info()?; | |
| 58 | + | |
| 59 | + let used_gb = used as f32 / 1024.0 / 1024.0; | |
| 60 | + let total_gb = total as f32 / 1024.0 / 1024.0; | |
| 61 | + | |
| 62 | + let result = self | |
| 63 | + .format | |
| 64 | + .replace("{used}", &format!("{:.1}", used_gb)) | |
| 65 | + .replace("{total}", &format!("{:.1}", total_gb)) | |
| 66 | + .replace("{percent}", &format!("{:.1}", percentage)) | |
| 67 | + .replace("{}", &format!("{:.1}", used_gb)); | |
| 68 | + | |
| 69 | + Ok(result) | |
| 70 | + } | |
| 71 | + | |
| 72 | + fn interval(&self) -> Duration { | |
| 73 | + self.interval | |
| 74 | + } | |
| 75 | + | |
| 76 | + fn color(&self) -> u32 { | |
| 77 | + self.color | |
| 78 | + } | |
| 79 | +} |
+29 −15 src/config.rs
| @@ -6,24 +6,24 @@ use x11rb::protocol::xproto::KeyButMask; | ||
| 6 | 6 | // ======================================== |
| 7 | 7 | // APPEARANCE |
| 8 | 8 | // ======================================== |
| 9 | -pub const BORDER_WIDTH: u32 = 0; | |
| 9 | +pub const BORDER_WIDTH: u32 = 2; | |
| 10 | 10 | pub const BORDER_FOCUSED: u32 = 0x6dade3; |
| 11 | 11 | pub const BORDER_UNFOCUSED: u32 = 0xbbbbbb; |
| 12 | -pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=14"; | |
| 12 | +pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=10"; | |
| 13 | 13 | |
| 14 | 14 | // ======================================== |
| 15 | 15 | // GAPS (Vanity Gaps) |
| 16 | 16 | // ======================================== |
| 17 | 17 | pub const GAPS_ENABLED: bool = true; |
| 18 | -pub const GAP_INNER_HORIZONTAL: u32 = 3; | |
| 19 | -pub const GAP_INNER_VERTICAL: u32 = 3; | |
| 20 | -pub const GAP_OUTER_HORIZONTAL: u32 = 3; | |
| 21 | -pub const GAP_OUTER_VERTICAL: u32 = 3; | |
| 22 | -// | |
| 18 | +pub const GAP_INNER_HORIZONTAL: u32 = 6; | |
| 19 | +pub const GAP_INNER_VERTICAL: u32 = 6; | |
| 20 | +pub const GAP_OUTER_HORIZONTAL: u32 = 6; | |
| 21 | +pub const GAP_OUTER_VERTICAL: u32 = 6; | |
| 22 | + | |
| 23 | 23 | // ======================================== |
| 24 | 24 | // DEFAULTS |
| 25 | 25 | // ======================================== |
| 26 | -pub const TERMINAL: &str = "alacritty"; | |
| 26 | +pub const TERMINAL: &str = "st"; | |
| 27 | 27 | pub const XCLOCK: &str = "xclock"; |
| 28 | 28 | pub const MODKEY: KeyButMask = KeyButMask::MOD4; |
| 29 | 29 | |
| @@ -129,14 +129,28 @@ pub const KEYBINDINGS: &[Key] = &[ | ||
| 129 | 129 | // STATUS BAR BLOCKS |
| 130 | 130 | // ======================================== |
| 131 | 131 | pub const STATUS_BLOCKS: &[BlockConfig] = &[ |
| 132 | + // BlockConfig { | |
| 133 | + // format: "", | |
| 134 | + // command: BlockCommand::Battery { | |
| 135 | + // format_charging: " Bat: {}%", | |
| 136 | + // format_discharging: " Bat:{}%", | |
| 137 | + // format_full: " Bat: {}%", | |
| 138 | + // }, | |
| 139 | + // interval_secs: 30, | |
| 140 | + // color: BLUE, | |
| 141 | + // underline: true, | |
| 142 | + // }, | |
| 143 | + // BlockConfig { | |
| 144 | + // format: " | ", | |
| 145 | + // command: BlockCommand::Static(""), | |
| 146 | + // interval_secs: u64::MAX, | |
| 147 | + // color: GRAY_LIGHT, | |
| 148 | + // underline: false, | |
| 149 | + // }, | |
| 132 | 150 | BlockConfig { |
| 133 | - format: "", | |
| 134 | - command: BlockCommand::Battery { | |
| 135 | - format_charging: " Bat: {}%", | |
| 136 | - format_discharging: " Bat:{}%", | |
| 137 | - format_full: " Bat: {}%", | |
| 138 | - }, | |
| 139 | - interval_secs: 30, | |
| 151 | + format: " {used}/{total} GB", | |
| 152 | + command: BlockCommand::Ram, | |
| 153 | + interval_secs: 5, | |
| 140 | 154 | color: BLUE, |
| 141 | 155 | underline: true, |
| 142 | 156 | }, |