mnml-rs 0.2.13

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! Mason-style tools registry. A curated list of every external binary
//! mnml looks for (language servers, formatters, linters) along with a
//! suggested install command. The picker (`tools.installer`) shows the
//! list with ✓/✗ "is on PATH" status; accepting a row copies the
//! install command to the clipboard so the user can run it themselves.
//!
//! This is intentionally a *catalog*, not a full package manager.
//! Nvim's Mason maintains ~250 packages with per-platform install
//! recipes; mnml's MVP captures the high-value "what tools do I still
//! need to install?" gesture without the maintenance burden.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolKind {
    Lsp,
    Formatter,
    Linter,
}

impl ToolKind {
    pub fn label(self) -> &'static str {
        match self {
            ToolKind::Lsp => "lsp",
            ToolKind::Formatter => "fmt",
            ToolKind::Linter => "lint",
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct ToolEntry {
    /// Short display name (e.g. "prettier", "rust-analyzer").
    pub name: &'static str,
    /// What category of tool this is.
    pub kind: ToolKind,
    /// The binary the user needs on `$PATH`. Used by the "installed?" check.
    pub bin: &'static str,
    /// One-line description shown as the picker's detail.
    pub description: &'static str,
    /// Suggested install command (the user runs this themselves —
    /// mnml doesn't auto-install). The picker copies this to clipboard
    /// on accept.
    pub install: &'static str,
}

/// Curated list. Add entries here as mnml grows its language coverage.
pub const KNOWN_TOOLS: &[ToolEntry] = &[
    // ── language servers (mirror DEFAULT_LSPS in src/lsp/mod.rs) ──
    ToolEntry {
        name: "rust-analyzer",
        kind: ToolKind::Lsp,
        bin: "rust-analyzer",
        description: "Rust language server",
        install: "rustup component add rust-analyzer",
    },
    ToolEntry {
        name: "typescript-language-server",
        kind: ToolKind::Lsp,
        bin: "typescript-language-server",
        description: "TypeScript / JavaScript language server",
        install: "npm i -g typescript typescript-language-server",
    },
    ToolEntry {
        name: "pyright",
        kind: ToolKind::Lsp,
        bin: "pyright-langserver",
        description: "Python language server (pyright)",
        install: "npm i -g pyright",
    },
    ToolEntry {
        name: "gopls",
        kind: ToolKind::Lsp,
        bin: "gopls",
        description: "Go language server",
        install: "go install golang.org/x/tools/gopls@latest",
    },
    ToolEntry {
        name: "clangd",
        kind: ToolKind::Lsp,
        bin: "clangd",
        description: "C / C++ language server",
        install: "brew install llvm  (or: apt install clangd)",
    },
    ToolEntry {
        name: "lua-language-server",
        kind: ToolKind::Lsp,
        bin: "lua-language-server",
        description: "Lua language server",
        install: "brew install lua-language-server  (or: apt install lua-language-server / cargo install lua-language-server)",
    },
    ToolEntry {
        name: "yaml-language-server",
        kind: ToolKind::Lsp,
        bin: "yaml-language-server",
        description: "YAML language server",
        install: "npm i -g yaml-language-server",
    },
    ToolEntry {
        name: "bash-language-server",
        kind: ToolKind::Lsp,
        bin: "bash-language-server",
        description: "Bash / sh language server",
        install: "npm i -g bash-language-server",
    },
    ToolEntry {
        name: "vscode-css-language-server",
        kind: ToolKind::Lsp,
        bin: "vscode-css-language-server",
        description: "CSS / SCSS language server",
        install: "npm i -g vscode-langservers-extracted",
    },
    ToolEntry {
        name: "vscode-html-language-server",
        kind: ToolKind::Lsp,
        bin: "vscode-html-language-server",
        description: "HTML language server",
        install: "npm i -g vscode-langservers-extracted",
    },
    ToolEntry {
        name: "vscode-json-language-server",
        kind: ToolKind::Lsp,
        bin: "vscode-json-language-server",
        description: "JSON language server",
        install: "npm i -g vscode-langservers-extracted",
    },
    ToolEntry {
        name: "tailwindcss-language-server",
        kind: ToolKind::Lsp,
        bin: "tailwindcss-language-server",
        description: "Tailwind CSS language server",
        install: "npm i -g @tailwindcss/language-server",
    },
    ToolEntry {
        name: "ruby-lsp",
        kind: ToolKind::Lsp,
        bin: "ruby-lsp",
        description: "Ruby language server",
        install: "gem install ruby-lsp",
    },
    // ── formatters (mirror DEFAULT_FORMATTERS in src/formatter.rs) ──
    ToolEntry {
        name: "prettier",
        kind: ToolKind::Formatter,
        bin: "prettier",
        description: "JS / TS / CSS / HTML / MD / JSON / YAML formatter",
        install: "npm i -g prettier",
    },
    ToolEntry {
        name: "rustfmt",
        kind: ToolKind::Formatter,
        bin: "rustfmt",
        description: "Rust formatter",
        install: "rustup component add rustfmt",
    },
    ToolEntry {
        name: "gofmt",
        kind: ToolKind::Formatter,
        bin: "gofmt",
        description: "Go formatter (ships with the Go toolchain)",
        install: "Install Go: https://go.dev/dl/",
    },
    ToolEntry {
        name: "ruff",
        kind: ToolKind::Formatter,
        bin: "ruff",
        description: "Python formatter + linter",
        install: "pip install ruff  (or: brew install ruff)",
    },
    ToolEntry {
        name: "black",
        kind: ToolKind::Formatter,
        bin: "black",
        description: "Python formatter",
        install: "pip install black",
    },
    ToolEntry {
        name: "shfmt",
        kind: ToolKind::Formatter,
        bin: "shfmt",
        description: "Shell script formatter",
        install: "brew install shfmt  (or: go install mvdan.cc/sh/v3/cmd/shfmt@latest)",
    },
    ToolEntry {
        name: "stylua",
        kind: ToolKind::Formatter,
        bin: "stylua",
        description: "Lua formatter",
        install: "brew install stylua  (or: cargo install stylua)",
    },
    ToolEntry {
        name: "nixfmt",
        kind: ToolKind::Formatter,
        bin: "nixfmt",
        description: "Nix formatter",
        install: "nix profile install nixpkgs#nixfmt",
    },
    ToolEntry {
        name: "biome",
        kind: ToolKind::Formatter,
        bin: "biome",
        description: "JS / TS formatter + linter (prettier+eslint alternative)",
        install: "npm i -g @biomejs/biome",
    },
    // ── linters (mirror DEFAULT_LINTERS in src/linter.rs) ──
    ToolEntry {
        name: "eslint",
        kind: ToolKind::Linter,
        bin: "eslint",
        description: "JS / TS linter",
        install: "npm i -g eslint",
    },
    ToolEntry {
        name: "shellcheck",
        kind: ToolKind::Linter,
        bin: "shellcheck",
        description: "Shell script linter",
        install: "brew install shellcheck  (or: apt install shellcheck)",
    },
];

/// External terminal-app catalog — htop, iftop, btop, etc. These
/// are visible-binary tools the user runs interactively; the
/// integration_icon's `:tools.<id>` command fires
/// `App::run_external_tool(id)`, which either opens the binary in
/// a Pty pane or toasts a `brew install` hint.
///
/// Kept separate from `KNOWN_TOOLS` (LSP/fmt/lint installed-state
/// indicators) — same shape, but a different runtime gesture.
pub struct ExternalTool {
    pub id: &'static str,
    pub binary: &'static str,
    /// Homebrew formula name — usually the same as `binary`.
    pub brew_pkg: &'static str,
    /// apt package name on Debian / Ubuntu. Same as `brew_pkg` unless
    /// the package is named differently in the apt repo.
    pub apt_pkg: &'static str,
    pub label: &'static str,
    /// qa-feature 2026-07-01 — some tools require raw packet /
    /// device access and only work under `sudo` (iftop needs
    /// `/dev/bpf*`). When true, mnml wraps the invocation in
    /// `sudo` so the pty prompts for a password rather than
    /// silently failing with a permission-denied dump.
    pub needs_sudo: bool,
}

/// Platform-appropriate install hint for a missing binary. Branches
/// on the host OS so a Linux user doesn't see `brew install` and a
/// Windows user gets a winget / scoop hint. Used by both the
/// external-tool launcher and the LSP missing-binary path.
pub fn install_hint(brew_pkg: &str, apt_pkg: &str) -> String {
    // Returns a clean shell-executable command — no parentheticals
    // or alternatives — so callers can both display AND run it.
    match std::env::consts::OS {
        "macos" => format!("brew install {brew_pkg}"),
        "linux" => format!("sudo apt install -y {apt_pkg}"),
        _ => format!("install {brew_pkg} via your package manager"),
    }
}

/// Whether `install_hint` returns a command that's safe to actually
/// SPAWN (vs just toast as a hint). True on macOS + Linux (brew /
/// apt are reasonable assumptions); false elsewhere where there's
/// no single canonical package manager.
pub fn install_is_spawnable() -> bool {
    matches!(std::env::consts::OS, "macos" | "linux")
}

pub const EXTERNAL_TOOLS: &[ExternalTool] = &[
    ExternalTool {
        id: "htop",
        binary: "htop",
        brew_pkg: "htop",
        apt_pkg: "htop",
        label: "htop — interactive process viewer",
        needs_sudo: false,
    },
    ExternalTool {
        id: "iftop",
        binary: "iftop",
        brew_pkg: "iftop",
        apt_pkg: "iftop",
        label: "iftop — interactive bandwidth monitor",
        // iftop opens /dev/bpf* on macOS + raw sockets on Linux —
        // both root-only. Without sudo it silently prints
        // Permission denied and dies.
        needs_sudo: true,
    },
    ExternalTool {
        id: "btop",
        binary: "btop",
        brew_pkg: "btop",
        apt_pkg: "btop",
        label: "btop — resource monitor (cpu / mem / disk / net)",
        needs_sudo: false,
    },
    // ── 2026-07-08 extension pass (TODO.md external-tools catalog). ──
    // Same shape as htop / btop; interactive CLI tools that make
    // sense to run in a Pty pane. No sudo needed for any of these.
    ExternalTool {
        id: "ncdu",
        binary: "ncdu",
        brew_pkg: "ncdu",
        apt_pkg: "ncdu",
        label: "ncdu — interactive disk usage (drill into what's big)",
        needs_sudo: false,
    },
    ExternalTool {
        id: "lazygit",
        binary: "lazygit",
        brew_pkg: "lazygit",
        apt_pkg: "lazygit",
        label: "lazygit — full-featured git TUI",
        needs_sudo: false,
    },
    ExternalTool {
        id: "gh",
        binary: "gh",
        brew_pkg: "gh",
        apt_pkg: "gh",
        label: "gh — GitHub CLI (issues, PRs, releases)",
        needs_sudo: false,
    },
    ExternalTool {
        id: "dust",
        binary: "dust",
        brew_pkg: "dust",
        apt_pkg: "dust",
        label: "dust — du replacement with a tree view (du -sh . done right)",
        needs_sudo: false,
    },
];

/// Best-effort detection of the primary network interface (the one
/// the default route uses). Runs `route -n get default` on macOS
/// and `ip route show default` on Linux, then parses the interface
/// name from the output. Returns `None` if neither command works or
/// the output can't be parsed.
///
/// Used by the iftop launcher so the tool binds to the interface
/// with actual traffic instead of macOS's `anpi2` (Apple secondary
/// radio) auto-pick.
pub fn default_route_iface() -> Option<String> {
    #[cfg(target_os = "macos")]
    {
        let out = std::process::Command::new("route")
            .args(["-n", "get", "default"])
            .output()
            .ok()?;
        if !out.status.success() {
            return None;
        }
        let stdout = String::from_utf8_lossy(&out.stdout);
        for line in stdout.lines() {
            let line = line.trim();
            if let Some(rest) = line.strip_prefix("interface:") {
                let iface = rest.trim();
                if !iface.is_empty() {
                    return Some(iface.to_string());
                }
            }
        }
        None
    }
    #[cfg(target_os = "linux")]
    {
        let out = std::process::Command::new("ip")
            .args(["route", "show", "default"])
            .output()
            .ok()?;
        if !out.status.success() {
            return None;
        }
        let stdout = String::from_utf8_lossy(&out.stdout);
        // Example: `default via 192.168.1.1 dev en0 proto dhcp`
        let mut parts = stdout.split_whitespace();
        while let Some(tok) = parts.next() {
            if tok == "dev"
                && let Some(iface) = parts.next()
            {
                return Some(iface.to_string());
            }
        }
        None
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        None
    }
}

/// Check whether `bin` is on `$PATH`. Walks PATH directories looking
/// for a file matching `bin` (case-sensitive on Unix; honors `.exe` on
/// Windows). Returns `true` on first hit.
pub fn is_on_path(bin: &str) -> bool {
    let path = match std::env::var_os("PATH") {
        Some(p) => p,
        None => return false,
    };
    for dir in std::env::split_paths(&path) {
        let candidate = dir.join(bin);
        if candidate.is_file() {
            return true;
        }
        #[cfg(windows)]
        {
            for ext in &["exe", "cmd", "bat"] {
                let mut p = candidate.clone();
                p.set_extension(ext);
                if p.is_file() {
                    return true;
                }
            }
        }
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn known_tools_has_no_empty_names() {
        assert!(!KNOWN_TOOLS.is_empty());
        for t in KNOWN_TOOLS {
            assert!(!t.name.is_empty(), "empty name");
            assert!(!t.bin.is_empty(), "empty bin for {}", t.name);
            assert!(!t.install.is_empty(), "empty install for {}", t.name);
        }
    }

    /// Was: `assert!(is_on_path("sh"))`. That relies on the CI
    /// runner having `/bin` or `/usr/bin` on PATH — which the
    /// ubuntu-latest cargo-test job apparently doesn't (SEV: CI red
    /// 2026-08-19 run 32206837207). Replaced with a hermetic
    /// self-check: create a temp dir, drop a fake binary into it,
    /// point PATH at that dir only, verify is_on_path finds it.
    /// No dependency on system state; passes identically on every
    /// runner + on Windows (PATHEXT branch exercised via the .exe
    /// candidate below).
    #[test]
    fn is_on_path_finds_binary_in_synthetic_path() {
        // #993 step 2a follow-up (2026-08-19): grab the shared
        // test_env_lock so this test doesn't race
        // `integration_detect::tests::find_shadowed_binaries_*`,
        // which also mutates PATH without their own guard chain.
        // Without the lock cargo test's parallel scheduler clobbers
        // one's PATH mid-run and both fail. Was CI-red at 32267413016
        // on the first run of the tools test alone (a different
        // failure mode); this guards against re-introduction from
        // parallel test collisions on repeat runs.
        let _lk = crate::test_env_lock()
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().expect("tempdir");
        let bin_name = "mnml_test_binary";
        let bin_path = dir.path().join(bin_name);
        std::fs::write(&bin_path, b"").expect("write fake bin");
        // Existence — not executability — is what `is_on_path`
        // checks (matches how `which(1)` on Linux behaves for
        // scripts).
        let prev = std::env::var_os("PATH");
        // SAFETY: guarded by test_env_lock above; PATH is restored
        // in the same critical section.
        unsafe {
            std::env::set_var("PATH", dir.path());
        }
        let found = is_on_path(bin_name);
        unsafe {
            match prev {
                Some(p) => std::env::set_var("PATH", p),
                None => std::env::remove_var("PATH"),
            }
        }
        assert!(found, "is_on_path should find {bin_name} in synthetic PATH");
    }

    #[test]
    fn is_on_path_misses_garbage() {
        assert!(!is_on_path("this-binary-does-not-exist-zzz"));
    }
}