snipexpand 0.2.1

Fast, config-based text expansion for Linux and Wayland
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
mod app;
mod config;
mod daemon;
mod expander;
mod injector;
mod ipc;
mod keyboard;

use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command(name = "snipexpand", about = "Wayland-native text expander", version)]
struct Cli {
    #[command(subcommand)]
    command: Option<Cmd>,
}

#[derive(Subcommand)]
enum Cmd {
    /// Create starter configuration without overwriting existing files
    Init,
    /// Add or overwrite an expansion (use \\n for newlines)
    Add { trigger: String, expansion: String },
    /// Remove an expansion
    Remove { trigger: String },
    /// List all expansions
    List,
    /// Validate all configuration files without starting the daemon
    Check,
    /// Diagnose configuration, session, permissions, and runtime dependencies
    Doctor,
    /// Show the active application's title, class, and executable
    Detect,
    /// Signal running daemon to reload config
    Reload,
    /// Show daemon status
    Status {
        /// Emit machine-readable JSON
        #[arg(long)]
        json: bool,
    },
    /// Initialize configuration and install the systemd user service
    Install,
}

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();
    ensure_config()?;
    match cli.command {
        None => {
            // Daemon mode: run the tokio event loop
            tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()?
                .block_on(async {
                    let config = config::Config::load_default()?;
                    daemon::run(config).await
                })
        }
        Some(cmd) => handle_cmd(cmd),
    }
}

fn handle_cmd(cmd: Cmd) -> anyhow::Result<()> {
    match cmd {
        Cmd::Init => init_config()?,
        Cmd::List => {
            let cfg = config::Config::load_default()?;
            if cfg.matches.is_empty() {
                println!("No expansions configured.");
                println!("Add one with: snipexpand add /trigger \"expansion text\"");
            } else {
                let mut rows: Vec<_> = cfg
                    .matches
                    .iter()
                    .flat_map(|item| item.triggers.iter().map(move |trigger| (trigger, item)))
                    .collect();
                rows.sort_by_key(|(trigger, _)| trigger.as_str());
                for (trigger, item) in rows {
                    println!(
                        "{:<20} => {:<36} [{}]",
                        trigger,
                        item.replace.replace('\n', "\\n"),
                        item.source.display()
                    );
                }
            }
        }

        Cmd::Check => {
            let cfg = config::Config::load_default()?;
            println!(
                "OK: {} match group(s), {} trigger(s), {} file(s)",
                cfg.matches.len(),
                cfg.matches
                    .iter()
                    .map(|item| item.triggers.len())
                    .sum::<usize>(),
                cfg.loaded_files.len()
            );
            print_config_warnings(&cfg);
        }

        Cmd::Doctor => doctor(),

        Cmd::Detect => {
            let info = app::detect()?;
            println!("title: {}", info.title.as_deref().unwrap_or("<unknown>"));
            println!("class: {}", info.class.as_deref().unwrap_or("<unknown>"));
            println!("exec: {}", info.exec.as_deref().unwrap_or("<unknown>"));
        }

        Cmd::Add { trigger, expansion } => {
            let expansion = expansion.replace("\\n", "\n");
            config::Config::add_generated(&trigger, &expansion)?;
            config::Config::load_default()?;
            println!("Added: {} => {}", trigger, expansion.replace('\n', "\\n"));
            // Signal daemon to reload if running
            let _ = signal_daemon_reload();
        }

        Cmd::Remove { trigger } => {
            if config::Config::remove_generated(&trigger)? {
                config::Config::load_default()?;
                println!("Removed: {}", trigger);
                let _ = signal_daemon_reload();
            } else {
                anyhow::bail!("Trigger '{}' not found", trigger);
            }
        }

        Cmd::Reload => {
            let sock = ipc::socket_path()?;
            if !sock.exists() {
                anyhow::bail!("SnipExpand daemon is not running");
            }
            signal_daemon_reload().map_err(|e| anyhow::anyhow!("Could not reach daemon: {}", e))?;
            println!("Reloaded");
        }

        Cmd::Status { json } => {
            let status = daemon_status()
                .map_err(|error| anyhow::anyhow!("SnipExpand daemon is not reachable: {error}"))?;
            if json {
                println!("{}", serde_json::to_string(&status)?);
            } else {
                println!("SnipExpand daemon is running");
                println!("version: {}", status.version);
                println!("pid: {}", status.pid);
                println!("injection backend: {}", status.injection_backend);
                println!(
                    "matches: {} group(s), {} trigger(s), {} file(s)",
                    status.match_groups, status.triggers, status.files
                );
                println!(
                    "configuration: {}",
                    if status.config_valid {
                        "valid"
                    } else {
                        "INVALID (last valid configuration remains active)"
                    }
                );
            }
        }

        Cmd::Install => {
            install_service()?;
        }
    }
    Ok(())
}

fn print_config_warnings(config: &config::Config) {
    for warning in config.unreachable_triggers() {
        println!(
            "WARNING: trigger '{}' in {} is unreachable in immediate mode because '{}' in {} expands first",
            warning.trigger,
            warning.source.display(),
            warning.blocking_trigger,
            warning.blocking_source.display()
        );
    }
}

fn init_config() -> anyhow::Result<()> {
    let dir = config::Config::dir();
    let match_dir = config::Config::match_dir();
    std::fs::create_dir_all(&match_dir)?;
    let settings = dir.join("config.yml");
    let matches = match_dir.join("base.yml");
    write_new(
        &settings,
        "trigger_mode: space\nterminators: [space]\ninjection_backend: auto\ninjection_delay_ms: 1\nwayland_injection_delay_ms: 0\nuinput_injection_delay_ms: 1\ninjection_settle_ms: 10\n",
    )?;
    write_new(
        &matches,
        "matches:\n  - trigger: \";hello\"\n    replace: \"Hello from SnipExpand!\"\n",
    )?;
    config::Config::load_default()?;
    println!("Configuration ready at {}", dir.display());
    Ok(())
}

fn ensure_config() -> anyhow::Result<()> {
    let dir = config::Config::dir();
    let fresh = !dir.exists();
    let match_dir = config::Config::match_dir();
    std::fs::create_dir_all(&match_dir)?;

    let settings = dir.join("config.yml");
    if !settings.exists() {
        write_new(
            &settings,
            "trigger_mode: space\nterminators: [space]\ninjection_backend: auto\ninjection_delay_ms: 1\nwayland_injection_delay_ms: 0\nuinput_injection_delay_ms: 1\ninjection_settle_ms: 10\n",
        )?;
    }

    if fresh {
        let matches = match_dir.join("base.yml");
        write_new(
            &matches,
            "matches:\n  - trigger: \";hello\"\n    replace: \"Hello from SnipExpand!\"\n",
        )?;
    }

    std::fs::write(
        dir.join("SKILL.md"),
        include_str!("../skills/snipexpand-shortcuts.md"),
    )?;

    Ok(())
}

fn write_new(path: &std::path::Path, contents: &str) -> anyhow::Result<()> {
    use std::io::Write;
    match std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
    {
        Ok(mut file) => {
            file.write_all(contents.as_bytes())?;
            println!("Created: {}", path.display());
        }
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            println!("Kept existing: {}", path.display());
        }
        Err(error) => return Err(error.into()),
    }
    Ok(())
}

fn doctor() {
    let mut failed = false;
    check_doctor(
        "Wayland session",
        std::env::var_os("WAYLAND_DISPLAY").is_some(),
        &mut failed,
    );
    check_doctor("input group", in_group("input"), &mut failed);
    check_doctor("/dev/uinput writable", can_open_uinput(), &mut failed);
    check_doctor(
        "wtype Unicode fallback",
        command_exists("wtype"),
        &mut failed,
    );
    check_doctor(
        "configuration",
        config::Config::load_default().is_ok(),
        &mut failed,
    );
    let running = daemon_status().is_ok();
    check_doctor("daemon", running, &mut failed);
    if failed {
        std::process::exit(1);
    }
}

fn check_doctor(label: &str, ok: bool, failed: &mut bool) {
    println!("{} {label}", if ok { "✓" } else { "✗" });
    *failed |= !ok;
}

fn command_exists(name: &str) -> bool {
    std::env::var_os("PATH")
        .is_some_and(|paths| std::env::split_paths(&paths).any(|dir| dir.join(name).is_file()))
}

fn in_group(name: &str) -> bool {
    std::process::Command::new("id")
        .arg("-nG")
        .output()
        .is_ok_and(|output| {
            output.status.success()
                && String::from_utf8_lossy(&output.stdout)
                    .split_whitespace()
                    .any(|group| group == name)
        })
}

fn can_open_uinput() -> bool {
    std::fs::OpenOptions::new()
        .write(true)
        .open("/dev/uinput")
        .is_ok()
}

fn signal_daemon_reload() -> anyhow::Result<()> {
    let sock = ipc::socket_path()?;
    if !sock.exists() {
        return Ok(()); // Daemon not running, config will be read on next start
    }
    use std::io::Write;
    let mut stream = std::os::unix::net::UnixStream::connect(&sock)?;
    stream.write_all(b"reload\n")?;
    let _ = stream.shutdown(std::net::Shutdown::Write);
    Ok(())
}

fn daemon_status() -> anyhow::Result<ipc::DaemonStatus> {
    use std::io::{BufRead, BufReader, Write};
    use std::time::Duration;

    let sock = ipc::socket_path()?;
    let mut stream = std::os::unix::net::UnixStream::connect(&sock)?;
    stream.set_read_timeout(Some(Duration::from_secs(1)))?;
    stream.set_write_timeout(Some(Duration::from_secs(1)))?;
    stream.write_all(b"status\n")?;
    stream.shutdown(std::net::Shutdown::Write)?;

    let mut response = String::new();
    BufReader::new(stream).read_line(&mut response)?;
    if response.is_empty() {
        anyhow::bail!("daemon returned no status response");
    }
    serde_json::from_str(response.trim_end()).map_err(Into::into)
}

fn install_service() -> anyhow::Result<()> {
    // Find the binary path
    let binary = std::env::current_exe()?;

    // Build service file content
    let service = format!(
        r#"[Unit]
Description=SnipExpand Wayland text expander
Documentation=https://github.com/silouanwright/snipexpand
After=graphical-session.target

[Service]
Type=simple
ExecStart={}
Restart=on-failure
RestartSec=2s
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=graphical-session.target
"#,
        binary.display()
    );

    // Write service file
    let service_dir = {
        let config_home = match std::env::var("XDG_CONFIG_HOME") {
            Ok(v) => std::path::PathBuf::from(v),
            Err(_) => {
                let home = std::env::var("HOME")
                    .map_err(|_| anyhow::anyhow!("HOME environment variable is not set"))?;
                std::path::PathBuf::from(home).join(".config")
            }
        };
        config_home.join("systemd").join("user")
    };
    std::fs::create_dir_all(&service_dir)?;
    let service_path = service_dir.join("snipexpand.service");
    std::fs::write(&service_path, &service)?;
    println!("Wrote: {:?}", service_path);

    // Enable and start the service
    let status = std::process::Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .status()?;
    if !status.success() {
        anyhow::bail!("systemctl daemon-reload failed");
    }

    let status = std::process::Command::new("systemctl")
        .args(["--user", "enable", "--now", "snipexpand"])
        .status()?;
    if !status.success() {
        anyhow::bail!("systemctl enable --now snipexpand failed");
    }

    println!("SnipExpand service installed and started.");
    if !in_group("input") {
        println!();
        println!("Input-device access still needs permission:");
        println!("  sudo usermod -a -G input $USER");
        println!("  # Then log out and back in");
    }
    Ok(())
}