- 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. Needs keb/lazysecrets.nvim#6 for the reveal; without it, links behave as before. Reviewed-on: #95 Co-authored-by: Karl-Einar Bergstrøm <karl-einarb@outlook.com>
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
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))
|
|
}
|