init commit
This commit is contained in:
13
internal/clipboard/clipboard.go
Normal file
13
internal/clipboard/clipboard.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package clipboard
|
||||
|
||||
import "context"
|
||||
|
||||
// Clipboard provides an interface for clipboard operations
|
||||
type Clipboard interface {
|
||||
// Read returns the current clipboard content
|
||||
Read() (string, error)
|
||||
// Write sets the clipboard content
|
||||
Write(content string) error
|
||||
// Watch returns a channel that emits clipboard changes
|
||||
Watch(ctx context.Context) <-chan string
|
||||
}
|
||||
39
internal/clipboard/detector.go
Normal file
39
internal/clipboard/detector.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package clipboard
|
||||
|
||||
import "os"
|
||||
|
||||
type DisplayServer int
|
||||
|
||||
const (
|
||||
DisplayServerUnknown DisplayServer = iota
|
||||
DisplayServerX11
|
||||
DisplayServerWayland
|
||||
)
|
||||
|
||||
// Detect returns the current display server type
|
||||
func Detect() DisplayServer {
|
||||
// Check for Wayland first
|
||||
if os.Getenv("WAYLAND_DISPLAY") != "" {
|
||||
return DisplayServerWayland
|
||||
}
|
||||
|
||||
// Check for X11
|
||||
if os.Getenv("DISPLAY") != "" {
|
||||
return DisplayServerX11
|
||||
}
|
||||
|
||||
return DisplayServerUnknown
|
||||
}
|
||||
|
||||
// New creates a clipboard implementation for the current display server
|
||||
func New() (Clipboard, error) {
|
||||
switch Detect() {
|
||||
case DisplayServerWayland:
|
||||
return NewWayland()
|
||||
case DisplayServerX11:
|
||||
return NewX11()
|
||||
default:
|
||||
// Try X11 as fallback
|
||||
return NewX11()
|
||||
}
|
||||
}
|
||||
77
internal/clipboard/wayland.go
Normal file
77
internal/clipboard/wayland.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WaylandClipboard struct{}
|
||||
|
||||
func NewWayland() (*WaylandClipboard, error) {
|
||||
// Check if wl-paste is available
|
||||
if _, err := exec.LookPath("wl-paste"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := exec.LookPath("wl-copy"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &WaylandClipboard{}, nil
|
||||
}
|
||||
|
||||
func (c *WaylandClipboard) Read() (string, error) {
|
||||
cmd := exec.Command("wl-paste", "--no-newline")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
// wl-paste returns error if clipboard is empty
|
||||
return "", nil
|
||||
}
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
func (c *WaylandClipboard) Write(content string) error {
|
||||
cmd := exec.Command("wl-copy")
|
||||
cmd.Stdin = strings.NewReader(content)
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func (c *WaylandClipboard) Watch(ctx context.Context) <-chan string {
|
||||
out := make(chan string)
|
||||
|
||||
go func() {
|
||||
defer close(out)
|
||||
|
||||
// Use wl-paste --watch to detect clipboard changes
|
||||
// When clipboard changes, it runs "echo x" which outputs a single line
|
||||
// Then we read the full clipboard content separately
|
||||
cmd := exec.CommandContext(ctx, "wl-paste", "--watch", "echo", "x")
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
cmd.Process.Kill()
|
||||
return
|
||||
default:
|
||||
// Clipboard changed, read full content
|
||||
content, err := c.Read()
|
||||
if err == nil && content != "" {
|
||||
out <- content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmd.Wait()
|
||||
}()
|
||||
|
||||
return out
|
||||
}
|
||||
51
internal/clipboard/x11.go
Normal file
51
internal/clipboard/x11.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package clipboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.design/x/clipboard"
|
||||
)
|
||||
|
||||
type X11Clipboard struct{}
|
||||
|
||||
func NewX11() (*X11Clipboard, error) {
|
||||
if err := clipboard.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &X11Clipboard{}, nil
|
||||
}
|
||||
|
||||
func (c *X11Clipboard) Read() (string, error) {
|
||||
return string(clipboard.Read(clipboard.FmtText)), nil
|
||||
}
|
||||
|
||||
func (c *X11Clipboard) Write(content string) error {
|
||||
clipboard.Write(clipboard.FmtText, []byte(content))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *X11Clipboard) Watch(ctx context.Context) <-chan string {
|
||||
out := make(chan string)
|
||||
|
||||
go func() {
|
||||
defer close(out)
|
||||
|
||||
// golang.design/x/clipboard provides a Watch function
|
||||
ch := clipboard.Watch(ctx, clipboard.FmtText)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case data, ok := <-ch:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if len(data) > 0 {
|
||||
out <- string(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user