-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (121 loc) · 3.31 KB
/
main.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
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
package main
import (
"context"
"encoding/json"
"fmt"
"html"
"os"
"strconv"
"github.com/joshuarubin/go-sway"
"github.com/spf13/pflag"
)
type Options struct {
WorkspaceColors []string
Debug bool
}
func main() {
var opts Options
flags := pflag.NewFlagSet("swytch", pflag.ExitOnError)
flags.StringSliceVar(&opts.WorkspaceColors, "workspace-colors", []string{"lightblue", "lightgreen", "orange", "red", "magenta", "cyan"}, "Use workspace `color`")
flags.BoolVar(&opts.Debug, "debug", false, "Print debug messages")
err := flags.Parse(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "parse flags: %v\n", err)
os.Exit(10)
}
// get opts from environment if passed from there, overrides cli flags
if v, ok := os.LookupEnv("ROFI_WINDOW_ACTION_OPTS"); ok {
err = json.Unmarshal([]byte(v), &opts)
if err != nil {
fmt.Fprintf(os.Stderr, "json decode opts from $ROFI_WINDOW_ACTION_OPTS: %v\n", err)
os.Exit(12)
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = run(ctx, opts, flags.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(11)
}
}
func run(ctx context.Context, opts Options, args []string) error {
if _, ok := os.LookupEnv("ROFI_RETV"); !ok {
// just run rofi with the right parameters, pass all opts as json in environment
buf, err := json.Marshal(opts)
if err != nil {
return fmt.Errorf("json encode: %w", err)
}
return RunRofi(ctx, "ROFI_WINDOW_ACTION_OPTS="+string(buf))
}
retv, err := strconv.Atoi(os.Getenv("ROFI_RETV"))
if err != nil {
return fmt.Errorf("invalid value passed in ROFI_RETV: %w", err)
}
info := os.Getenv("ROFI_INFO")
// called by rofi
if info != "" {
// item selected
if opts.Debug {
fmt.Fprintf(os.Stderr, "selected item %v (%v) by rofi: %v\n", info, retv, args[1])
}
switch retv {
case 1:
if opts.Debug {
fmt.Fprintf(os.Stderr, "focus window %v\n", info)
}
return FocusWindow(info)
case 10:
if opts.Debug {
fmt.Fprintf(os.Stderr, "move window %v to current workspace\n", info)
}
return MoveWindowToCurrentWorkspace(info)
case 11:
if opts.Debug {
fmt.Fprintf(os.Stderr, "kill window %v\n", info)
}
return KillWindow(info)
default:
return fmt.Errorf("unknown keyboard shortcut %v received from rofi via $ROFI_RETV", retv)
}
}
// build menu
client, err := sway.New(ctx)
if err != nil {
return fmt.Errorf("connect window manager: %w", err)
}
windows, err := getAllWindows(ctx, client)
if err != nil {
return fmt.Errorf("get windows: %w", err)
}
// configure rofi
dispOpts := DisplayOptions{
Prompt: "window",
NoCustom: true,
UseHotKeys: true,
MarkupRows: true,
}
fmt.Print(dispOpts.ConfigString())
workspace := ""
color := ""
colorIndex := 0
for _, w := range windows {
if w.Workspace != workspace {
color = opts.WorkspaceColors[colorIndex%len(opts.WorkspaceColors)]
colorIndex++
workspace = w.Workspace
}
active := ""
if w.Active {
active = ` font_weight="bold"`
}
text := fmt.Sprintf("<span foreground=%q>[%s]</span>", color, html.EscapeString(w.Workspace))
text += fmt.Sprintf("\t<span%s>%s\t%s</span>", active, html.EscapeString(w.Program), html.EscapeString(w.Title))
row := Row{
Text: text,
Info: fmt.Sprintf("%d", w.ID),
}
fmt.Print(row.ConfigString())
}
return nil
}