mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-28 03:12:46 +01:00
refactor(komorebic): the KDE start and stop use the Launcher skeleton of komorebic-core
start becomes a KdeLauncher (the Command for komorebi, the process lookup by exact name, the run in front of the user) around komorebic_core::launcher::start, which has the attempt loop; stop sends launcher::stop_message. The text printed and the commands run are the same. Verified: KDE cargo test -p komorebic --locked: 10 + 12 + 49 + 9 passed, 0 failed; oracle kde 166 of 166 pages. Not verified: an actual komorebic start against komorebi (no test starts the daemon).
This commit is contained in:
1 parent
be3d1cd6b6
commit
a2fa30acdc
2 files changed
+62
-61
No files matched your search
Generated
+1
@@ -2640,6 +2640,7 @@ version = "0.1.42"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"clap_complete",
|
||||
"color-eyre",
|
||||
"komorebi-layouts",
|
||||
"komorebi-protocol",
|
||||
]
|
||||
|
||||
@@ -141,6 +141,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 lazy_static::lazy_static;
|
||||
use std::fs::File;
|
||||
@@ -152,7 +154,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;
|
||||
|
||||
mod hotkeys;
|
||||
@@ -500,6 +501,60 @@ fn print_query(message: &SocketMessage) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Starting `komorebi` for `komorebic start`: a `Command` for the daemon, found among the
|
||||
/// processes by its exact name.
|
||||
struct KdeLauncher<'a> {
|
||||
args: &'a Start,
|
||||
system: sysinfo::System,
|
||||
}
|
||||
|
||||
impl Launcher for KdeLauncher<'_> {
|
||||
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", &config.display().to_string()]);
|
||||
}
|
||||
|
||||
command
|
||||
.args(&self.args.komorebi_args)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.stdin(Stdio::null())
|
||||
.spawn()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_running(&mut self) -> eyre::Result<bool> {
|
||||
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.args(["--config", &config.display().to_string()]);
|
||||
}
|
||||
|
||||
if let Ok(output) = command.args(&self.args.komorebi_args).output() {
|
||||
println!("{}", String::from_utf8(output.stderr)?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// What the top of the help and `--version` say.
|
||||
const META: Meta = Meta {
|
||||
name: "komorebic",
|
||||
@@ -679,68 +734,13 @@ fn main() -> eyre::Result<()> {
|
||||
}
|
||||
}
|
||||
KdeCommands::Start(args) => {
|
||||
let mut command = &mut Command::new("komorebi");
|
||||
|
||||
if let Some(config) = &args.config {
|
||||
command = command.args(["--config", &config.display().to_string()])
|
||||
};
|
||||
|
||||
command = command
|
||||
.args(&args.komorebi_args)
|
||||
.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::io::stdout().flush()?;
|
||||
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");
|
||||
let mut command = Command::new("komorebi");
|
||||
|
||||
if let Some(config) = &args.config {
|
||||
command.args(["--config", &config.display().to_string()]);
|
||||
}
|
||||
|
||||
if let Ok(output) = command.args(&args.komorebi_args).output() {
|
||||
println!("{}", String::from_utf8(output.stderr)?);
|
||||
}
|
||||
}
|
||||
launcher::start(&mut KdeLauncher {
|
||||
args: &args,
|
||||
system: sysinfo::System::new_all()?,
|
||||
})?;
|
||||
}
|
||||
KdeCommands::Stop(args) => {
|
||||
if args.ignore_restore {
|
||||
send_message(&SocketMessage::StopIgnoreRestore)?;
|
||||
} else {
|
||||
send_message(&SocketMessage::Stop)?;
|
||||
}
|
||||
send_message(&launcher::stop_message(args.ignore_restore))?;
|
||||
}
|
||||
KdeCommands::Configuration => {
|
||||
let static_config = HOME_DIR.join("komorebi.json");
|
||||
|
||||
Reference in new issue
Block a user