-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaste.go
More file actions
176 lines (157 loc) · 3.91 KB
/
Copy pathpaste.go
File metadata and controls
176 lines (157 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
type SourceWindow struct {
Address string
Class string
}
func detectCompositor() string {
if os.Getenv("XDG_CURRENT_DESKTOP") == "niri" {
return "niri"
}
if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") != "" {
return "hyprland"
}
return ""
}
func captureSourceWindow() SourceWindow {
switch detectCompositor() {
case "niri":
return captureSourceWindowNiri()
case "hyprland":
return captureSourceWindowHyprland()
}
return SourceWindow{}
}
func captureSourceWindowHyprland() SourceWindow {
out, err := exec.Command("hyprctl", "clients", "-j").Output()
if err != nil {
return SourceWindow{}
}
var clients []struct {
Address string `json:"address"`
Class string `json:"class"`
FocusHistoryID int `json:"focusHistoryID"`
}
if err := json.Unmarshal(out, &clients); err != nil {
return SourceWindow{}
}
best := -1
for i, c := range clients {
if strings.ToLower(c.Class) == "yoink" {
continue
}
if best == -1 || c.FocusHistoryID < clients[best].FocusHistoryID {
best = i
}
}
if best == -1 {
return SourceWindow{}
}
return SourceWindow{
Address: clients[best].Address,
Class: strings.ToLower(clients[best].Class),
}
}
func captureSourceWindowNiri() SourceWindow {
out, err := exec.Command("niri", "msg", "-j", "focused-window").Output()
if err != nil {
return SourceWindow{}
}
var data struct {
ID int `json:"id"`
AppID string `json:"app_id"`
}
if err := json.Unmarshal(out, &data); err != nil {
return SourceWindow{}
}
return SourceWindow{
Address: strconv.Itoa(data.ID),
Class: strings.ToLower(data.AppID),
}
}
func pasteToWindowDirect(sw SourceWindow) {
if sw.Address == "" {
return
}
script := buildPasteScript(sw)
c := exec.Command("bash", "-c", script)
c.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
_ = c.Start()
}
func pasteToWindow(sw SourceWindow, rawLine string) {
// Decode and copy to clipboard before exiting
data, err := cliphistDecode(rawLine)
if err != nil {
return
}
cmd := exec.Command("wl-copy")
cmd.Stdin = bytes.NewReader(data)
if err := cmd.Run(); err != nil {
return
}
// Only paste-back if we have a valid source window to target
if sw.Address == "" {
return
}
// Fork detached process that waits for our kitty window to close,
// then focuses source and pastes
script := buildPasteScript(sw)
c := exec.Command("bash", "-c", script)
c.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
_ = c.Start()
}
func buildPasteScript(sw SourceWindow) string {
var pasteCmd string
switch {
case strings.Contains(sw.Class, "brave"),
strings.Contains(sw.Class, "firefox"),
strings.Contains(sw.Class, "chromium"):
pasteCmd = "wtype -s 20 -M ctrl -k v -m ctrl"
case strings.Contains(sw.Class, "kitty"),
strings.Contains(sw.Class, "alacritty"),
strings.Contains(sw.Class, "ghostty"),
strings.Contains(sw.Class, "wezterm"),
strings.Contains(sw.Class, "foot"):
pasteCmd = "wtype -s 20 -M ctrl -M shift -k v -m shift -m ctrl"
default:
pasteCmd = "wtype -s 20 -M shift -k Insert -m shift"
}
switch detectCompositor() {
case "niri":
return buildNiriPasteScript(sw, pasteCmd)
default:
return buildHyprlandPasteScript(sw, pasteCmd)
}
}
func buildNiriPasteScript(sw SourceWindow, pasteCmd string) string {
return `
for i in $(seq 1 25); do
if ! niri msg -j windows 2>/dev/null | jq -e '.[] | select(.app_id == "yoink")' >/dev/null 2>&1; then
break
fi
sleep 0.02
done
niri msg action focus-window --id ` + sw.Address + ` >/dev/null 2>&1
sleep 0.02
` + pasteCmd
}
func buildHyprlandPasteScript(sw SourceWindow, pasteCmd string) string {
return `
for i in $(seq 1 25); do
if ! hyprctl clients -j 2>/dev/null | jq -e '.[] | select(.class == "yoink")' >/dev/null 2>&1; then
break
fi
sleep 0.02
done
hyprctl dispatch focuswindow 'address:` + sw.Address + `' >/dev/null 2>&1
sleep 0.02
` + pasteCmd
}