-
Notifications
You must be signed in to change notification settings - Fork 0
/
rofi.go
84 lines (66 loc) · 1.48 KB
/
rofi.go
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
package main
import (
"context"
"fmt"
"os"
"os/exec"
)
func RunRofi(ctx context.Context, additionalEnvironment ...string) error {
program, err := os.Executable()
if err != nil {
return fmt.Errorf("find executable: %w", err)
}
cmd := exec.CommandContext(ctx,
"rofi", "-modi", "swytch:"+program, "-show", "swytch",
"-kb-accept-alt", "", // disable alternative accept
"-kb-custom-1", "Shift+Return", // set custom keybinding 1 to shift+return
"-kb-custom-2", "Control+c", // kill window
)
cmd.Env = append(os.Environ(), additionalEnvironment...)
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return fmt.Errorf("run rofi: %w", err)
}
return nil
}
type DisplayOptions struct {
Prompt string
Message string
MarkupRows bool
NoCustom bool
UseHotKeys bool
}
func formatOpt(name, value string) string {
return "\x00" + name + "\x1f" + value + "\n"
}
func (opts DisplayOptions) ConfigString() string {
res := ""
if opts.Prompt != "" {
res += formatOpt("prompt", opts.Prompt)
}
if opts.Message != "" {
res += formatOpt("message", opts.Message)
}
if opts.MarkupRows {
res += formatOpt("markup-rows", "true")
}
if opts.NoCustom {
res += formatOpt("no-custom", "true")
}
if opts.UseHotKeys {
res += formatOpt("use-hot-keys", "true")
}
return res
}
type Row struct {
Text string
Info string
}
func (r Row) ConfigString() string {
extra := ""
if r.Info != "" {
extra += "\x00" + "info" + "\x1f" + r.Info
}
return r.Text + extra + "\n"
}