Parcourir la source

fix(tui): don't panic on missing linux clipboard tool

adamdottv il y a 7 mois
Parent
commit
37a86439c4
2 fichiers modifiés avec 21 ajouts et 0 suppressions
  1. 21 0
      packages/tui/internal/clipboard/clipboard_linux.go
  2. BIN
      packages/tui/opencode

+ 21 - 0
packages/tui/internal/clipboard/clipboard_linux.go

@@ -12,6 +12,7 @@ import (
 	"bytes"
 	"bytes"
 	"context"
 	"context"
 	"fmt"
 	"fmt"
+	"log/slog"
 	"os/exec"
 	"os/exec"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
@@ -72,11 +73,13 @@ func initialize() error {
 			clipboardTools[i].available = true
 			clipboardTools[i].available = true
 			if selectedTool < 0 {
 			if selectedTool < 0 {
 				selectedTool = i
 				selectedTool = i
+				slog.Debug("Clipboard tool found", "tool", tool.name)
 			}
 			}
 		}
 		}
 	}
 	}
 
 
 	if selectedTool < 0 {
 	if selectedTool < 0 {
+		slog.Warn("No clipboard utility found on system. Copy/paste functionality will be disabled.")
 		return fmt.Errorf(`%w: No clipboard utility found. Install one of the following:
 		return fmt.Errorf(`%w: No clipboard utility found. Install one of the following:
 
 
 For X11 systems:
 For X11 systems:
@@ -98,6 +101,12 @@ If running in a headless environment, you may also need:
 }
 }
 
 
 func read(t Format) (buf []byte, err error) {
 func read(t Format) (buf []byte, err error) {
+	// Ensure clipboard is initialized before attempting to read
+	if err := initialize(); err != nil {
+		slog.Debug("Clipboard read failed: not initialized", "error", err)
+		return nil, err
+	}
+
 	toolMutex.Lock()
 	toolMutex.Lock()
 	tool := clipboardTools[selectedTool]
 	tool := clipboardTools[selectedTool]
 	toolMutex.Unlock()
 	toolMutex.Unlock()
@@ -167,6 +176,11 @@ func readImage(tool struct {
 }
 }
 
 
 func write(t Format, buf []byte) (<-chan struct{}, error) {
 func write(t Format, buf []byte) (<-chan struct{}, error) {
+	// Ensure clipboard is initialized before attempting to write
+	if err := initialize(); err != nil {
+		return nil, err
+	}
+
 	toolMutex.Lock()
 	toolMutex.Lock()
 	tool := clipboardTools[selectedTool]
 	tool := clipboardTools[selectedTool]
 	toolMutex.Unlock()
 	toolMutex.Unlock()
@@ -230,6 +244,13 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
 
 
 func watch(ctx context.Context, t Format) <-chan []byte {
 func watch(ctx context.Context, t Format) <-chan []byte {
 	recv := make(chan []byte, 1)
 	recv := make(chan []byte, 1)
+
+	// Ensure clipboard is initialized before starting watch
+	if err := initialize(); err != nil {
+		close(recv)
+		return recv
+	}
+
 	ti := time.NewTicker(time.Second)
 	ti := time.NewTicker(time.Second)
 
 
 	// Get initial clipboard content
 	// Get initial clipboard content

BIN
packages/tui/opencode