diff --git a/docs/cli/disable-autostart.md b/docs/cli/disable-autostart.md new file mode 100644 index 00000000..32d0bf88 --- /dev/null +++ b/docs/cli/disable-autostart.md @@ -0,0 +1,12 @@ +# disable-autostart + +``` +Unloads and deletes the com.lgug2z.komorebi.plist file in $HOME/Library/LaunchAgents + +Usage: komorebic disable-autostart + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/docs/cli/enable-autostart.md b/docs/cli/enable-autostart.md new file mode 100644 index 00000000..355a62dd --- /dev/null +++ b/docs/cli/enable-autostart.md @@ -0,0 +1,15 @@ +# enable-autostart + +``` +Generates and loads the com.lgug2z.komorebi.plist file in $HOME/Library/LaunchAgents + +Usage: komorebic enable-autostart [OPTIONS] + +Options: + -c, --config + Path to a static configuration JSON file + + -h, --help + Print help + +``` \ No newline at end of file diff --git a/docs/cli/quickstart.md b/docs/cli/quickstart.md new file mode 100644 index 00000000..b296dfea --- /dev/null +++ b/docs/cli/quickstart.md @@ -0,0 +1,12 @@ +# quickstart + +``` +Gather example configurations for a new-user quickstart + +Usage: komorebic quickstart + +Options: + -h, --help + Print help + +``` \ No newline at end of file diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 63dd7e75..57bd0e70 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -590,6 +590,14 @@ struct ReplaceConfiguration { path: PathBuf, } +#[derive(Parser)] +struct EnableAutostart { + /// Path to a static configuration JSON file + #[clap(action, short, long)] + #[clap(value_parser = replace_env_in_path)] + config: Option, +} + #[derive(Parser)] struct EagerFocus { /// Case-sensitive exe identifier @@ -1128,10 +1136,10 @@ enum SubCommand { StaticConfigSchema, /// Generates a static configuration JSON file based on the current window manager state GenerateStaticConfig, - // /// Generates the komorebi.lnk shortcut in shell:startup to autostart komorebi - // EnableAutostart(EnableAutostart), - // /// Deletes the komorebi.lnk shortcut in shell:startup to disable autostart - // DisableAutostart, + /// Generates and loads the com.lgug2z.komorebi.plist file in $HOME/Library/LaunchAgents + EnableAutostart(EnableAutostart), + /// Unloads and deletes the com.lgug2z.komorebi.plist file in $HOME/Library/LaunchAgents + DisableAutostart, } fn print_query(message: &SocketMessage) { @@ -1242,7 +1250,99 @@ fn main() -> eyre::Result<()> { skhdrc_path.display() ); } + SubCommand::EnableAutostart(args) => { + let mut current_exe = std::env::current_exe().expect("unable to get exec path"); + current_exe.pop(); + let komorebic_exe = current_exe.join("komorebi"); + let plist = match args.config { + None => { + format!( + r#" + + + + + EnvironmentVariables + + PATH + $HOME/.nix-profile/bin:/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + + KeepAlive + + Label + com.lgug2z.komorebi + ProcessType + Interactive + ProgramArguments + + {} + + +"#, + komorebic_exe.to_string_lossy() + ) + } + Some(config) => { + format!( + r#" + + + + + EnvironmentVariables + + PATH + $HOME/.nix-profile/bin:/etc/profiles/per-user/$USER/bin:/run/current-system/sw/bin:/nix/var/nix/profiles/default/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin + + KeepAlive + + Label + com.lgug2z.komorebi + ProcessType + Interactive + ProgramArguments + + {} + --config + {} + + +"#, + komorebic_exe.to_string_lossy(), + config.to_string_lossy() + ) + } + }; + + let target = dirs::home_dir() + .expect("no $HOME dir") + .join("Library") + .join("LaunchAgents") + .join("com.lgug2z.komorebi.plist"); + + std::fs::write(&target, &plist)?; + + println!("Created com.lgug2z.komorebi.plist at {}", target.display()); + Command::new("launchctl") + .args(["load", &*target.to_string_lossy()]) + .output()?; + println!("Ran 'launchctl load {}'", target.display()); + } + SubCommand::DisableAutostart => { + let target = dirs::home_dir() + .expect("no $HOME dir") + .join("Library") + .join("LaunchAgents") + .join("com.lgug2z.komorebi.plist"); + + Command::new("launchctl") + .args(["unload", &*target.to_string_lossy()]) + .output()?; + println!("Ran 'launchctl unload {}'", target.display()); + std::fs::remove_file(&target)?; + println!("Deleted {}", target.display()); + } SubCommand::Start(arg) => { let mut command = &mut Command::new("komorebi");