| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package clipboard |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"context" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"os" |
| 16 |
"os/exec" |
| 17 |
"sync" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
xsel = "xsel" |
| 22 |
xclip = "xclip" |
| 23 |
powershellExe = "powershell.exe" |
| 24 |
clipExe = "clip.exe" |
| 25 |
wlcopy = "wl-copy" |
| 26 |
wlpaste = "wl-paste" |
| 27 |
termuxClipboardGet = "termux-clipboard-get" |
| 28 |
termuxClipboardSet = "termux-clipboard-set" |
| 29 |
) |
| 30 |
|
| 31 |
var ( |
| 32 |
wrap *wrapper |
| 33 |
once sync.Once |
| 34 |
|
| 35 |
xselPasteArgs = []string{xsel, "--output", "--clipboard"} |
| 36 |
xselCopyArgs = []string{xsel, "--input", "--clipboard"} |
| 37 |
|
| 38 |
xclipPasteArgs = []string{xclip, "-out", "-selection", "clipboard"} |
| 39 |
xclipCopyArgs = []string{xclip, "-in", "-selection", "clipboard"} |
| 40 |
|
| 41 |
powershellExePasteArgs = []string{powershellExe, "Get-Clipboard"} |
| 42 |
clipExeCopyArgs = []string{clipExe} |
| 43 |
|
| 44 |
wlpasteArgs = []string{wlpaste, "--no-newline"} |
| 45 |
wlcopyArgs = []string{wlcopy} |
| 46 |
|
| 47 |
termuxPasteArgs = []string{termuxClipboardGet} |
| 48 |
termuxCopyArgs = []string{termuxClipboardSet} |
| 49 |
|
| 50 |
errMissingCommands = errors.New("no clipboard utilities available. Please install xsel, xclip, wl-clipboard or Termux:API add-on for termux-clipboard-get/set") |
| 51 |
) |
| 52 |
|
| 53 |
type wrapper struct { |
| 54 |
trimDOS bool |
| 55 |
|
| 56 |
pasteCmdArgs []string |
| 57 |
copyCmdArgs []string |
| 58 |
copySecretArgs []string |
| 59 |
|
| 60 |
|
| 61 |
supported bool |
| 62 |
} |
| 63 |
|
| 64 |
func (w *wrapper) unsupported() bool { |
| 65 |
return !w.supported |
| 66 |
} |
| 67 |
|
| 68 |
func hasBinary(name string) bool { |
| 69 |
_, err := exec.LookPath(name) |
| 70 |
|
| 71 |
return err == nil |
| 72 |
} |
| 73 |
|
| 74 |
func newWrapper() *wrapper { |
| 75 |
w := &wrapper{} |
| 76 |
|
| 77 |
switch { |
| 78 |
case os.Getenv("WAYLAND_DISPLAY") != "" && hasBinary(wlcopy) && hasBinary(wlpaste): |
| 79 |
|
| 80 |
w.pasteCmdArgs = wlpasteArgs |
| 81 |
w.copyCmdArgs = wlcopyArgs |
| 82 |
w.copySecretArgs = append(wlcopyArgs, "--type", "x-kde-passwordManagerHint/secret") |
| 83 |
w.supported = true |
| 84 |
case hasBinary(xclip): |
| 85 |
|
| 86 |
w.pasteCmdArgs = xclipPasteArgs |
| 87 |
w.copyCmdArgs = xclipCopyArgs |
| 88 |
w.supported = true |
| 89 |
case hasBinary(xsel): |
| 90 |
|
| 91 |
w.pasteCmdArgs = xselPasteArgs |
| 92 |
w.copyCmdArgs = xselCopyArgs |
| 93 |
w.supported = true |
| 94 |
case hasBinary(termuxClipboardSet) && hasBinary(termuxClipboardGet): |
| 95 |
|
| 96 |
w.pasteCmdArgs = termuxPasteArgs |
| 97 |
w.copyCmdArgs = termuxCopyArgs |
| 98 |
w.supported = true |
| 99 |
case hasBinary(clipExe) && hasBinary(powershellExe): |
| 100 |
|
| 101 |
w.pasteCmdArgs = powershellExePasteArgs |
| 102 |
w.copyCmdArgs = clipExeCopyArgs |
| 103 |
w.trimDOS = true |
| 104 |
w.supported = true |
| 105 |
default: |
| 106 |
|
| 107 |
w.supported = false |
| 108 |
} |
| 109 |
|
| 110 |
return w |
| 111 |
} |
| 112 |
|
| 113 |
func getWrapper() *wrapper { |
| 114 |
once.Do(func() { |
| 115 |
wrap = newWrapper() |
| 116 |
}) |
| 117 |
|
| 118 |
return wrap |
| 119 |
} |
| 120 |
|
| 121 |
func init() { |
| 122 |
} |
| 123 |
|
| 124 |
func readAll(ctx context.Context) ([]byte, error) { |
| 125 |
w := getWrapper() |
| 126 |
if w.unsupported() || ForceUnsupported { |
| 127 |
return nil, errMissingCommands |
| 128 |
} |
| 129 |
|
| 130 |
pasteCmd := exec.CommandContext(ctx, w.pasteCmdArgs[0], w.pasteCmdArgs[1:]...) |
| 131 |
|
| 132 |
eOut := &bytes.Buffer{} |
| 133 |
pasteCmd.Stderr = eOut |
| 134 |
|
| 135 |
out, err := pasteCmd.Output() |
| 136 |
if err != nil { |
| 137 |
return nil, fmt.Errorf("failed to run command: %w. Output: %s", err, eOut.String()) |
| 138 |
} |
| 139 |
result := out |
| 140 |
if w.trimDOS && len(result) > 1 { |
| 141 |
result = result[:len(result)-2] |
| 142 |
} |
| 143 |
|
| 144 |
return result, nil |
| 145 |
} |
| 146 |
|
| 147 |
func writeAll(ctx context.Context, text []byte, secret bool) error { |
| 148 |
w := getWrapper() |
| 149 |
if w.unsupported() || ForceUnsupported { |
| 150 |
return errMissingCommands |
| 151 |
} |
| 152 |
|
| 153 |
copyCmd := exec.CommandContext(ctx, w.copyCmdArgs[0], w.copyCmdArgs[1:]...) |
| 154 |
if secret && len(w.copySecretArgs) > 0 { |
| 155 |
copyCmd = exec.CommandContext(ctx, w.copySecretArgs[0], w.copySecretArgs[1:]...) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
in, err := copyCmd.StdinPipe() |
| 161 |
if err != nil { |
| 162 |
return fmt.Errorf("failed to get stdin pipe: %w", err) |
| 163 |
} |
| 164 |
if err := copyCmd.Start(); err != nil { |
| 165 |
return fmt.Errorf("failed to start command: %w", err) |
| 166 |
} |
| 167 |
if _, err := in.Write(text); err != nil { |
| 168 |
return fmt.Errorf("failed to write to stdin: %w", err) |
| 169 |
} |
| 170 |
if err := in.Close(); err != nil { |
| 171 |
return fmt.Errorf("failed to close stdin: %w", err) |
| 172 |
} |
| 173 |
if err := copyCmd.Wait(); err != nil { |
| 174 |
return fmt.Errorf("failed to wait for command: %w", err) |
| 175 |
} |
| 176 |
|
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
func unsupported() bool { |
| 181 |
w := getWrapper() |
| 182 |
|
| 183 |
return w.unsupported() |
| 184 |
} |