summaryrefslogtreecommitdiff
path: root/client/clipboard
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2025-06-05 14:56:22 -0500
committercitrons <citrons@mondecitronne.com>2025-06-07 16:02:18 -0500
commitcf78bc0515fa38a6cc570afd7a7e0000bdcda09e (patch)
tree6a72a3ef0f67dde072d0a9010851ee06441041dc /client/clipboard
parent8352123b5c2c479cea31c991eeebf7868559db29 (diff)
clipboard support
Diffstat (limited to 'client/clipboard')
-rw-r--r--client/clipboard/clipboard.go19
-rw-r--r--client/clipboard/shell_clipboard.go78
2 files changed, 91 insertions, 6 deletions
diff --git a/client/clipboard/clipboard.go b/client/clipboard/clipboard.go
index b110378..b81b500 100644
--- a/client/clipboard/clipboard.go
+++ b/client/clipboard/clipboard.go
@@ -1,4 +1,4 @@
-package tui
+package clipboard
import (
"sync"
@@ -12,23 +12,30 @@ type Clipboard interface {
type virtualClipboard string
func (clip *virtualClipboard) Copy(text string) {
- *clip = text
+ *clip = virtualClipboard(text)
}
var ch = make(chan string)
func (clip *virtualClipboard) Paste() <-chan string {
go func() {
- ch <- *clip
+ ch <- string(*clip)
}()
return ch
}
-var clipboard Clipboard = virtualClipboard {}
-var mut RWMutex
+var defaultClipboard virtualClipboard
+var clipboard Clipboard = &defaultClipboard
+var mut sync.RWMutex
-func Get() {
+func Get() Clipboard {
mut.RLock()
c := clipboard
mut.RUnlock()
return c
}
+
+func Set(c Clipboard) {
+ mut.Lock()
+ clipboard = c
+ mut.Unlock()
+}
diff --git a/client/clipboard/shell_clipboard.go b/client/clipboard/shell_clipboard.go
new file mode 100644
index 0000000..9d8ecc8
--- /dev/null
+++ b/client/clipboard/shell_clipboard.go
@@ -0,0 +1,78 @@
+package clipboard
+
+import (
+ "os/exec"
+ "strings"
+ "bytes"
+)
+
+type ShellClipboard struct {
+ copyCommand string
+ pasteCommand string
+ testCommand string
+}
+
+var try = []ShellClipboard {
+ ShellClipboard {"wl-copy", "wl-paste -n", "wl-copy -h"},
+ ShellClipboard {
+ "xclip -selection clipboard", "xclip -selection clipboard -o",
+ "xclip -h",
+ },
+ ShellClipboard {"pbcopy", "pbpaste", "pbcopy -help"},
+}
+
+func command(command string) *exec.Cmd {
+ args := strings.Split(command, " ")
+ return exec.Command(args[0], args[1:]...)
+}
+
+func (c ShellClipboard) Test() bool {
+ cmd := command(c.testCommand)
+ return cmd.Run() == nil
+}
+
+func (c ShellClipboard) Copy(text string) {
+ cmd := command(c.copyCommand)
+ pipe, err := cmd.StdinPipe()
+ cmd.Start()
+ go func() {
+ if err != nil {
+ return
+ }
+ defer pipe.Close()
+
+ buf := bytes.NewBuffer([]byte(text))
+ buf.WriteTo(pipe)
+ }()
+}
+
+func (c ShellClipboard) Paste() <-chan string {
+ cmd := command(c.pasteCommand)
+ pipe, err := cmd.StdoutPipe()
+ cmd.Start()
+ ch := make(chan string, 1)
+ go func() {
+ if err != nil {
+ return
+ }
+ defer pipe.Close()
+
+ buf := bytes.NewBuffer(nil)
+ _, err = buf.ReadFrom(pipe)
+ if err != nil {
+ return
+ }
+ ch <- buf.String()
+ close(ch)
+ }()
+ return ch
+}
+
+func DiscoverCommand() {
+ for _, c := range try {
+ if c.Test() {
+ Set(c)
+ break
+ }
+ }
+}