Skip to content

Commit

Permalink
add process list mode skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
ibra committed Aug 24, 2024
1 parent 370f5f1 commit 71f1113
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions laches/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Commands {
Autostart { toggle: String },
Start,
Stop,
Mode { mode: String },
List,
Reset,
}
38 changes: 38 additions & 0 deletions laches/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
use laches::{
cli::{Cli, Commands},
process::{start_monitoring, stop_monitoring},
process_list::ListMode,
store::{get_stored_processes, load_or_create_store, reset_store, LachesStore, STORE_NAME},
utils::{confirm, format_uptime},
};
Expand All @@ -23,13 +24,18 @@ fn main() -> Result<(), Box<dyn Error>> {
Commands::Autostart { toggle } => handle_autostart(toggle),
Commands::Start => start_monitoring(&mut laches_store, &store_path),
Commands::Stop => stop_monitoring(&mut laches_store),
Commands::Mode { mode } => set_mode(mode, &mut laches_store),
Commands::List => list_processes(&laches_store),
Commands::Reset => confirm_reset_store(&store_path),
}?;

Ok(())
}

fn set_mode(mode: &str, laches_store: &LachesStore) -> Result<(), Box<dyn Error>> {
Ok(())
}

fn configure_daemon(laches_store: &LachesStore, store_path: &Path) {
let mut monitor = Command::new("laches_mon");
monitor
Expand Down Expand Up @@ -60,12 +66,44 @@ fn list_processes(laches_store: &LachesStore) -> Result<(), Box<dyn Error>> {
let all_windows = get_stored_processes(laches_store);
let mut builder = Builder::default();

println!(
"{}",
&format!(
"Tracked Window Usage ({} Mode)",
match laches_store.process_list_options.mode {
ListMode::Whitelist => "Whitelist",
ListMode::Blacklist => "Blacklist",
ListMode::Default => "Default",
}
)
);

let mut sorted_windows = all_windows.clone();
sorted_windows.sort_by_key(|window| std::cmp::Reverse(window.uptime));

builder.push_record(["Process Name", "Usage Time"]);

for window in &sorted_windows {
match laches_store.process_list_options.mode {
ListMode::Whitelist => {
if let Some(ref whitelist) = laches_store.process_list_options.whitelist {
if !whitelist.contains(&window.title) {
continue;
}
}
}
ListMode::Blacklist => {
if let Some(ref blacklist) = laches_store.process_list_options.blacklist {
if blacklist.contains(&window.title) {
continue;
}
}
}
ListMode::Default => {
// default mode does no filtering, so process all windows
}
}

builder.push_record([&window.title, &format_uptime(window.uptime)]);
}

Expand Down

0 comments on commit 71f1113

Please sign in to comment.