~humaid/hstatus

ff50926242ca09afffd8e4f712f9367b67a4faaa — Humaid AlQassimi 2 years ago 09915ad
Add system load.
2 files changed, 56 insertions(+), 16 deletions(-)

M Cargo.toml
M src/main.rs
M Cargo.toml => Cargo.toml +1 -0
@@ 9,3 9,4 @@ edition = "2018"
[dependencies]
x11 = { version = "2.18.2", features = ["xlib"] }
chrono = "0.4"
libc = "0.2.68"

M src/main.rs => src/main.rs +55 -16
@@ 1,9 1,10 @@
extern crate x11;
use std::ffi::CString;
use chrono::prelude::*;
use std::{thread, time, string};
use std::ffi::CString;
use std::fs::File;
use std::io::prelude::*;
use std::{string, thread, time};
use libc::{getloadavg, c_int};

#[derive(Debug, Clone, Copy)]
pub struct DesktopStatus {


@@ 12,14 13,18 @@ pub struct DesktopStatus {

impl DesktopStatus {
    pub fn new() -> Self {
        DesktopStatus {disp: unsafe {
            x11::xlib::XOpenDisplay(std::ptr::null())
        }}
        DesktopStatus {
            disp: unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) },
        }
    }
    pub fn set_status(self, stat: &str) {
        unsafe {
            let s = CString::new(stat).expect("CString::new failed at set_status");
            x11::xlib::XStoreName(self.disp, x11::xlib::XDefaultRootWindow(self.disp), s.as_ptr());
            x11::xlib::XStoreName(
                self.disp,
                x11::xlib::XDefaultRootWindow(self.disp),
                s.as_ptr(),
            );
            x11::xlib::XSync(self.disp, 0);
        }
    }


@@ 28,35 33,49 @@ impl DesktopStatus {
            x11::xlib::XCloseDisplay(self.disp);
        }
    }

}


fn main() {
    let status: DesktopStatus = DesktopStatus::new();
    loop {
        println!("Update");
        let mut stat = String::new();
        // load
        let load_res = get_load();
        match load_res {
            Ok(load) => {
                stat.push_str("L:");
                stat.push_str(load.as_str());
                stat.push('|');
            }
            Err(why) => println!("Cannot get load: {}", why),
        }

        // bat
        let bat = get_battery();
        match bat {
            Ok(perc) => {
                stat.push_str("B:");
                stat.push_str(perc.as_str());
                stat.push('|');
            }
            Err(why) => println!("Cannot get battery percentage: {}", why)
            Err(why) => println!("Cannot get battery percentage: {}", why),
        }
        stat.push('|');
        // time
        let local: DateTime<Local> = Local::now();
        stat.push_str("UK:");
        stat.push_str(local.with_timezone(&chrono::FixedOffset::east(3600)).format("%I:%M").to_string().as_str());
        stat.push_str(
            local
                .with_timezone(&chrono::FixedOffset::east(3600))
                .format("%I:%M")
                .to_string()
                .as_str(),
        );
        stat.push(' ');
        stat.push_str("AE:");
        stat.push_str(local.format("%I:%M %p %d-%m-%Y").to_string().as_str());
        stat.push('|');


        stat.push_str("humaid's system");
        status.set_status(stat.as_str());
        thread::sleep(time::Duration::from_secs(1));


@@ 71,6 90,22 @@ fn read_file(file: &str) -> std::io::Result<string::String> {
    Ok(contents.replace("\n", ""))
}

fn get_load() -> Result<string::String, &'static str> {
    let mut avgs = Vec::with_capacity(3_usize);
    match unsafe {
        getloadavg(avgs.as_mut_ptr(), 3 as c_int)
    } {
        -1 => Err("returned -1"),
        3 => {
            unsafe {
                avgs.set_len(3_usize);
            }
            Ok(format!("{} {} {}", avgs[0], avgs[1], avgs[2]))
        },
        _ => Err("unknown value"),
    }
}

fn get_battery() -> std::io::Result<string::String> {
    let present = read_file("/sys/class/power_supply/BAT0/present")?;
    assert_eq!(present, "1", "Battery not present");


@@ 83,10 118,14 @@ fn get_battery() -> std::io::Result<string::String> {

    let status = read_file("/sys/class/power_supply/BAT0/status")?;
    let stat = match status.as_ref() {
        "Discharging" =>  "-",
        "Charging" =>"+",
        _ =>"/",
        "Discharging" => "-",
        "Charging" => "+",
        _ => "/",
    };

    Ok(format!("{}%{}",((now_cap as f64/full_design as f64)*100_f64) as i32, stat))
    Ok(format!(
        "{}%{}",
        ((now_cap as f64 / full_design as f64) * 100_f64) as i32,
        stat
    ))
}