lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 6873c9fe646f26c0dcecb4e20039dc30baf36059
parent c3c521691e90bcc9dbcaad08b732a6545f163603
Author: Sean Enck <sean@ttypty.com>
Date:   Thu,  4 Nov 2021 18:56:16 -0400

working through multiple clipboard commands

Diffstat:
Mcmd/lb/main.go | 10+++++++++-
Minternal/clip.go | 52++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/cmd/lb/main.go b/cmd/lb/main.go @@ -175,11 +175,19 @@ func main() { if err != nil { stock.Die("unable to read value to clear", err) } + _, paste, err := internal.GetClipboardCommand() + if err != nil { + stock.Die("unable to get paste command", err) + } + var args []string + if len(paste) > 1 { + args = paste[1:] + } val = strings.TrimSpace(val) for idx < internal.MaxClipTime { idx++ time.Sleep(1 * time.Second) - out, err := exec.Command("pbpaste").Output() + out, err := exec.Command(paste[0], args...).Output() if err != nil { continue } diff --git a/internal/clip.go b/internal/clip.go @@ -2,19 +2,66 @@ package internal import ( "fmt" + "os" "os/exec" + "strings" "voidedtech.com/stock" ) const ( // MaxClipTime is the max time to let something stay in the clipboard. - MaxClipTime = 45 + MaxClipTime = 45 + pbClipMode = "pb" + waylandClipMode = "wayland" + xClipMode = "x11" ) +// GetClipboardCommand will retrieve the commands to use for clipboard operations. +func GetClipboardCommand() ([]string, []string, error) { + env := strings.TrimSpace(os.Getenv("LOCKBOX_CLIPMODE")) + if env == "" { + b, err := exec.Command("uname").Output() + if err != nil { + return nil, nil, err + } + uname := strings.TrimSpace(string(b)) + switch uname { + case "Darwin": + env = pbClipMode + case "Linux": + if strings.TrimSpace(os.Getenv("WAYLAND_DISPLAY")) == "" { + env = xClipMode + } else { + env = waylandClipMode + } + default: + return nil, nil, stock.NewBasicError("unable to detect clipboard mode") + } + } + switch env { + case pbClipMode: + return []string{"pbcopy"}, []string{"pbpaste"}, nil + case xClipMode: + return []string{"xclip"}, []string{"xclip", "-o"}, nil + case waylandClipMode: + return []string{"wl-copy"}, []string{"wl-paste"}, nil + } + return nil, nil, stock.NewBasicError("unable to get clipboard command(s)") +} + // CopyToClipboard will copy to clipboard, if non-empty will clear later. func CopyToClipboard(value string) { - pipeTo("pbcopy", value, true) + cp, _, err := GetClipboardCommand() + if err != nil { + fmt.Printf("unable to copy to clipboard: %v\n", err) + return + } + var args []string + if len(cp) > 1 { + args = cp[1:] + } + pipeTo(cp[0], value, true, args...) if value != "" { fmt.Printf("clipboard will clear in %d seconds\n", MaxClipTime) pipeTo("lb", value, false, "clear") @@ -43,4 +90,5 @@ func pipeTo(command, value string, wait bool, args ...string) { if ran != nil { stock.Die("failed to run command", ran) } + }