From f7b750580cab658b6f6f725608a15a8e75423a97 Mon Sep 17 00:00:00 2001 From: mustafa salih Date: Mon, 21 Dec 2020 13:29:45 +0300 Subject: [PATCH] init commit --- .gitignore | 1 + Cargo.lock | 5 ++++ Cargo.toml | 9 ++++++++ src/date.rs | 11 +++++++++ src/disk.rs | 19 +++++++++++++++ src/main.rs | 26 +++++++++++++++++++++ src/mem.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/date.rs create mode 100644 src/disk.rs create mode 100644 src/main.rs create mode 100644 src/mem.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..c1c1c87 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rs_blocks" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2e7e252 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rs_blocks" +version = "0.1.0" +authors = ["mustafa salih "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/date.rs b/src/date.rs new file mode 100644 index 0000000..e5d4ebd --- /dev/null +++ b/src/date.rs @@ -0,0 +1,11 @@ +use std::process::Command; + +pub fn fmt_date(ft: &str) -> String { + let cmd = format!("date +\"{}\"", ft); + let cmd = Command::new("sh").arg("-c").args(&[cmd]).output().unwrap(); + let result = String::from_utf8_lossy(&cmd.stdout) + .to_string() + .trim() + .to_string(); + format!("  {} │", result) +} diff --git a/src/disk.rs b/src/disk.rs new file mode 100644 index 0000000..72dd5bf --- /dev/null +++ b/src/disk.rs @@ -0,0 +1,19 @@ +use std::process::Command; + +pub fn disk_free() -> String { + let cmd = Command::new("sh") + .arg("-c") + .args(&["df -h"]) + .output() + .unwrap(); + let output = String::from_utf8_lossy(&cmd.stdout); + let mut disk_used = String::new(); + for line in output.lines() { + if line.ends_with('/') { + let splited = line.split_whitespace().collect::>(); + disk_used = splited[2].to_string(); + break; + } + } + format!("  {} │", disk_used) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e69b5af --- /dev/null +++ b/src/main.rs @@ -0,0 +1,26 @@ +use std::process::Command; +use std::thread; +use std::time::Duration; + +mod date; +mod disk; +mod mem; + +fn main() { + loop { + let args = format!( + "{}{}{}", + disk::disk_free(), + mem::mem().unwrap(), + date::fmt_date("%d %b, %I:%M:%S %p") + ); + + Command::new("xsetroot") + .arg("-name") + .arg(args) + .output() + .unwrap(); + + thread::sleep(Duration::new(1, 0)); + } +} diff --git a/src/mem.rs b/src/mem.rs new file mode 100644 index 0000000..ac0f0ae --- /dev/null +++ b/src/mem.rs @@ -0,0 +1,66 @@ +use std::fs::File; +use std::io::Read; +//MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable +pub fn mem() -> Result { + let mut buf = String::new(); + + File::open("/proc/meminfo")?.read_to_string(&mut buf)?; + + let mut mem_total: u32 = 0; + let mut shmem: u32 = 0; + let mut mem_free: u32 = 0; + let mut mem_buffers: u32 = 0; + let mut mem_cached: u32 = 0; + let mut mem_srecl: u32 = 0; + + for line in buf.lines() { + if mem_total > 0 + && shmem > 0 + && mem_free > 0 + && mem_buffers > 0 + && mem_cached > 0 + && mem_srecl > 0 + { + break; + } + if line.starts_with("MemTotal") { + assign_val(line, &mut mem_total); + } + if line.starts_with("SReclaimable") { + assign_val(line, &mut mem_srecl) + } + if line.starts_with("Cached") { + assign_val(line, &mut mem_cached) + } + + if line.starts_with("Shmem") { + assign_val(line, &mut shmem); + } + + if line.starts_with("MemFree") { + assign_val(line, &mut mem_free); + } + if line.starts_with("Buffers") { + assign_val(line, &mut mem_buffers); + } + } + + let mem_used = (mem_total + shmem - mem_free - mem_buffers - mem_cached - mem_srecl) / 1024; + let result: String; + if mem_used > 1000 { + result = format!(" ▦ {:.1}G │", mem_used as f32 / 1000.0); + } else { + result = format!(" ▦ {}M │", mem_used); + } + Ok(result) +} + +fn assign_val(line: &str, assignable: &mut u32) { + let parsed: u32 = line.split(':').collect::>()[1] + .trim() + .split(' ') + .collect::>()[0] + .parse() + .unwrap(); + *assignable = parsed; +}