diff --git a/komorebi-for-mac/Cargo.lock b/komorebi-for-mac/Cargo.lock index 0bd36ecd..c4854fe5 100644 --- a/komorebi-for-mac/Cargo.lock +++ b/komorebi-for-mac/Cargo.lock @@ -3344,6 +3344,7 @@ version = "0.1.42" dependencies = [ "clap", "clap_complete", + "color-eyre", "komorebi-layouts", "komorebi-protocol", ] diff --git a/komorebi-for-mac/komorebic/src/main.rs b/komorebi-for-mac/komorebic/src/main.rs index 0e3b7b09..41b00436 100644 --- a/komorebi-for-mac/komorebic/src/main.rs +++ b/komorebi-for-mac/komorebic/src/main.rs @@ -13,6 +13,8 @@ use komorebic_core::cli::Cmd; use komorebic_core::cli::Meta; use komorebic_core::cli::command; use komorebic_core::docgen::DocFormat; +use komorebic_core::launcher; +use komorebic_core::launcher::Launcher; use komorebic_core::messages::Messages; use komorebic_core::pan; use komorebic_core::platform::macos::*; @@ -28,7 +30,6 @@ use std::process::Command; use std::process::Stdio; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; -use std::time::Duration; use sysinfo::ProcessesToUpdate; lazy_static! { @@ -61,6 +62,59 @@ fn print_query(message: &SocketMessage) { } } +/// Starting `komorebi` for `komorebic start`: a `Command` for the daemon, found among the +/// processes by its exact name. +struct MacosLauncher<'a> { + args: &'a Start, + system: sysinfo::System, +} + +impl Launcher for MacosLauncher<'_> { + fn daemon_name(&self) -> &str { + "komorebi" + } + + fn spawn(&mut self) -> eyre::Result<()> { + let mut command = Command::new("komorebi"); + + if let Some(config) = &self.args.config { + command.args(["--config", &format!("'--config=\"{}\"'", config.display())]); + } + + command + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .stdin(Stdio::null()) + .spawn()?; + + Ok(()) + } + + fn is_running(&mut self) -> eyre::Result { + self.system.refresh_processes(ProcessesToUpdate::All, true); + + Ok(self + .system + .processes_by_exact_name("komorebi".as_ref()) + .next() + .is_some()) + } + + fn diagnose(&mut self) -> eyre::Result<()> { + let mut command = Command::new("komorebi"); + + if let Some(config) = &self.args.config { + command.arg(format!("'--config=\"{}\"'", config.display())); + } + + if let Ok(output) = command.output() { + println!("{}", String::from_utf8(output.stderr)?); + } + + Ok(()) + } +} + /// What the top of the help and `--version` say. const META: Meta = Meta { name: "komorebic", @@ -552,61 +606,12 @@ exit 1 } } MacosCommands::Start(args) => { - let mut command = &mut Command::new("komorebi"); - - if let Some(config) = &args.config { - command = - command.args(["--config", &format!("'--config=\"{}\"'", config.display())]) + let mut launcher = MacosLauncher { + args: &args, + system: sysinfo::System::new_all(), }; - command = command - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .stdin(Stdio::null()); - - let mut system = sysinfo::System::new_all(); - system.refresh_processes(ProcessesToUpdate::All, true); - - let mut attempts = 0; - let mut running = system - .processes_by_exact_name("komorebi".as_ref()) - .next() - .is_some(); - - while !running && attempts <= 2 { - command.spawn()?; - - print!("Waiting for komorebi to start... "); - std::thread::sleep(Duration::from_secs(3)); - - system.refresh_processes(ProcessesToUpdate::All, true); - - if system - .processes_by_exact_name("komorebi".as_ref()) - .next() - .is_some() - { - println!("Started!"); - running = true; - } else { - println!("komorebi did not start... Trying again"); - attempts += 1; - } - } - - if !running { - println!("\nRunning komorebi directly for detailed error output\n"); - if let Some(config) = &args.config { - if let Ok(output) = Command::new("komorebi") - .arg(format!("'--config=\"{}\"'", config.display())) - .output() - { - println!("{}", String::from_utf8(output.stderr)?); - } - } else if let Ok(output) = Command::new("komorebi").output() { - println!("{}", String::from_utf8(output.stderr)?); - } - + if !launcher::start(&mut launcher)? { return Ok(()); } @@ -662,11 +667,7 @@ exit 1 ); } MacosCommands::Stop(args) => { - if args.ignore_restore { - send_message(&SocketMessage::StopIgnoreRestore)?; - } else { - send_message(&SocketMessage::Stop)?; - } + send_message(&launcher::stop_message(args.ignore_restore))?; if args.bar { Command::new("pkill").arg("komorebi-bar").spawn()?;