oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

Added ram block

Commit
93d1e6ee3bf90cb8f9dae44d541be8de636544a7
Parent
d1aa40f
Author
tonybanters <tony@tonybtw.com>
Date
2025-10-07 15:35:25

Diff

diff --git a/src/bar/bar.rs b/src/bar/bar.rs
index 53f0cf6..1d2b7cc 100644
--- a/src/bar/bar.rs
+++ b/src/bar/bar.rs
@@ -40,7 +40,7 @@ impl Bar {
         }
         let font = Font::new(display, screen_num as i32, FONT)?;
 
-        let height = (font.height() as f32 * 1.5) as u16;
+        let height = (font.height() as f32 * 1.4) as u16;
 
         connection.create_window(
             COPY_DEPTH_FROM_PARENT,
@@ -297,7 +297,7 @@ impl Bar {
             }
             current_x_position += tag_width as i16;
         }
-        None
+        return None;
     }
 
     pub fn needs_redraw(&self) -> bool {
diff --git a/src/bar/blocks/mod.rs b/src/bar/blocks/mod.rs
index 0a9986a..e1babfb 100644
--- a/src/bar/blocks/mod.rs
+++ b/src/bar/blocks/mod.rs
@@ -3,10 +3,12 @@ use std::time::Duration;
 
 mod battery;
 mod datetime;
+mod ram;
 mod shell;
 
 use battery::Battery;
 use datetime::DateTime;
+use ram::Ram;
 use shell::ShellBlock;
 
 pub trait Block {
@@ -31,6 +33,7 @@ pub enum BlockCommand {
         format_discharging: &'static str,
         format_full: &'static str,
     },
+    Ram,
     Static(&'static str),
 }
 
@@ -60,6 +63,7 @@ impl BlockConfig {
                 self.interval_secs,
                 self.color,
             )),
+            BlockCommand::Ram => Box::new(Ram::new(self.format, self.interval_secs, self.color)),
             BlockCommand::Static(text) => Box::new(StaticBlock::new(
                 &format!("{}{}", self.format, text),
                 self.color,
diff --git a/src/bar/blocks/ram.rs b/src/bar/blocks/ram.rs
new file mode 100644
index 0000000..025525b
--- /dev/null
+++ b/src/bar/blocks/ram.rs
@@ -0,0 +1,79 @@
+use super::Block;
+use anyhow::Result;
+use std::fs;
+use std::time::Duration;
+
+pub struct Ram {
+    format: String,
+    interval: Duration,
+    color: u32,
+}
+
+impl Ram {
+    pub fn new(format: &str, interval_secs: u64, color: u32) -> Self {
+        Self {
+            format: format.to_string(),
+            interval: Duration::from_secs(interval_secs),
+            color,
+        }
+    }
+
+    fn get_memory_info(&self) -> Result<(u64, u64, f32)> {
+        let meminfo = fs::read_to_string("/proc/meminfo")?;
+        let mut total: u64 = 0;
+        let mut available: u64 = 0;
+
+        for line in meminfo.lines() {
+            if line.starts_with("MemTotal:") {
+                total = line
+                    .split_whitespace()
+                    .nth(1)
+                    .and_then(|s| s.parse().ok())
+                    .unwrap_or(0)
+            } else if line.starts_with("MemAvailable:") {
+                available = line
+                    .split_whitespace()
+                    .nth(1)
+                    .and_then(|s| s.parse().ok())
+                    .unwrap_or(0)
+            }
+        }
+
+        let used = total.saturating_sub(available);
+        let used_gb = used as f32 / 1024.0 / 1024.0;
+        let total_gb = total as f32 / 1024.0 / 1024.0;
+        let percentage = if total > 0 {
+            (used as f32 / total as f32) * 100.0
+        } else {
+            0.0
+        };
+
+        Ok((used, total, percentage))
+    }
+}
+
+impl Block for Ram {
+    fn content(&mut self) -> Result<String> {
+        let (used, total, percentage) = self.get_memory_info()?;
+
+        let used_gb = used as f32 / 1024.0 / 1024.0;
+        let total_gb = total as f32 / 1024.0 / 1024.0;
+
+        let result = self
+            .format
+            .replace("{used}", &format!("{:.1}", used_gb))
+            .replace("{total}", &format!("{:.1}", total_gb))
+            .replace("{percent}", &format!("{:.1}", percentage))
+            .replace("{}", &format!("{:.1}", used_gb));
+
+        Ok(result)
+    }
+
+    fn interval(&self) -> Duration {
+        self.interval
+    }
+
+    fn color(&self) -> u32 {
+        self.color
+    }
+}
diff --git a/src/config.rs b/src/config.rs
index ccb40fd..200e222 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -6,24 +6,24 @@ use x11rb::protocol::xproto::KeyButMask;
 // ========================================
 // APPEARANCE
 // ========================================
-pub const BORDER_WIDTH: u32 = 0;
+pub const BORDER_WIDTH: u32 = 2;
 pub const BORDER_FOCUSED: u32 = 0x6dade3;
 pub const BORDER_UNFOCUSED: u32 = 0xbbbbbb;
-pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=14";
+pub const FONT: &str = "JetBrainsMono Nerd Font:style=Bold:size=10";
 
 // ========================================
 // GAPS (Vanity Gaps)
 // ========================================
 pub const GAPS_ENABLED: bool = true;
-pub const GAP_INNER_HORIZONTAL: u32 = 3;
-pub const GAP_INNER_VERTICAL: u32 = 3;
-pub const GAP_OUTER_HORIZONTAL: u32 = 3;
-pub const GAP_OUTER_VERTICAL: u32 = 3;
-//
+pub const GAP_INNER_HORIZONTAL: u32 = 6;
+pub const GAP_INNER_VERTICAL: u32 = 6;
+pub const GAP_OUTER_HORIZONTAL: u32 = 6;
+pub const GAP_OUTER_VERTICAL: u32 = 6;
+
 // ========================================
 // DEFAULTS
 // ========================================
-pub const TERMINAL: &str = "alacritty";
+pub const TERMINAL: &str = "st";
 pub const XCLOCK: &str = "xclock";
 pub const MODKEY: KeyButMask = KeyButMask::MOD4;
 
@@ -129,14 +129,28 @@ pub const KEYBINDINGS: &[Key] = &[
 // STATUS BAR BLOCKS
 // ========================================
 pub const STATUS_BLOCKS: &[BlockConfig] = &[
+    // BlockConfig {
+    //     format: "",
+    //     command: BlockCommand::Battery {
+    //         format_charging: "󰂄 Bat: {}%",
+    //         format_discharging: "󰁹 Bat:{}%",
+    //         format_full: "󰁹 Bat: {}%",
+    //     },
+    //     interval_secs: 30,
+    //     color: BLUE,
+    //     underline: true,
+    // },
+    // BlockConfig {
+    //     format: " |  ",
+    //     command: BlockCommand::Static(""),
+    //     interval_secs: u64::MAX,
+    //     color: GRAY_LIGHT,
+    //     underline: false,
+    // },
     BlockConfig {
-        format: "",
-        command: BlockCommand::Battery {
-            format_charging: "󰂄 Bat: {}%",
-            format_discharging: "󰁹 Bat:{}%",
-            format_full: "󰁹 Bat: {}%",
-        },
-        interval_secs: 30,
+        format: "󰍛 {used}/{total} GB",
+        command: BlockCommand::Ram,
+        interval_secs: 5,
         color: BLUE,
         underline: true,
     },