Skip to content

Commit c6ce5a8

Browse files
committed
🐛 fix(clipboard): add WSL2 support and refactor to switch statement
- Add isWSL() detection for Windows Subsystem for Linux - Support clipboard copy on Ubuntu WSL2 via clip.exe - Refactor if-else chain to switch statement per gocritic lint - Check WSL_DISTRO_NAME env and /proc/version for WSL detection - Prioritize WSL → Wayland → xclip → xsel → fallback Fixes #8
1 parent 4098191 commit c6ce5a8

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

internal/sys/clipboard.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ func CopyToClipboard(ctx context.Context, text string) error {
2020
// Windows uses clip command
2121
cmd = exec.CommandContext(ctx, "clip")
2222
case "linux", "freebsd", "openbsd", "netbsd":
23-
// Smart logic for Linux clipboard
24-
if isWayland() {
23+
// Check for WSL first, then Wayland, then X11
24+
switch {
25+
case isWSL():
26+
// WSL uses Windows' clip.exe to access system clipboard
27+
cmd = exec.CommandContext(ctx, "clip.exe")
28+
case isWayland():
2529
// Prefer Wayland
2630
cmd = exec.CommandContext(ctx, "wl-copy")
27-
} else {
28-
// Fallback to X11, prefer xclip then xsel
29-
//nolint:gocritic // if-else is clearer than switch for command availability checks
30-
if isCommandAvailable("xclip") {
31-
cmd = exec.CommandContext(ctx, "xclip", "-selection", "clipboard")
32-
} else if isCommandAvailable("xsel") {
33-
cmd = exec.CommandContext(ctx, "xsel", "--clipboard", "--input")
34-
} else {
35-
// Last resort: try wl-copy (in case environment variables are incorrect)
36-
cmd = exec.CommandContext(ctx, "wl-copy")
37-
}
31+
case isCommandAvailable("xclip"):
32+
// Fallback to X11, prefer xclip
33+
cmd = exec.CommandContext(ctx, "xclip", "-selection", "clipboard")
34+
case isCommandAvailable("xsel"):
35+
// Then xsel
36+
cmd = exec.CommandContext(ctx, "xsel", "--clipboard", "--input")
37+
default:
38+
// Last resort: try wl-copy (in case environment variables are incorrect)
39+
cmd = exec.CommandContext(ctx, "wl-copy")
3840
}
3941
default:
4042
return nil // Unsupported OS
@@ -47,6 +49,25 @@ func CopyToClipboard(ctx context.Context, text string) error {
4749
return cmd.Run()
4850
}
4951

52+
// isWSL checks if the binary is running inside Windows Subsystem for Linux.
53+
func isWSL() bool {
54+
// Method 1: Check for WSL_DISTRO_NAME environment variable (common in newer WSL versions)
55+
if os.Getenv("WSL_DISTRO_NAME") != "" {
56+
return true
57+
}
58+
59+
// Method 2: Check /proc/version for "microsoft" string
60+
data, err := os.ReadFile("/proc/version")
61+
if err == nil {
62+
content := strings.ToLower(string(data))
63+
if strings.Contains(content, "microsoft") || strings.Contains(content, "wsl") {
64+
return true
65+
}
66+
}
67+
68+
return false
69+
}
70+
5071
// isWayland checks if user is running Wayland.
5172
func isWayland() bool {
5273
// Check common Wayland environment variables

0 commit comments

Comments
 (0)