From b7d3fd3c86ed6a85a67637a31ea30063a2e0ec9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Sat, 28 Nov 2026 17:38:14 +0100 Subject: [PATCH] feat: configure chosen terminals for zsh-edit-select on apply Point the module at the JtheGunner fork (v0.7.02), whose installer takes --terminals, and add one bool option per terminal. The new check and install hooks run that installer for the chosen terminals and record the plugin commit plus the list, so it only reruns when either changes. Check hooks now also run for modules whose software comes only from a fallback. --- CHANGELOG.md | 14 +- README.md | 2 +- docs/design-decisions.md | 14 ++ docs/writing-a-module.md | 6 +- internal/engine/check_hook_fallback_test.go | 52 ++++ internal/engine/plan.go | 16 +- .../modules/fallback-hooked/bash.tmpl | 1 + .../modules/fallback-hooked/hooks/check.sh | 2 + .../modules/fallback-hooked/hooks/install.sh | 2 + .../modules/fallback-hooked/manifest.toml | 16 ++ .../testdata/modules/fallback-hooked/zsh.tmpl | 1 + .../builtin/zsh-edit-select/cheatsheet.tmpl | 2 +- .../builtin/zsh-edit-select/hooks/check.sh | 32 +++ .../builtin/zsh-edit-select/hooks/install.sh | 37 +++ modules/builtin/zsh-edit-select/manifest.toml | 78 +++++- .../zsh-edit-select/cheatsheet-zsh.golden | 2 +- modules/zsh_edit_select_hooks_test.go | 232 ++++++++++++++++++ modules/zsh_edit_select_test.go | 18 +- 18 files changed, 502 insertions(+), 25 deletions(-) create mode 100644 internal/engine/check_hook_fallback_test.go create mode 100644 internal/engine/testdata/modules/fallback-hooked/bash.tmpl create mode 100644 internal/engine/testdata/modules/fallback-hooked/hooks/check.sh create mode 100644 internal/engine/testdata/modules/fallback-hooked/hooks/install.sh create mode 100644 internal/engine/testdata/modules/fallback-hooked/manifest.toml create mode 100644 internal/engine/testdata/modules/fallback-hooked/zsh.tmpl create mode 100644 modules/builtin/zsh-edit-select/hooks/check.sh create mode 100644 modules/builtin/zsh-edit-select/hooks/install.sh create mode 100644 modules/zsh_edit_select_hooks_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index d2cc4b7..e115a49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,14 +8,20 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - `zsh-edit-select` module (zsh): edit the command line like a text editor with [zsh-edit-select](https://github.com/Michael-Matta1/zsh-edit-select) (Shift selection, copy, cut, paste, undo, mouse selection). Installed from a pinned - clone (`v0.7.01`); on macOS `apply` builds the clipboard agent from that clone + clone of the [JtheGunner fork](https://github.com/JtheGunner/zsh-edit-select) + (`v0.7.02`), whose installer can configure chosen terminals; on macOS `apply` builds the clipboard agent from that clone with `make`, so a failed build degrades the module instead of surfacing on the first shell start. On Linux the plugin still downloads its agent itself on the first shell start, from its latest release instead of the pinned tag. It loads after `autosuggestions` and `syntax-highlighting`. - The terminal needs key bindings for it, which omnishell does not write; the - cheatsheet lists them. A manual install of the plugin blocks the module until - its `source` line is removed. + The terminal needs key bindings for it: switch on the `terminal_*` options + (`terminal_ghostty`, `terminal_kitty`, …; off by default) and `apply` runs the + plugin's own installer for those terminals, again only when the choice or the + plugin changes. A failing installer degrades the module. Those files are not + covered by backups, `rollback` or `doctor`. A manual install of the plugin + blocks the module until its `source` line is removed. +- Module check hooks now also run for modules whose software comes only from a + fallback, so a module can finish its setup after the clone. ## [0.10.0] - 2026-11-28 diff --git a/README.md b/README.md index 81e261d..80b96db 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ copy is backed up. Only `apply`, `remove`, and `uninstall` change your system (a | `pay-respects` | Suggest a corrected command after a failed one, run via the alias (a Rust `thefuck` alternative) — [iffse/pay-respects](https://github.com/iffse/pay-respects) | `pay-respects` (`brew`); else `git` + `cargo` fallback (needs Rust) | zsh, bash | `alias` (string, default `f`) | | `atuin` | SQLite-backed shell history with fuzzy search, context and stats — [atuinsh/atuin](https://github.com/atuinsh/atuin). **Mutually exclusive with `fzf`** (both bind Ctrl+R) — enabling both is a config error. | `atuin` (`brew`, `pacman`); else `git` + `cargo` fallback (needs Rust) | zsh, bash | `bind_ctrl_r` (bool, default `true`), `bind_up_arrow` (bool, default `true`) | | `ble-sh` | Autosuggestions and syntax highlighting for **bash** (until now zsh-only) via [ble.sh](https://github.com/akinomyoga/ble.sh). **Opt-in, bash only: it replaces parts of Readline** with its own line editor, so it **conflicts with `fzf` and `atuin`** (they rebind Ctrl+R and the completion keys; enabling either alongside is a config error) and may interact with your own `bind` settings. Loads after `completion` and `history`; sourced with `--attach=prompt` in interactive shells only. Built from a pinned clone (`v0.4.0-devel3`), so it needs `make`, `gawk` and `git` (on macOS: `brew install gawk`). A missing build is skipped silently. | pinned `git` clone built with `make` (no package manager ships it) | bash | — | -| `zsh-edit-select` | Edit the command line like a text editor: Shift+arrow selection, copy, cut, paste, undo/redo, type-to-replace and mouse selections — [Michael-Matta1/zsh-edit-select](https://github.com/Michael-Matta1/zsh-edit-select) (MIT). **It needs terminal key bindings** that send its key sequences to the shell (Ghostty, iTerm2, Kitty, WezTerm, VS Code: see the plugin's README; restart the terminal afterwards), and on macOS Accessibility permission for mouse selections (`edit-select setup-ax`). omnishell writes no terminal configuration. Loads after `completion`, `autosuggestions` and `syntax-highlighting`. **A manual install blocks it**: remove the plugin's `source` line from `~/.zshrc`, delete the old clone and stop its agent. `omnishell cheatsheet` lists the keys | pinned `git` clone (`v0.7.01`); on macOS `apply` builds the clipboard agent with `make` (Xcode Command Line Tools). **On Linux the plugin itself downloads its agent on the first shell start, from its latest GitHub release rather than the pinned tag** (checked against that release's `SHA256SUMS.txt`, not against anything omnishell pins); `edit-select build` compiles it from source instead | zsh | — | +| `zsh-edit-select` | Edit the command line like a text editor: Shift+arrow selection, copy, cut, paste, undo/redo, type-to-replace and mouse selections — [Michael-Matta1/zsh-edit-select](https://github.com/Michael-Matta1/zsh-edit-select) (MIT), installed from the [JtheGunner fork](https://github.com/JtheGunner/zsh-edit-select) whose installer can configure terminals. **It needs terminal key bindings** that send its key sequences to the shell: switch on the terminals you use with the `terminal_*` options and `apply` runs the plugin's own installer for them (restart the terminal afterwards). On macOS it also needs Accessibility permission for mouse selections (`edit-select setup-ax`). Those terminal files are written by the plugin's installer, not by omnishell, so backups, `rollback` and `doctor` do not cover them. Loads after `completion`, `autosuggestions` and `syntax-highlighting`. **A manual install blocks it**: remove the plugin's `source` line from `~/.zshrc`, delete the old clone and stop its agent. `omnishell cheatsheet` lists the keys | pinned `git` clone of the fork (`v0.7.02`); on macOS `apply` builds the clipboard agent with `make` (Xcode Command Line Tools). **On Linux the plugin itself downloads its agent on the first shell start, from its latest GitHub release rather than the pinned tag** (checked against that release's `SHA256SUMS.txt`, not against anything omnishell pins); `edit-select build` compiles it from source instead | zsh | `terminal_ghostty`, `terminal_kitty`, `terminal_alacritty`, `terminal_wezterm`, `terminal_foot`, `terminal_konsole`, `terminal_iterm2`, `terminal_vscode`, `terminal_windows_terminal` (bool, default `false`) | | `modern-aliases` | Replace ls/cat/find with [eza](https://github.com/eza-community/eza), [bat](https://github.com/sharkdp/bat) and [fd](https://github.com/sharkdp/fd) when selected | `eza`, `bat`, `fd`; `eza` as a release binary where the manager has no package | zsh, bash | `replace` (`list` of `ls`, `cat`, `find`; default `["ls", "cat", "find"]`) | | `colorized-man` | Syntax-highlighted man pages via [bat](https://github.com/sharkdp/bat) when present, with a zero-dependency `less` colour fallback | — (config only) | zsh, bash | — | | `tealdeer` | A fast `tldr` client: short, practical command examples instead of long man pages — [tealdeer-rs/tealdeer](https://github.com/tealdeer-rs/tealdeer). The page cache is not filled by `apply`; `tldr` downloads it on first use | `tealdeer` (all managers); else a verified release binary (Linux x86_64, arm64, armv7) | zsh, bash | — | diff --git a/docs/design-decisions.md b/docs/design-decisions.md index 4a2bfe5..974e1f4 100644 --- a/docs/design-decisions.md +++ b/docs/design-decisions.md @@ -43,6 +43,20 @@ not built. For how the features work, see the [README](../README.md), unset, so existing user settings win. `GIT_PAGER` overrides `core.pager` without editing it. `interactive.diffFilter` (for `git add -p`) cannot be set through the environment and is left to the user. +- **Exception: `zsh-edit-select` terminal setup.** The plugin only works once + the terminal sends its key sequences to the shell. omnishell does not write + those files itself: the module's `install` hook runs the plugin's own + installer (`--mode integrate --terminals `) for the terminals the user + switched on through the `terminal_*` options. The installer owns the format + and the files (for Ghostty a block in `ghostty.local` plus one import line). + Consequences: backups, `rollback` and `doctor` do not cover these files; the + installer keeps its own backup, and its uninstaller removes the blocks. + `omnishell remove` leaves them in place. The `check` hook records the plugin + commit and the terminal list in `zsh-edit-select.terminals` in the config + dir, so the installer runs again only when either changes, and a failing + installer degrades the module and leaves no record, so the next `apply` + retries. The options are bools because the TUI and `omnishell set` edit + those but not lists. ## Self-update diff --git a/docs/writing-a-module.md b/docs/writing-a-module.md index bb62b20..de801ac 100644 --- a/docs/writing-a-module.md +++ b/docs/writing-a-module.md @@ -300,8 +300,10 @@ context as environment variables: | `OMNISHELL_OPT_` | one per option, e.g. `OMNISHELL_OPT_LOG_FORMAT` | - `check.sh` — exit `0` = requirement satisfied, exit `1` = missing. Consulted - when the module declares no `[packages]` entry for the detected manager, or - to gate rendering. + when the module declares no `[packages]` entry for the detected manager, to + gate rendering, or when the module's software comes only from a + `[[packages.fallback]]` (no manager packages), so it can finish its setup + after the clone. - `install.sh` — run only when `check.sh` fails and the package path did not satisfy the requirement. - `remove.sh` — run by `omnishell remove --purge`. diff --git a/internal/engine/check_hook_fallback_test.go b/internal/engine/check_hook_fallback_test.go new file mode 100644 index 0000000..d09e090 --- /dev/null +++ b/internal/engine/check_hook_fallback_test.go @@ -0,0 +1,52 @@ +package engine_test + +import ( + "strings" + "testing" + + "github.com/JtheGunner/omnishell/internal/engine" + "github.com/JtheGunner/omnishell/internal/pkgmgr" +) + +// A module whose software comes only from a fallback can still be gated by a +// check hook: the clone is not the whole requirement (zsh-edit-select configures +// terminals after cloning it). +func TestComputePlanUsesCheckHookForFallbackOnlyModule(t *testing.T) { + withManager := testEngine(t, &pkgmgr.MockManager{NameV: "apt", DetectV: true, Installed: map[string]bool{}}) + for name, e := range map[string]engine.Engine{ + "without a manager": noManagerEngine(t), + "with a manager": withManager, + } { + if mp := planNoManager(t, e, "fallback-hooked"); !mp.UsesCheckHook { + t.Errorf("%s: UsesCheckHook = false, want true", name) + } + } +} + +// A module without a check hook never gets one, whatever its packages are. +func TestComputePlanLeavesModuleWithoutCheckHookAlone(t *testing.T) { + e := testEngine(t, &pkgmgr.MockManager{NameV: "apt", DetectV: true, Installed: map[string]bool{"fzf": true}}) + if mp := planNoManager(t, e, "fzf"); mp.UsesCheckHook { + t.Error("fzf has no check hook and must not use one") + } + if mp := planNoManager(t, noManagerEngine(t), "cargo-built"); mp.UsesCheckHook { + t.Error("cargo-built has no check hook and must not use one") + } +} + +func TestApplyRunsCheckHookForFallbackOnlyModule(t *testing.T) { + runner := &pkgmgr.MockRunner{} + mgr := &pkgmgr.MockManager{NameV: "apt", DetectV: true, Installed: map[string]bool{}} + if _, err := applyModule(t, "fallback-hooked", mgr, runner); err != nil { + t.Fatal(err) + } + var ran bool + for _, c := range runner.Calls { + if strings.Contains(c, "omnishell-check-") { + ran = true + } + } + if !ran { + t.Fatalf("check hook was not run; calls: %v", runner.Calls) + } +} diff --git a/internal/engine/plan.go b/internal/engine/plan.go index 49f5c5b..4de515b 100644 --- a/internal/engine/plan.go +++ b/internal/engine/plan.go @@ -286,11 +286,23 @@ func ComputePlan(e Engine, cfg config.Config, lock lockfile.Lock, noPackages boo return p, nil } +// hasManagerPackages reports whether the manifest lists packages for any +// package manager. +func hasManagerPackages(mf module.Manifest) bool { + return len(mf.Packages.Brew)+len(mf.Packages.Apt)+len(mf.Packages.Dnf)+ + len(mf.Packages.Pacman)+len(mf.Packages.Zypper)+len(mf.Packages.Apk) > 0 +} + func planPackages(mp *ModulePlan, e Engine, mod module.Module, shells []string, prev lockfile.ModuleState) { mf := mp.Manifest + // A module whose software comes only from a fallback is not done once the + // clone is in place (zsh-edit-select configures terminals afterwards), so + // its check hook is consulted too. + if mod.HasHook("check") && len(mf.Packages.Fallback) > 0 && !hasManagerPackages(mf) { + mp.UsesCheckHook = true + } if !e.ManagerOK { - if len(mf.Packages.Brew)+len(mf.Packages.Apt)+len(mf.Packages.Dnf)+ - len(mf.Packages.Pacman)+len(mf.Packages.Zypper)+len(mf.Packages.Apk) > 0 { + if hasManagerPackages(mf) { mp.DegradedReason = "no package manager detected" return } diff --git a/internal/engine/testdata/modules/fallback-hooked/bash.tmpl b/internal/engine/testdata/modules/fallback-hooked/bash.tmpl new file mode 100644 index 0000000..918fcaf --- /dev/null +++ b/internal/engine/testdata/modules/fallback-hooked/bash.tmpl @@ -0,0 +1 @@ +echo fallback-hooked diff --git a/internal/engine/testdata/modules/fallback-hooked/hooks/check.sh b/internal/engine/testdata/modules/fallback-hooked/hooks/check.sh new file mode 100644 index 0000000..039e4d0 --- /dev/null +++ b/internal/engine/testdata/modules/fallback-hooked/hooks/check.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exit 0 diff --git a/internal/engine/testdata/modules/fallback-hooked/hooks/install.sh b/internal/engine/testdata/modules/fallback-hooked/hooks/install.sh new file mode 100644 index 0000000..039e4d0 --- /dev/null +++ b/internal/engine/testdata/modules/fallback-hooked/hooks/install.sh @@ -0,0 +1,2 @@ +#!/bin/sh +exit 0 diff --git a/internal/engine/testdata/modules/fallback-hooked/manifest.toml b/internal/engine/testdata/modules/fallback-hooked/manifest.toml new file mode 100644 index 0000000..72bb7ca --- /dev/null +++ b/internal/engine/testdata/modules/fallback-hooked/manifest.toml @@ -0,0 +1,16 @@ +platforms = ["macos", "linux"] +shells = ["zsh", "bash"] +requires = [] +after = [] + +[module] +id = "fallback-hooked" +name = "Fallback Hooked" +description = "Fixture: a module whose only software is a git clone, gated by a check hook" +version = "1.0.0" +schema = 1 + +[[packages.fallback]] +type = "git" +repo = "https://example.com/fallback-hooked.git" +dest = "{{.VendorDir}}/fallback-hooked" diff --git a/internal/engine/testdata/modules/fallback-hooked/zsh.tmpl b/internal/engine/testdata/modules/fallback-hooked/zsh.tmpl new file mode 100644 index 0000000..918fcaf --- /dev/null +++ b/internal/engine/testdata/modules/fallback-hooked/zsh.tmpl @@ -0,0 +1 @@ +echo fallback-hooked diff --git a/modules/builtin/zsh-edit-select/cheatsheet.tmpl b/modules/builtin/zsh-edit-select/cheatsheet.tmpl index 0556e7b..69a08bb 100644 --- a/modules/builtin/zsh-edit-select/cheatsheet.tmpl +++ b/modules/builtin/zsh-edit-select/cheatsheet.tmpl @@ -3,6 +3,6 @@ - `{{ $mod }}+A` select all, `{{ $mod }}+C` copy, `{{ $mod }}+X` cut, `{{ $mod }}+Z` undo, `{{ $mod }}+Shift+Z` redo - Paste with your terminal's own paste shortcut; it replaces a selection - Run `edit-select config` to change the mouse behaviour and the key bindings -- Needs terminal key bindings that send these sequences to the shell (examples for Ghostty, iTerm2, Kitty, WezTerm and VS Code are in the plugin's README); restart the terminal afterwards +- Needs terminal key bindings that send these sequences to the shell: switch on your terminals with `omnishell set zsh-edit-select terminal_ghostty true` (also `terminal_kitty`, `terminal_alacritty`, `terminal_wezterm`, `terminal_foot`, `terminal_konsole`, `terminal_iterm2`, `terminal_vscode`, `terminal_windows_terminal`) or in the TUI, then run `omnishell apply` and restart the terminal {{ if eq .Platform "macos" }}- Mouse selections need Accessibility permission for your terminal: run `edit-select setup-ax` {{ end }}- A manual install of the plugin blocks this module: remove its `source` line from `~/.zshrc`, delete the old clone and stop its agent, otherwise nothing is loaded from here diff --git a/modules/builtin/zsh-edit-select/hooks/check.sh b/modules/builtin/zsh-edit-select/hooks/check.sh new file mode 100644 index 0000000..7a90ab2 --- /dev/null +++ b/modules/builtin/zsh-edit-select/hooks/check.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Exit 0 when the terminals chosen in the module options are already set up for +# the current plugin clone, 1 when hooks/install.sh has to run. With no terminal +# chosen there is nothing to configure. +# +# The state is one line in $OMNISHELL_CONFIG_DIR/zsh-edit-select.terminals: +# " ". + +terminals="" +add() { + if [ "$2" = "true" ]; then + terminals="${terminals:+$terminals,}$1" + fi +} + +add ghostty "$OMNISHELL_OPT_TERMINAL_GHOSTTY" +add kitty "$OMNISHELL_OPT_TERMINAL_KITTY" +add alacritty "$OMNISHELL_OPT_TERMINAL_ALACRITTY" +add wezterm "$OMNISHELL_OPT_TERMINAL_WEZTERM" +add foot "$OMNISHELL_OPT_TERMINAL_FOOT" +add konsole "$OMNISHELL_OPT_TERMINAL_KONSOLE" +add iterm2 "$OMNISHELL_OPT_TERMINAL_ITERM2" +add vscode "$OMNISHELL_OPT_TERMINAL_VSCODE" +add windows-terminal "$OMNISHELL_OPT_TERMINAL_WINDOWS_TERMINAL" + +[ -z "$terminals" ] && exit 0 + +plugin="$OMNISHELL_VENDOR_DIR/zsh-edit-select" +commit=$(git -C "$plugin" rev-parse HEAD 2>/dev/null) || exit 1 + +state="$OMNISHELL_CONFIG_DIR/zsh-edit-select.terminals" +[ "$(cat "$state" 2>/dev/null)" = "$commit $terminals" ] diff --git a/modules/builtin/zsh-edit-select/hooks/install.sh b/modules/builtin/zsh-edit-select/hooks/install.sh new file mode 100644 index 0000000..a3d4f43 --- /dev/null +++ b/modules/builtin/zsh-edit-select/hooks/install.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Configure the chosen terminals with the plugin's own installer, then record +# what was done so hooks/check.sh stays quiet until the choice or the plugin +# changes. The state is written only when the installer succeeds; a failing +# installer leaves it alone, so the next `apply` tries again. + +terminals="" +add() { + if [ "$2" = "true" ]; then + terminals="${terminals:+$terminals,}$1" + fi +} + +add ghostty "$OMNISHELL_OPT_TERMINAL_GHOSTTY" +add kitty "$OMNISHELL_OPT_TERMINAL_KITTY" +add alacritty "$OMNISHELL_OPT_TERMINAL_ALACRITTY" +add wezterm "$OMNISHELL_OPT_TERMINAL_WEZTERM" +add foot "$OMNISHELL_OPT_TERMINAL_FOOT" +add konsole "$OMNISHELL_OPT_TERMINAL_KONSOLE" +add iterm2 "$OMNISHELL_OPT_TERMINAL_ITERM2" +add vscode "$OMNISHELL_OPT_TERMINAL_VSCODE" +add windows-terminal "$OMNISHELL_OPT_TERMINAL_WINDOWS_TERMINAL" + +[ -z "$terminals" ] && exit 0 + +plugin="$OMNISHELL_VENDOR_DIR/zsh-edit-select" +installer="$plugin/assets/auto-install/install.sh" +if [ ! -f "$installer" ]; then + echo "zsh-edit-select: installer not found at $installer" >&2 + exit 1 +fi + +commit=$(git -C "$plugin" rev-parse HEAD 2>/dev/null) || exit 1 + +bash "$installer" --local --mode integrate --non-interactive --terminals "$terminals" "$OMNISHELL_CONFIG_DIR/zsh-edit-select.terminals" diff --git a/modules/builtin/zsh-edit-select/manifest.toml b/modules/builtin/zsh-edit-select/manifest.toml index 049196b..b622513 100644 --- a/modules/builtin/zsh-edit-select/manifest.toml +++ b/modules/builtin/zsh-edit-select/manifest.toml @@ -6,23 +6,79 @@ after = ["completion", "autosuggestions", "syntax-highlighting"] id = "zsh-edit-select" name = "zsh-edit-select" description = "Edit the command line like a text editor: Shift selection, copy, cut, paste, undo and mouse selection (zsh)" -homepage = "https://github.com/Michael-Matta1/zsh-edit-select" -version = "1.0.0" +homepage = "https://github.com/JtheGunner/zsh-edit-select" +version = "1.1.0" schema = 1 -# No package manager ships the plugin, so it is always a pinned clone (MIT). -# It also needs a native clipboard agent. On macOS the agent is built here, from -# the pinned clone, so a failure shows up in `apply`; the plugin would otherwise -# download an unpinned binary from its latest release on the first shell start. -# The first run entry renders to an empty command on every other platform, which -# skips the build: on Linux the agent depends on the display server (X11 or -# Wayland) and its development libraries, so the plugin provisions it itself. +# No package manager ships the plugin, so it is always a pinned clone (MIT). The +# clone is the JtheGunner fork of Michael-Matta1/zsh-edit-select: its installer +# takes a terminal list (--terminals) and writes the Ghostty keybinds to +# ghostty.local, which the upstream installer does not do yet. +# +# The plugin also needs a native clipboard agent. On macOS the agent is built +# here, from the pinned clone, so a failure shows up in `apply`; the plugin would +# otherwise download an unpinned binary from its latest release on the first +# shell start. The first run entry renders to an empty command on every other +# platform, which skips the build: on Linux the agent depends on the display +# server (X11 or Wayland) and its development libraries, so the plugin +# provisions it itself. +# # Loading after autosuggestions and syntax-highlighting is the order the plugin # is written for: it registers its hooks on the first prompt, after the widget # wrappers those plugins install at load time exist. [[packages.fallback]] type = "git" -repo = "https://github.com/Michael-Matta1/zsh-edit-select.git" +repo = "https://github.com/JtheGunner/zsh-edit-select.git" dest = "{{.VendorDir}}/zsh-edit-select" -ref = "v0.7.01" +ref = "v0.7.02" run = ["{{ if eq .Platform \"macos\" }}make{{ end }}", "-C", "{{.VendorDir}}/zsh-edit-select/impl-macos/backends/macos"] + +# The plugin only works once the terminal sends its key sequences to the shell. +# Each option below names a terminal the plugin's own installer should +# configure during `apply` (hooks/check.sh and hooks/install.sh). All are off by +# default: without a choice no terminal config is touched. They are bool options +# because the TUI and `omnishell set` can change those, but not lists. +[options.terminal_ghostty] +type = "bool" +default = false +help = "Configure Ghostty (keybinds go to ghostty.local; restart Ghostty afterwards)" + +[options.terminal_kitty] +type = "bool" +default = false +help = "Configure Kitty" + +[options.terminal_alacritty] +type = "bool" +default = false +help = "Configure Alacritty" + +[options.terminal_wezterm] +type = "bool" +default = false +help = "Configure WezTerm" + +[options.terminal_foot] +type = "bool" +default = false +help = "Configure Foot (Linux only)" + +[options.terminal_konsole] +type = "bool" +default = false +help = "Configure Konsole (Linux only)" + +[options.terminal_iterm2] +type = "bool" +default = false +help = "Configure iTerm2 (macOS only)" + +[options.terminal_vscode] +type = "bool" +default = false +help = "Configure the VS Code integrated terminal" + +[options.terminal_windows_terminal] +type = "bool" +default = false +help = "Configure Windows Terminal (WSL only)" diff --git a/modules/testdata/zsh-edit-select/cheatsheet-zsh.golden b/modules/testdata/zsh-edit-select/cheatsheet-zsh.golden index db297cb..dd67ff0 100644 --- a/modules/testdata/zsh-edit-select/cheatsheet-zsh.golden +++ b/modules/testdata/zsh-edit-select/cheatsheet-zsh.golden @@ -2,6 +2,6 @@ - `Cmd+A` select all, `Cmd+C` copy, `Cmd+X` cut, `Cmd+Z` undo, `Cmd+Shift+Z` redo - Paste with your terminal's own paste shortcut; it replaces a selection - Run `edit-select config` to change the mouse behaviour and the key bindings -- Needs terminal key bindings that send these sequences to the shell (examples for Ghostty, iTerm2, Kitty, WezTerm and VS Code are in the plugin's README); restart the terminal afterwards +- Needs terminal key bindings that send these sequences to the shell: switch on your terminals with `omnishell set zsh-edit-select terminal_ghostty true` (also `terminal_kitty`, `terminal_alacritty`, `terminal_wezterm`, `terminal_foot`, `terminal_konsole`, `terminal_iterm2`, `terminal_vscode`, `terminal_windows_terminal`) or in the TUI, then run `omnishell apply` and restart the terminal - Mouse selections need Accessibility permission for your terminal: run `edit-select setup-ax` - A manual install of the plugin blocks this module: remove its `source` line from `~/.zshrc`, delete the old clone and stop its agent, otherwise nothing is loaded from here diff --git a/modules/zsh_edit_select_hooks_test.go b/modules/zsh_edit_select_hooks_test.go new file mode 100644 index 0000000..03b951e --- /dev/null +++ b/modules/zsh_edit_select_hooks_test.go @@ -0,0 +1,232 @@ +package modules_test + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// zesHookEnv is a sandbox for the zsh-edit-select hooks: a vendor dir whose +// plugin clone is a real git repo with a stub installer that records its +// arguments, and a config dir for the state file. +type zesHookEnv struct { + t *testing.T + vendor string + configDir string + argsLog string + options map[string]string +} + +func newZesHookEnv(t *testing.T) *zesHookEnv { + t.Helper() + git, err := exec.LookPath("git") + if err != nil { + t.Skip("git not installed") + } + if _, err := exec.LookPath("sh"); err != nil { + t.Skip("sh not installed") + } + + root := t.TempDir() + env := &zesHookEnv{ + t: t, + vendor: filepath.Join(root, "vendor"), + configDir: filepath.Join(root, "config"), + argsLog: filepath.Join(root, "installer-args.log"), + options: map[string]string{}, + } + plugin := filepath.Join(env.vendor, "zsh-edit-select") + installerDir := filepath.Join(plugin, "assets", "auto-install") + for _, dir := range []string{installerDir, env.configDir} { + if err := os.MkdirAll(dir, 0o755); err != nil { + t.Fatal(err) + } + } + env.writeStubInstaller(installerDir, 0) + + for _, args := range [][]string{ + {"init", "-q"}, + {"-c", "user.name=t", "-c", "user.email=t@example.com", "add", "-A"}, + {"-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "stub"}, + } { + cmd := exec.Command(git, append([]string{"-C", plugin}, args...)...) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + } + return env +} + +// writeStubInstaller replaces the installer with one that logs its arguments +// and exits with code. +func (e *zesHookEnv) writeStubInstaller(installerDir string, code int) { + e.t.Helper() + script := "#!/bin/bash\necho \"$*\" >>" + e.argsLog + "\nexit " + string(rune('0'+code)) + "\n" + if err := os.WriteFile(filepath.Join(installerDir, "install.sh"), []byte(script), 0o755); err != nil { + e.t.Fatal(err) + } +} + +// run materialises the hook like the engine does and runs it with the module +// environment. It returns the exit code. +func (e *zesHookEnv) run(hook string) int { + e.t.Helper() + body, err := zesModule(e.t).ReadHook(hook) + if err != nil { + e.t.Fatalf("read %s hook: %v", hook, err) + } + script := filepath.Join(e.t.TempDir(), hook+".sh") + if err := os.WriteFile(script, body, 0o700); err != nil { + e.t.Fatal(err) + } + + cmd := exec.Command("sh", script) + cmd.Env = append(os.Environ(), + "OMNISHELL_VENDOR_DIR="+e.vendor, + "OMNISHELL_CONFIG_DIR="+e.configDir, + "OMNISHELL_PLATFORM=macos", + "OMNISHELL_SHELL=zsh", + ) + for _, name := range []string{ + "ghostty", "kitty", "alacritty", "wezterm", "foot", "konsole", "iterm2", "vscode", "windows_terminal", + } { + value := e.options[name] + if value == "" { + value = "false" + } + cmd.Env = append(cmd.Env, "OMNISHELL_OPT_TERMINAL_"+strings.ToUpper(name)+"="+value) + } + if out, err := cmd.CombinedOutput(); err != nil { + if exit, ok := err.(*exec.ExitError); ok { + return exit.ExitCode() + } + e.t.Fatalf("%s hook: %v\n%s", hook, err, out) + } + return 0 +} + +func (e *zesHookEnv) installerCalls() []string { + data, err := os.ReadFile(e.argsLog) + if err != nil { + return nil + } + return strings.Split(strings.TrimSpace(string(data)), "\n") +} + +func (e *zesHookEnv) stateFile() string { + return filepath.Join(e.configDir, "zsh-edit-select.terminals") +} + +func TestZshEditSelectHooksExist(t *testing.T) { + m := zesModule(t) + for _, hook := range []string{"check", "install"} { + if !m.HasHook(hook) { + t.Errorf("module has no %s hook", hook) + } + } +} + +// With no terminal chosen nothing is configured and nothing is recorded. +func TestZshEditSelectHooksDoNothingWithoutTerminals(t *testing.T) { + env := newZesHookEnv(t) + if code := env.run("check"); code != 0 { + t.Errorf("check = %d, want 0 (nothing to configure)", code) + } + if code := env.run("install"); code != 0 { + t.Errorf("install = %d, want 0", code) + } + if calls := env.installerCalls(); len(calls) != 0 { + t.Errorf("installer ran without a chosen terminal: %v", calls) + } + if _, err := os.Stat(env.stateFile()); err == nil { + t.Error("a state file was written without a chosen terminal") + } +} + +func TestZshEditSelectHooksConfigureChosenTerminalsOnce(t *testing.T) { + env := newZesHookEnv(t) + env.options["ghostty"] = "true" + env.options["kitty"] = "true" + + if code := env.run("check"); code != 1 { + t.Fatalf("check before the first install = %d, want 1", code) + } + if code := env.run("install"); code != 0 { + t.Fatalf("install = %d, want 0", code) + } + want := "--local --mode integrate --non-interactive --terminals ghostty,kitty" + if calls := env.installerCalls(); len(calls) != 1 || calls[0] != want { + t.Fatalf("installer calls = %v, want exactly [%q]", calls, want) + } + if code := env.run("check"); code != 0 { + t.Errorf("check after install = %d, want 0 (so the installer is not run again)", code) + } +} + +func TestZshEditSelectHooksRerunWhenTheChoiceChanges(t *testing.T) { + env := newZesHookEnv(t) + env.options["ghostty"] = "true" + if code := env.run("install"); code != 0 { + t.Fatalf("install = %d", code) + } + + env.options["kitty"] = "true" + if code := env.run("check"); code != 1 { + t.Errorf("check after adding a terminal = %d, want 1", code) + } + + env.options["kitty"] = "false" + if code := env.run("check"); code != 0 { + t.Errorf("check with the original choice = %d, want 0", code) + } +} + +func TestZshEditSelectHooksRerunWhenThePluginChanges(t *testing.T) { + env := newZesHookEnv(t) + env.options["ghostty"] = "true" + if code := env.run("install"); code != 0 { + t.Fatalf("install = %d", code) + } + + plugin := filepath.Join(env.vendor, "zsh-edit-select") + cmd := exec.Command("git", "-C", plugin, "-c", "user.name=t", "-c", "user.email=t@example.com", + "commit", "-q", "--allow-empty", "-m", "next") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git commit: %v\n%s", err, out) + } + if code := env.run("check"); code != 1 { + t.Errorf("check after the clone moved = %d, want 1", code) + } +} + +// A failing installer must fail the hook and leave no state, so the next +// apply tries again instead of believing the terminals are configured. +func TestZshEditSelectHookFailsAndKeepsNoStateWhenInstallerFails(t *testing.T) { + env := newZesHookEnv(t) + env.options["ghostty"] = "true" + env.writeStubInstaller(filepath.Join(env.vendor, "zsh-edit-select", "assets", "auto-install"), 1) + + if code := env.run("install"); code == 0 { + t.Fatal("install = 0, want a failure when the installer fails") + } + if _, err := os.Stat(env.stateFile()); err == nil { + t.Error("a state file was written after a failed installer run") + } + if code := env.run("check"); code != 1 { + t.Errorf("check after a failed run = %d, want 1", code) + } +} + +func TestZshEditSelectHookFailsWhenInstallerIsMissing(t *testing.T) { + env := newZesHookEnv(t) + env.options["ghostty"] = "true" + installer := filepath.Join(env.vendor, "zsh-edit-select", "assets", "auto-install", "install.sh") + if err := os.Remove(installer); err != nil { + t.Fatal(err) + } + if code := env.run("install"); code == 0 { + t.Error("install = 0, want a failure when the installer is missing") + } +} diff --git a/modules/zsh_edit_select_test.go b/modules/zsh_edit_select_test.go index a63d081..df7817d 100644 --- a/modules/zsh_edit_select_test.go +++ b/modules/zsh_edit_select_test.go @@ -91,8 +91,20 @@ func TestZshEditSelectManifest(t *testing.T) { t.Fatalf("fallbacks = %+v, want exactly one", fbs) } fb := fbs[0] - if fb.Type != "git" || fb.Ref != "v0.7.01" || fb.Repo != "https://github.com/Michael-Matta1/zsh-edit-select.git" { - t.Errorf("fallback = %+v, want the pinned git clone", fb) + if fb.Type != "git" || fb.Ref != "v0.7.02" || fb.Repo != "https://github.com/JtheGunner/zsh-edit-select.git" { + t.Errorf("fallback = %+v, want the pinned git clone of the fork", fb) + } + for _, name := range []string{ + "ghostty", "kitty", "alacritty", "wezterm", "foot", "konsole", "iterm2", "vscode", "windows_terminal", + } { + opt, ok := m.Manifest.Options["terminal_"+name] + if !ok { + t.Errorf("missing option terminal_%s", name) + continue + } + if opt.Type != "bool" || opt.Default != false { + t.Errorf("terminal_%s = %+v, want a bool option defaulting to false", name, opt) + } } if got := strings.Join(m.Manifest.After, ","); got != "completion,autosuggestions,syntax-highlighting" { t.Errorf("after = %q", got) @@ -143,7 +155,7 @@ func TestZshEditSelectBuildRunsOnMacOSOnly(t *testing.T) { build := "make -C " + filepath.Join(vendor, "zsh-edit-select", "impl-macos", "backends", "macos") var cloned, built bool for _, c := range runner.Calls { - if strings.Contains(c, " clone ") && strings.Contains(c, "--branch v0.7.01") { + if strings.Contains(c, " clone ") && strings.Contains(c, "--branch v0.7.02") { cloned = true } if c == build {