This commit is contained in:
gallant 2023-10-21 18:40:07 -05:00
parent 439b46b0fc
commit 41463d0b73
4 changed files with 55 additions and 18 deletions

View file

@ -1,4 +1,5 @@
use chrono::prelude::*;
pub struct Clock {
pub command: String,
}
@ -11,7 +12,7 @@ impl Clock {
}
pub fn update(&mut self) {
let t: DateTime<Utc> = Utc::now();
let t: DateTime<Local> = Local::now();
let time = t.format("%H:%M");
let date = t.format("%a %d. %b");

View file

@ -1,14 +1,22 @@
use libc::mach_host_self;
use mach::message::mach_msg_type_number_t;
use std::io;
use std::str;
use mach::kern_return::KERN_SUCCESS;
use mach::host_info::*;
use mach::machine::*;
use libc::mach_host_self;
use mach::kern_return::KERN_SUCCESS;
use mach::host_info::{
host_cpu_load_info_data_t,
host_info_t,
HOST_CPU_LOAD_INFO,
HOST_CPU_LOAD_INFO_COUNT
};
use mach::machine::{
CPU_STATE_MAX,
CPU_STATE_IDLE,
CPU_STATE_USER,
CPU_STATE_SYSTEM
};
use mach::message::mach_msg_type_number_t;
static TOPPROC: &str = "/bin/ps -Aceo pid,pcpu,comm -r";
static FILTER_PATTERN: &str = "com.apple.";

View file

@ -1,17 +1,20 @@
mod cpu;
mod clock;
//mod mem; not done yet, would like to find a different implementation of these shenanigans
use std::env;
use std::process;
use cpu::CPU;
use clock::Clock;
use std::sync::Mutex;
use std::sync::Arc;
use cpu::CPU;
use clock::Clock;
use lazy_static::lazy_static;
lazy_static!{
static ref G_CPU: Arc<Mutex<CPU>> = Arc::new(Mutex::new(CPU::new()));
static ref G_CLOCK: Arc<Mutex<Clock>> = Arc::new(Mutex::new(Clock::new()));
static ref G_CPU: Arc<Mutex<Option<CPU>>> = Arc::new(Mutex::new(Some(CPU::new())));
static ref G_CLOCK: Arc<Mutex<Option<Clock>>> = Arc::new(Mutex::new(Some(Clock::new())));
}
@ -44,17 +47,19 @@ pub extern "C" fn handler(env: sketchybar_rs::Env) {
sketchybar_rs::message(&command).unwrap();
} else if sender == "routine" || sender == "forced" {
// CPU and Clock routine updates
let mut cpu = G_CPU.lock().unwrap();
let mut clock = G_CLOCK.lock().unwrap();
let mut temp_cpu = G_CPU.lock().unwrap();
let cpu = temp_cpu.as_mut().unwrap();
let mut temp_clock = G_CLOCK.lock().unwrap();
let clock = temp_clock.as_mut().unwrap();
cpu.update();
clock.update();
if !cpu.command.is_empty() && !clock.command.is_empty() {
let command = format!("{} {}", cpu.command, clock.command);
sketchybar_rs::message(&command);
let _ = sketchybar_rs::message(&command);
}
drop(cpu);
drop(clock);
drop(temp_clock);
drop(temp_cpu);
}
}
@ -69,5 +74,7 @@ fn main() {
let bootstrap_name = args.next().unwrap();
sketchybar_rs::server_begin(handler, &bootstrap_name);
G_CPU.lock().unwrap().take();
G_CLOCK.lock().unwrap().take();
}

21
src/mem.rs Normal file
View file

@ -0,0 +1,21 @@
use std::io;
static TOPPROC: &str = "/bin/ps -Aceo pid,pmem,comm -r";
static FILTER_PATTERN: &str = "com.apple.";
fn get_top_process(command: &str, filter_pattern: &str) -> Result<String, io::Error> {
let output = std::process::Command::new("sh")
.arg("-c")
.arg(command)
.output()?;
let output_str = String::from_utf8_lossy(&output.stdout);
let top_process = output_str.lines().nth(2).unwrap_or("");
let top_process = top_process
.splitn(2, filter_pattern)
.last()
.unwrap_or(top_process);
Ok(top_process.to_string())
}