-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
225 lines (184 loc) · 4.71 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package rofi
import (
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type Option struct {
Label string
Info []string
Icon string
Value string
Cmds []string
IsMultiline bool
IsUrgent bool
IsHighlighted bool
UseMarkup bool
}
type Value struct {
Cmd string
Value string
}
type Model struct {
Message string
Overlay string
Prompt string
Input string
ActiveEntry int
Options []Option
}
type blockOption struct {
Text string `json:"text,omitempty"`
Icon string `json:"icon,omitempty"`
Data string `json:"data,omitempty"`
Urgent bool `json:"urgent,omitempty"`
Highlight bool `json:"highlight,omitempty"`
Markup bool `json:"markup,omitempty"`
}
type blockModel struct {
Message string `json:"message"`
Overlay string `json:"overlay"`
Prompt string `json:"prompt"`
Input string `json:"input"`
InputAction string `json:"input action,omitempty"`
EventFormat string `json:"event format,omitempty"`
ActiveEntry int `json:"active entry,omitempty"`
Lines []blockOption `json:"lines,omitempty"`
}
type eventName string
const (
eventNameSelectedEntry eventName = "SELECT_ENTRY"
eventNameCustomEntry eventName = "ACTIVE_ENTRY"
eventNameCustomEntryIndex eventName = "CUSTOM_KEY"
)
func (en eventName) IsValid() bool {
switch en {
case eventNameSelectedEntry, eventNameCustomEntry, eventNameCustomEntryIndex:
return true
}
return false
}
func (e eventName) String() string {
return string(e)
}
type event struct {
Name eventName `json:"name"`
Value string `json:"value"`
Index string `json:"index"`
}
func (e event) isValid() bool {
if !e.Name.IsValid() {
return false
}
if (e.Name == eventNameCustomEntry || e.Name == eventNameSelectedEntry) && e.Value == "" {
return false
}
i, _ := strconv.Atoi(e.Index)
if e.Name == eventNameCustomEntryIndex && i < 1 {
return false
}
return true
}
const eventFormat string = "{\"index\":\"{{value_escaped}}\",\"name\":\"{{name_enum}}\",\"value\":\"{{data}}\"}"
func NewRofiBlock() (Model, <-chan Value) {
ch := make(chan Value)
go broadcastEvents(ch)
return Model{}, ch
}
// Using spread to make passing selected index optional... Only cares about the first value
func (m *Model) Render(i ...int) {
data := blockModel{
Message: m.Message,
Overlay: m.Overlay,
Prompt: m.Prompt,
Input: m.Input,
EventFormat: eventFormat,
Lines: m.mapOptions(),
}
if len(i) > 0 {
data.ActiveEntry = i[0]
}
j, err := json.Marshal(data)
if err != nil {
log.Fatalf("rofi.Render: could not marshal: %s\n", err)
}
fmt.Println(string(j))
}
func (m *Model) mapOptions() []blockOption {
var bos []blockOption
for _, o := range m.Options {
if o.Label == "" {
continue
} else if len(o.Cmds) < 1 {
log.Println("Can't print options with no commands")
continue
}
label := o.Label
if len(o.Info) > 0 {
separator := " "
if o.IsMultiline {
separator = "\n"
}
label = fmt.Sprintf("%s%s%s", label, separator, strings.Join(o.Info, separator))
}
bos = append(bos, blockOption{
Icon: o.Icon,
Text: label,
Data: strings.Join(append([]string{o.Value}, o.Cmds...), "||"),
Markup: o.UseMarkup,
Urgent: o.IsUrgent,
Highlight: o.IsHighlighted,
})
}
return bos
}
func getValue(eventValue string, index int) Value {
val := Value{}
output := strings.Split(eventValue, "||")
if len(output) < 2 {
log.Fatalf("Invalid event value. Needs to have a value and at least one command: %s\n", eventValue)
}
val.Value = output[0]
cmds := output[1:]
if len(cmds) <= index {
log.Printf("Index %d did not result in a valid command. Selecting first command", index)
index = 0
}
val.Cmd = cmds[index]
return val
}
func broadcastEvents(ch chan<- Value) {
dec := json.NewDecoder(os.Stdin)
for {
index := 0
var ev event
// Waiting for events here
if err := dec.Decode(&ev); err != nil {
log.Fatalf("rofi.broadcastEvents: Could not decode event: %s\n", err)
}
if !ev.isValid() {
log.Printf("rofi.broadcastEvents: event was not valid: %#v\n", ev)
continue
}
// If it is a custom entry, a second event gets post right after
if ev.Name == eventNameCustomEntry {
var indexEv event
if err := dec.Decode(&indexEv); err != nil {
log.Fatalf("rofi.broadcastEvents: Could not decode event: %s\n", err)
}
if indexEv.Name != eventNameCustomEntryIndex && !indexEv.isValid() {
log.Printf("rofi.broadcastEvents: event with index was not valid: %#v\n", indexEv)
continue
}
var err error
index, err = strconv.Atoi(indexEv.Index)
if err != nil {
log.Printf("rofi.broadcastEvents: event with index could not be converted: %s\n", err)
}
}
ch <- getValue(ev.Value, index)
}
}