From 98c0d7f176eea1d48f0f0f0eeae3da98180c6ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Einar=20Bergstr=C3=B8m?= Date: Wed, 28 Oct 2026 13:03:21 +0100 Subject: [PATCH] feat: a link brings back the lazysecrets nvim hid; Azure shows its account MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - In nvim, lazysecrets asks lazysecrets.nvim whether its window is shown: a hidden one is brought back for a link, unless nvim is busy (typing, a command, a picker, mid-operator), when the link opens elsewhere. Without an answer (an older plugin), nothing changes. - The Azure group is named for its account, Azure · alice, also with a single account; its ID, and so stars and cache, stay as they were. --- docs/azure.md | 5 ++- docs/sharing.md | 5 +++ internal/instance/instance.go | 3 ++ internal/instance/nvim.go | 46 +++++++++++++++++++++ internal/instance/nvim_test.go | 64 +++++++++++++++++++++++++++++ internal/instance/tmux.go | 2 + internal/provider/azure/accounts.go | 11 +++-- 7 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 internal/instance/nvim.go create mode 100644 internal/instance/nvim_test.go diff --git a/docs/azure.md b/docs/azure.md index 5c11727..1667e3a 100644 --- a/docs/azure.md +++ b/docs/azure.md @@ -5,8 +5,9 @@ it reuses it. ## Accounts -Every account signed in to az gets its own group in the Sources pane -(`az login` again with another account to add one), and each account's +Every account signed in to az gets its own group in the Sources pane, +named for it, `Azure · alice` (`az login` again with another account to +add one), and each account's vaults are read with that account. A subscription several accounts can see is listed under each, since their permissions can differ. diff --git a/docs/sharing.md b/docs/sharing.md index 6168b3b..e588251 100644 --- a/docs/sharing.md +++ b/docs/sharing.md @@ -177,6 +177,11 @@ linkHandler: a hidden dropdown terminal (kitty's quick access terminal) is shown. With none running, a new window opens. +- **Hidden in nvim** ([lazysecrets.nvim](https://github.com/karleinarb/lazysecrets.nvim) + hides it when you leave its window): it's brought back, unless nvim is + in the middle of something a window popping up would break (typing, a + command, a picker); then it counts as not running. + The link is then offered as if pasted. `linkHandler.reuse: false` always opens a new window. diff --git a/internal/instance/instance.go b/internal/instance/instance.go index f4c5955..b73c120 100644 --- a/internal/instance/instance.go +++ b/internal/instance/instance.go @@ -123,6 +123,9 @@ func (s *Server) handle(c net.Conn) { switch req.Op { case "status": _, app, reach := place(req.Front) + if nvimState(context.Background()) == "busy" { + reach = unreachable // a link would interrupt you: it opens elsewhere + } s.mu.Lock() resp.Status = Status{PID: os.Getpid(), LastActive: s.active, Reach: reach, App: app} s.mu.Unlock() diff --git a/internal/instance/nvim.go b/internal/instance/nvim.go new file mode 100644 index 0000000..8e64c6a --- /dev/null +++ b/internal/instance/nvim.go @@ -0,0 +1,46 @@ +package instance + +import ( + "context" + "fmt" + "os" + "os/exec" + "strings" + "time" +) + +// In nvim (lazysecrets.nvim), the tmux pane can be in view while +// lazysecrets isn't: the plugin hides its window when you leave it. The +// plugin says (state) and brings it back (reveal), when nvim isn't in the +// middle of something a window popping up would break. + +// nvimState is what lazysecrets.nvim says of this lazysecrets: "shown", +// "hidden" (and can be brought back), "busy", or "" outside nvim, or with +// no answer (an older plugin, a plain :terminal): reach as before. +func nvimState(ctx context.Context) string { + switch s := nvim(ctx, "state"); s { + case "shown", "hidden", "busy": + return s + } + return "" +} + +// nvimReveal brings this lazysecrets' window back, if the plugin hid it. +func nvimReveal(ctx context.Context) { nvim(ctx, "reveal") } + +// nvim calls the plugin's fn with this process' pid, in the nvim that +// started it ($NVIM), and returns what it says. +func nvim(ctx context.Context, fn string) string { + addr := os.Getenv("NVIM") + if addr == "" { + return "" + } + ctx, cancel := context.WithTimeout(ctx, 500*time.Millisecond) // within the link handler's second; a stuck nvim is skipped + defer cancel() + expr := fmt.Sprintf(`luaeval("require('lazysecrets').%s(%d)")`, fn, os.Getpid()) + out, err := exec.CommandContext(ctx, "nvim", "--server", addr, "--remote-expr", expr).Output() + if err != nil { + return "" + } + return strings.TrimSpace(string(out)) +} diff --git a/internal/instance/nvim_test.go b/internal/instance/nvim_test.go new file mode 100644 index 0000000..2fd4811 --- /dev/null +++ b/internal/instance/nvim_test.go @@ -0,0 +1,64 @@ +package instance + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "strconv" + "testing" + "time" +) + +// With a stand-in lazysecrets.nvim in a headless nvim: what it says comes +// back, for this process' pid, and an older plugin's error is "". +func TestNvim(t *testing.T) { + if _, err := exec.LookPath("nvim"); err != nil { + t.Skip("needs nvim") + } + dir := t.TempDir() + lua := filepath.Join(dir, "lua") + os.MkdirAll(lua, 0o755) + pid := strconv.Itoa(os.Getpid()) + os.WriteFile(filepath.Join(lua, "lazysecrets.lua"), []byte(` +local M = {} +function M.state(pid) return pid == `+pid+` and vim.g.state or "none" end +function M.reveal(pid) vim.g.revealed = pid; return true end +return M +`), 0o644) + sock := filepath.Join(dir, "nvim.sock") + nv := exec.Command("nvim", "--headless", "--clean", "--listen", sock, "--cmd", "set rtp^="+dir, "--cmd", "let g:state = 'hidden'") + if err := nv.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { nv.Process.Kill(); nv.Wait() }) + for i := 0; i < 50; i++ { + if _, err := os.Stat(sock); err == nil { + break + } + time.Sleep(50 * time.Millisecond) + } + t.Setenv("NVIM", sock) + ctx := context.Background() + + if s := nvimState(ctx); s != "hidden" { + t.Errorf("state %q", s) + } + nvimReveal(ctx) + if got := nvim(ctx, "state"); got != "hidden" { + t.Errorf("after reveal: %q", got) + } + out, _ := exec.Command("nvim", "--server", sock, "--remote-expr", "g:revealed").Output() + if string(out) != pid { + t.Errorf("revealed %q, want this pid %s", out, pid) + } + + if got := nvim(ctx, "missing"); got != "" { // an older plugin + t.Errorf("a function the plugin lacks: %q", got) + } + + t.Setenv("NVIM", "") + if s := nvimState(ctx); s != "" { + t.Errorf("outside nvim: %q", s) + } +} diff --git a/internal/instance/tmux.go b/internal/instance/tmux.go index 6603f7c..5fa363b 100644 --- a/internal/instance/tmux.go +++ b/internal/instance/tmux.go @@ -2,6 +2,7 @@ package instance import ( "cmp" + "context" "os" "os/exec" "slices" @@ -101,6 +102,7 @@ func Raise(front string) { if reach == unreachable { return } + nvimReveal(context.Background()) if inTmux() { pane := os.Getenv("TMUX_PANE") _, _ = tmux("select-window", "-t", pane) diff --git a/internal/provider/azure/accounts.go b/internal/provider/azure/accounts.go index e760b82..c5828d3 100644 --- a/internal/provider/azure/accounts.go +++ b/internal/provider/azure/accounts.go @@ -181,10 +181,9 @@ func NewAccounts(s Settings) ([]*Provider, error) { if i > 0 { settings.Vaults = nil } - label := "" - if len(accounts) > 1 { - label = string(provider.KindAzure) + " · " + labels[i] - } + // Named for its account, also when it's the only one: which + // account lazysecrets sees Azure through is worth seeing. + label := string(provider.KindAzure) + " · " + labels[i] cred := authCredential{ cred: newCLICredential(a.user, a.tenant, func() ([]account, error) { return readAccounts(path) }), login: []string{"az", "login", "--tenant", a.tenant}, @@ -195,9 +194,9 @@ func NewAccounts(s Settings) ([]*Provider, error) { return nil, err } p.account = &accountDetails{account: a, claims: cred.cred.(*cliCredential).claims} - if label != "" { + p.label = label + if len(accounts) > 1 { // one keeps the plain ID its stars and cache are under p.id = string(provider.KindAzure) + ":" + a.user + "/" + a.tenant - p.label = label } out = append(out, p) } -- 2.54.1