init commit

This commit is contained in:
mustafa salih 2020-12-21 13:29:45 +03:00
commit f7b750580c
7 changed files with 137 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

5
Cargo.lock generated Normal file
View File

@ -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"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rs_blocks"
version = "0.1.0"
authors = ["mustafa salih <mustafasalih1991@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

11
src/date.rs Normal file
View File

@ -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)
}

19
src/disk.rs Normal file
View File

@ -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::<Vec<&str>>();
disk_used = splited[2].to_string();
break;
}
}
format!("{}", disk_used)
}

26
src/main.rs Normal file
View File

@ -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));
}
}

66
src/mem.rs Normal file
View File

@ -0,0 +1,66 @@
use std::fs::File;
use std::io::Read;
//MemUsed = Memtotal + Shmem - MemFree - Buffers - Cached - SReclaimable
pub fn mem() -> Result<String, std::io::Error> {
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::<Vec<&str>>()[1]
.trim()
.split(' ')
.collect::<Vec<&str>>()[0]
.parse()
.unwrap();
*assignable = parsed;
}