hm, not a problem with the program but a problem with the sketchybar impl.

This commit is contained in:
gallant 2023-10-25 08:04:26 -05:00
parent 41463d0b73
commit 9fa44b96c9
4 changed files with 28 additions and 23 deletions

BIN
helper2

Binary file not shown.

View file

@ -21,7 +21,6 @@ use mach::message::mach_msg_type_number_t;
static TOPPROC: &str = "/bin/ps -Aceo pid,pcpu,comm -r";
static FILTER_PATTERN: &str = "com.apple.";
#[derive()]
pub struct CPU {
pub host: mach::mach_types::host_t,
pub count: mach_msg_type_number_t,
@ -34,7 +33,7 @@ pub struct CPU {
impl CPU {
pub fn new() -> Self {
CPU {
host: unsafe{ mach_host_self()},
host: unsafe{ mach_host_self() },
count: HOST_CPU_LOAD_INFO_COUNT,
load: host_cpu_load_info_data_t {
cpu_ticks: [0; CPU_STATE_MAX as usize],
@ -63,17 +62,17 @@ impl CPU {
}
if self.has_prev_load {
let delta_user = self.load.cpu_ticks[CPU_STATE_USER as usize]
- self.prev_load.cpu_ticks[CPU_STATE_USER as usize];
let delta_user = self.load.cpu_ticks[CPU_STATE_USER as usize] as f32
- self.prev_load.cpu_ticks[CPU_STATE_USER as usize] as f32;
let delta_system = self.load.cpu_ticks[CPU_STATE_SYSTEM as usize]
- self.prev_load.cpu_ticks[CPU_STATE_SYSTEM as usize];
let delta_system = self.load.cpu_ticks[CPU_STATE_SYSTEM as usize] as f32
- self.prev_load.cpu_ticks[CPU_STATE_SYSTEM as usize] as f32;
let delta_idle = self.load.cpu_ticks[CPU_STATE_IDLE as usize]
- self.prev_load.cpu_ticks[CPU_STATE_IDLE as usize];
let delta_idle = self.load.cpu_ticks[CPU_STATE_IDLE as usize] as f32
- self.prev_load.cpu_ticks[CPU_STATE_IDLE as usize] as f32;
let user_perc = delta_user as f64 / (delta_system as f64 + delta_user as f64 + delta_idle as f64);
let sys_perc = delta_system as f64 / (delta_system as f64 + delta_user as f64 + delta_idle as f64);
let user_perc = delta_user / (delta_system + delta_user + delta_idle);
let sys_perc = delta_system / (delta_system + delta_user + delta_idle);
let total_perc = user_perc + sys_perc;
let top_proc = match get_top_process(TOPPROC, FILTER_PATTERN) {

View file

@ -5,16 +5,15 @@ mod clock;
use std::env;
use std::process;
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<Option<CPU>>> = Arc::new(Mutex::new(Some(CPU::new())));
static ref G_CLOCK: Arc<Mutex<Option<Clock>>> = Arc::new(Mutex::new(Some(Clock::new())));
static ref G_CPU: Mutex<CPU> = Mutex::new(CPU::new());
//static ref COUNT : Mutex<u32> = Mutex::new(0);
}
@ -24,6 +23,7 @@ pub extern "C" fn handler(env: sketchybar_rs::Env) {
let sender = env.get_v_for_c("SENDER");
let info = env.get_v_for_c("INFO");
let selected = env.get_v_for_c("SELECTED");
//let mut cpu = CPU::new();
if selected.len() > 0 {
// Space items
@ -47,10 +47,14 @@ 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 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();
let mut cpu = G_CPU.lock().unwrap();
// if *COUNT.lock().unwrap() == 5 {
// *G_CPU.lock().unwrap() = CPU::new();
// }
// else {
// *COUNT.lock().unwrap() += 1;
// }
let mut clock = Clock::new();
cpu.update();
clock.update();
@ -58,8 +62,13 @@ pub extern "C" fn handler(env: sketchybar_rs::Env) {
let command = format!("{} {}", cpu.command, clock.command);
let _ = sketchybar_rs::message(&command);
}
drop(temp_clock);
drop(temp_cpu);
else if !clock.command.is_empty() {
let _ = sketchybar_rs::message(&clock.command);
println!("Clock is working");
}
else {
println!("Nothing is working lol");
}
}
}
@ -74,7 +83,5 @@ 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();
}

View file

@ -16,6 +16,5 @@ fn get_top_process(command: &str, filter_pattern: &str) -> Result<String, io::Er
.splitn(2, filter_pattern)
.last()
.unwrap_or(top_process);
Ok(top_process.to_string())
}