summon-switcher 0.1.0

A tiny macOS command-line tool for opening, focusing, and cycling applications from declarative keybindings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Command-line interface for Summon.

use std::process::ExitCode;

use clap::{CommandFactory, Parser};
use summon::app;
use summon::config;
use summon::controller;
use summon::daemon;
use summon::diagnostics;
use summon::runner;

/// Summon — open, focus, and cycle macOS apps from your keyboard.
#[derive(Debug, Parser)]
#[command(name = "summon", version, about)]
pub struct Cli {
    /// Increase diagnostic output (-v for decision, -vv for backend context).
    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
    pub verbose: u8,

    #[command(subcommand)]
    pub command: Option<Command>,

    /// A binding name to summon directly (shorthand for `summon binding <name>`).
    #[arg(value_name = "BINDING")]
    pub binding: Option<String>,
}

/// Summon subcommands.
#[derive(Debug, clap::Subcommand)]
pub enum Command {
    /// Summon an app directly by name or bundle identifier.
    App {
        /// Application name, bundle identifier, or path.
        app: String,
    },

    /// List all configured bindings.
    List,

    /// Show or validate the configuration file.
    Config {
        #[command(subcommand)]
        subcommand: ConfigCommand,
    },

    /// Check whether Summon has the macOS permissions it needs.
    Doctor {
        /// Request the macOS Accessibility permission prompt if not trusted.
        #[arg(long)]
        request_accessibility: bool,

        /// Optional app target to inspect.
        app: Option<String>,
    },

    /// Inspect live app state that Summon uses internally.
    Inspect {
        #[command(subcommand)]
        subcommand: InspectCommand,
    },

    /// Manage the optional summon daemon.
    Daemon {
        #[command(subcommand)]
        subcommand: DaemonCommand,
    },
}

/// Configuration subcommands.
#[derive(Debug, Clone, Copy, clap::Subcommand)]
pub enum ConfigCommand {
    /// Print the active configuration file path.
    Path,

    /// Validate the configuration file and print any errors.
    Check,
}

/// Inspection subcommands.
#[derive(Debug, Clone, clap::Subcommand)]
pub enum InspectCommand {
    /// Print the live AX window state used for cycling an app.
    Windows {
        /// Application name, bundle identifier, or path.
        app: String,

        /// Pretty-print the JSON output.
        #[arg(long)]
        pretty: bool,
    },
}

/// Daemon management subcommands.
#[derive(Debug, Clone, Copy, clap::Subcommand)]
pub enum DaemonCommand {
    /// Start the summon daemon in the background.
    Start,

    /// Run the summon daemon in the foreground.
    Run,

    /// Show daemon status.
    Status,

    /// Stop the running daemon.
    Stop,
}

// ---------------------------------------------------------------------------
// Dispatch
// ---------------------------------------------------------------------------

/// Runs the parsed CLI command.
///
/// Returns [`ExitCode::SUCCESS`] on success, [`ExitCode::FAILURE`] on error.
/// Errors are printed to stderr.
pub fn run(cli: Cli) -> ExitCode {
    let verbose = cli.verbose;
    match cli.command {
        Some(Command::Config { subcommand }) => run_config(subcommand),
        Some(Command::App { ref app }) => run_app(app, verbose),
        Some(Command::List) => run_list(),
        Some(Command::Doctor {
            request_accessibility,
            ref app,
        }) => run_doctor(request_accessibility, app.as_deref()),
        Some(Command::Inspect { subcommand }) => run_inspect(subcommand),
        Some(Command::Daemon { subcommand }) => run_daemon(subcommand),
        None => {
            if let Some(binding) = cli.binding {
                run_binding(&binding, verbose)
            } else {
                // No args — print help so new users see usage information.
                let _ = Cli::command().print_help();
                ExitCode::SUCCESS
            }
        }
    }
}

/// Dispatches `summon config` subcommands.
fn run_config(subcommand: ConfigCommand) -> ExitCode {
    match subcommand {
        ConfigCommand::Path => run_config_path(),
        ConfigCommand::Check => run_config_check(),
    }
}

/// `summon list` — prints all configured bindings.
fn run_list() -> ExitCode {
    let path = match config::config_path() {
        Ok(p) => p,
        Err(err) => {
            eprintln!("{err}");
            return ExitCode::FAILURE;
        }
    };

    let config = match config::load_from(&path) {
        Ok(c) => c,
        Err(err) => {
            eprintln!("Config error in {}:", path.display());
            eprintln!("  {err}");
            return ExitCode::FAILURE;
        }
    };

    if config.bindings.is_empty() {
        println!("No bindings configured.");
        return ExitCode::SUCCESS;
    }

    let max_name_len = config.bindings.keys().map(String::len).max().unwrap_or(0);

    for (name, binding) in &config.bindings {
        println!("{name:max_name_len$} -> {}", binding.app);
    }

    ExitCode::SUCCESS
}

/// `summon config path` — prints the active config file path.
fn run_config_path() -> ExitCode {
    match config::config_path() {
        Ok(path) => {
            println!("{}", path.display());
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("{err}");
            ExitCode::FAILURE
        }
    }
}

/// `summon config check` — validates the config file.
fn run_config_check() -> ExitCode {
    let path = match config::config_path() {
        Ok(p) => p,
        Err(err) => {
            eprintln!("{err}");
            return ExitCode::FAILURE;
        }
    };

    match config::load_from(&path) {
        Ok(config) => {
            let count = config.bindings.len();
            println!("Config is valid: {}", path.display());
            println!("  {count} binding(s) configured");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("Config error in {}:", path.display());
            eprintln!("  {err}");
            ExitCode::FAILURE
        }
    }
}

/// `summon app <app>` — summon an app directly by name, bundle ID, or path.
///
/// Classifies the app string, uses sensible defaults (launch if not running,
/// no cycling), and runs the decide/execute cycle.
fn run_app(app: &str, verbose: u8) -> ExitCode {
    emit_run_output(daemon::run_app_or_direct(app, verbose))
}

/// `summon <binding>` — the core command path.
///
/// Loads config, resolves the binding, decides the action, and executes it.
fn run_binding(name: &str, verbose: u8) -> ExitCode {
    let path = match config::config_path() {
        Ok(p) => p,
        Err(err) => {
            eprintln!("{err}");
            return ExitCode::FAILURE;
        }
    };
    emit_run_output(daemon::run_binding_or_direct(name, &path, verbose))
}

/// `summon doctor` — runs diagnostic checks.
fn run_doctor(request_accessibility: bool, app: Option<&str>) -> ExitCode {
    println!("Summon doctor");
    println!();
    let target = match app {
        Some(app) => match app::classify_app_target(app) {
            Ok(target) => Some(target),
            Err(err) => {
                eprintln!("Invalid app target: {err}");
                return ExitCode::FAILURE;
            }
        },
        None => None,
    };
    let result = diagnostics::run_doctor(diagnostics::DoctorOptions {
        request_accessibility,
        target: target.as_ref(),
    });
    println!();
    println!(
        "{} check(s): {} passed, {} warning(s), {} failed",
        result.checks, result.passed, result.warnings, result.failures
    );

    if result.is_ok() {
        println!("Summon looks healthy.");
        ExitCode::SUCCESS
    } else {
        eprintln!("Some checks failed. See above for details.");
        ExitCode::FAILURE
    }
}

fn run_daemon(subcommand: DaemonCommand) -> ExitCode {
    match subcommand {
        DaemonCommand::Start => match daemon::start() {
            Ok(status) => {
                println!(
                    "Summon daemon running (pid {}, socket {})",
                    status.pid,
                    status.socket_path.display()
                );
                if let Ok(path) = daemon::log_path() {
                    println!("  log: {}", path.display());
                }
                ExitCode::SUCCESS
            }
            Err(err) => {
                eprintln!("{err}");
                ExitCode::FAILURE
            }
        },
        DaemonCommand::Run => match daemon::run_server() {
            Ok(()) => ExitCode::SUCCESS,
            Err(err) => {
                eprintln!("{err}");
                ExitCode::FAILURE
            }
        },
        DaemonCommand::Status => match daemon::status() {
            Ok(status) => {
                println!("Summon daemon: running");
                println!("  pid: {}", status.pid);
                println!("  socket: {}", status.socket_path.display());
                println!("  protocol: v{}", status.protocol_version);
                if let Ok(path) = daemon::log_path() {
                    println!("  log: {}", path.display());
                }
                ExitCode::SUCCESS
            }
            Err(err) => {
                eprintln!("Summon daemon: not running");
                eprintln!("  {err}");
                ExitCode::FAILURE
            }
        },
        DaemonCommand::Stop => match daemon::stop() {
            Ok(()) => {
                println!("Summon daemon stopped.");
                ExitCode::SUCCESS
            }
            Err(err) => {
                eprintln!("{err}");
                ExitCode::FAILURE
            }
        },
    }
}

fn run_inspect(subcommand: InspectCommand) -> ExitCode {
    match subcommand {
        InspectCommand::Windows { app, pretty } => {
            let target = match app::classify_app_target(&app) {
                Ok(target) => target,
                Err(err) => {
                    eprintln!("Invalid app target: {err}");
                    return ExitCode::FAILURE;
                }
            };

            let snapshot = match controller::capture_window_cycle_snapshot(&target) {
                Ok(snapshot) => snapshot,
                Err(err) => {
                    eprintln!("Failed to inspect windows for {app}: {err}");
                    return ExitCode::FAILURE;
                }
            };

            let render = if pretty {
                serde_json::to_string_pretty(&snapshot)
            } else {
                serde_json::to_string(&snapshot)
            };

            match render {
                Ok(json) => {
                    println!("{json}");
                    ExitCode::SUCCESS
                }
                Err(err) => {
                    eprintln!("Failed to serialize window snapshot: {err}");
                    ExitCode::FAILURE
                }
            }
        }
    }
}

fn emit_run_output(output: runner::RunOutput) -> ExitCode {
    output.emit();
    if output.success {
        ExitCode::SUCCESS
    } else {
        ExitCode::FAILURE
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use clap::Parser;

    #[test]
    fn parse_binding_shorthand() {
        let cli = Cli::try_parse_from(["summon", "terminal"]).expect("should parse");
        assert_eq!(cli.binding.as_deref(), Some("terminal"));
        assert!(cli.command.is_none());
    }

    #[test]
    fn parse_app_subcommand() {
        let cli =
            Cli::try_parse_from(["summon", "app", "com.mitchellh.ghostty"]).expect("should parse");
        match cli.command {
            Some(Command::App { app }) => assert_eq!(app, "com.mitchellh.ghostty"),
            other => panic!("expected App command, got {other:?}"),
        }
    }

    #[test]
    fn parse_list() {
        let cli = Cli::try_parse_from(["summon", "list"]).expect("should parse");
        assert!(matches!(cli.command, Some(Command::List)));
    }

    #[test]
    fn parse_config_path() {
        let cli = Cli::try_parse_from(["summon", "config", "path"]).expect("should parse");
        match cli.command {
            Some(Command::Config {
                subcommand: ConfigCommand::Path,
            }) => {}
            other => panic!("expected Config Path, got {other:?}"),
        }
    }

    #[test]
    fn parse_config_check() {
        let cli = Cli::try_parse_from(["summon", "config", "check"]).expect("should parse");
        match cli.command {
            Some(Command::Config {
                subcommand: ConfigCommand::Check,
            }) => {}
            other => panic!("expected Config Check, got {other:?}"),
        }
    }

    #[test]
    fn parse_doctor() {
        let cli = Cli::try_parse_from(["summon", "doctor"]).expect("should parse");
        assert!(matches!(
            cli.command,
            Some(Command::Doctor {
                request_accessibility: false,
                app: None
            })
        ));
    }

    #[test]
    fn parse_doctor_with_target_and_request_accessibility() {
        let cli =
            Cli::try_parse_from(["summon", "doctor", "--request-accessibility", "dev.zed.Zed"])
                .expect("should parse");
        assert!(matches!(
            cli.command,
            Some(Command::Doctor {
                request_accessibility: true,
                app: Some(_)
            })
        ));
    }

    #[test]
    fn parse_inspect_windows() {
        let cli = Cli::try_parse_from(["summon", "inspect", "windows", "dev.zed.Zed", "--pretty"])
            .expect("should parse");
        match cli.command {
            Some(Command::Inspect {
                subcommand: InspectCommand::Windows { app, pretty },
            }) => {
                assert_eq!(app, "dev.zed.Zed");
                assert!(pretty);
            }
            other => panic!("expected Inspect Windows, got {other:?}"),
        }
    }

    #[test]
    fn parse_daemon_start() {
        let cli = Cli::try_parse_from(["summon", "daemon", "start"]).expect("should parse");
        assert!(matches!(
            cli.command,
            Some(Command::Daemon {
                subcommand: DaemonCommand::Start
            })
        ));
    }

    #[test]
    fn parse_daemon_status() {
        let cli = Cli::try_parse_from(["summon", "daemon", "status"]).expect("should parse");
        assert!(matches!(
            cli.command,
            Some(Command::Daemon {
                subcommand: DaemonCommand::Status
            })
        ));
    }

    #[test]
    fn parse_verbose_short() {
        let cli = Cli::try_parse_from(["summon", "-v", "terminal"]).expect("should parse");
        assert_eq!(cli.verbose, 1);
    }

    #[test]
    fn parse_verbose_double() {
        let cli = Cli::try_parse_from(["summon", "-vv", "terminal"]).expect("should parse");
        assert_eq!(cli.verbose, 2);
    }

    #[test]
    fn positional_arg_accepted_as_binding() {
        let cli = Cli::try_parse_from(["summon", "explode"]).expect("should parse as binding");
        assert_eq!(cli.binding.as_deref(), Some("explode"));
        assert!(cli.command.is_none());
    }

    #[test]
    fn no_args_parses_successfully_and_run_prints_help() {
        // Parsing succeeds (no required args), but run() prints help.
        let cli = Cli::try_parse_from(["summon"]).expect("should parse");
        assert!(cli.binding.is_none());
        assert!(cli.command.is_none());
    }

    // -- List formatting tests -----------------------------------------------

    #[test]
    fn format_binding_list_aligns_names() {
        let config = config::parse(
            r#"
            [bindings.browser]
            app = "com.brave.Browser"

            [bindings.terminal]
            app = "com.mitchellh.ghostty"

            [bindings.editor]
            app = "dev.zed.Zed"
            "#,
        )
        .expect("should parse");

        let max_name_len = config.bindings.keys().map(String::len).max().unwrap_or(0);

        // "terminal" is the longest name at 8 chars
        assert_eq!(max_name_len, 8);

        let lines: Vec<String> = config
            .bindings
            .iter()
            .map(|(name, binding)| format!("{name:max_name_len$} -> {}", binding.app))
            .collect();

        // BTreeMap gives sorted order: browser, editor, terminal
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "browser  -> com.brave.Browser");
        assert_eq!(lines[1], "editor   -> dev.zed.Zed");
        assert_eq!(lines[2], "terminal -> com.mitchellh.ghostty");
    }

    #[test]
    fn format_binding_list_single_binding() {
        let config = config::parse(
            r#"
            [bindings.finder]
            app = "com.apple.finder"
            "#,
        )
        .expect("should parse");

        let max_name_len = config.bindings.keys().map(String::len).max().unwrap_or(0);

        let lines: Vec<String> = config
            .bindings
            .iter()
            .map(|(name, binding)| format!("{name:max_name_len$} -> {}", binding.app))
            .collect();

        assert_eq!(lines, ["finder -> com.apple.finder"]);
    }

    #[test]
    fn format_binding_list_empty_config() {
        let config = config::parse("").expect("should parse empty config");
        assert!(config.bindings.is_empty());
    }
}