-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (87 loc) · 1.59 KB
/
Copy pathmain.go
File metadata and controls
104 lines (87 loc) · 1.59 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
/*
Wrapper around `com` intended to be invoked from a tool like skhd
Usage of xcom:
Execute xcom from the shell
*/
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"github.com/dnjp/nyne"
)
func main() {
os.Unsetenv("winid") // do not trust the execution environment
winid, err := nyne.FocusedWinID(nyne.FocusedWinAddr())
if err != nil {
panic(err)
}
wins, err := nyne.Windows()
if err != nil {
panic(err)
}
w, ok := wins[winid]
if !ok {
panic(fmt.Errorf("could not find window with id %d", winid))
}
q0, q1, err := w.CurrentAddr()
if err != nil {
panic(err)
}
if q0 == q1 {
q1++
for {
err = w.SetAddr("#%d;#%d", q0, q1)
if err != nil {
panic(err)
}
dat, err := w.Data(q0, q1)
if err != nil {
panic(err)
}
if dat[len(dat)-1] == '\n' {
err = w.SetAddr("#%d;#%d", q0, q1)
if err != nil {
panic(err)
}
break
}
q1++
}
}
dat, err := w.Data(q0, q1)
if err != nil {
panic(err)
}
var in, out bytes.Buffer
_, err = in.Write(dat)
if err != nil {
panic(err)
}
com := exec.Command("com")
com.Stdin = &in
com.Stdout = &out
com.Env = os.Environ()
com.Env = append(com.Env, fmt.Sprintf("winid=%d", winid))
com.Env = append(com.Env, fmt.Sprintf("%%=%s", w.File))
com.Env = append(com.Env, fmt.Sprintf("samfile=%s", w.File))
err = com.Run()
if err != nil {
panic(err)
}
err = w.SetAddr("#%d;#%d", q0, q1)
if err != nil {
panic(err)
}
b := out.Bytes()
w.SetData(b)
err = w.SetAddr("#%d;#%d", q0, q0+len(b))
if err != nil {
panic(err)
}
err = w.SelectionFromAddr()
if err != nil {
panic(err)
}
}