-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitbar.go
444 lines (397 loc) · 11.8 KB
/
bitbar.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright (c) John McCabe 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// Package bitbar simplifies the creation of Bitbar plugins.
//
// Provides helper functions for adding lines and submenus along with setting
// command, style options etc using function chaining.
//
// See the BitBar project for more info - https://github.com/matryer/bitbar
package bitbar
import (
"fmt"
"image"
"strings"
)
// Plugin holds the content of the Bitbar plugin, lines and submenus.
type Plugin struct {
StatusBar StatusBar
SubMenu *SubMenu
}
// Line holds the content, styling and behaviour of a line in a Bitbar
// menu, both in the menu and submenus
type Line struct {
text string
href string
color string
font string
size int
terminal *bool
refresh *bool
dropDown *bool
length int
trim *bool
alternate *bool
emojize *bool
ansi *bool
bash string
params []string
templateImage string
image string
hr bool
}
// Style wraps options related to text presentation which can be added to a line
// using the *line.Style(s Style) function.
type Style struct {
Color string
Font string
Size int
Length int
Trim *bool
Emojize *bool
Ansi *bool
}
// Cmd wraps options related to commands which can be added to a line using the
// *line.Command(c Cmd) function.
type Cmd struct {
Bash string
Params []string
Terminal *bool
Refresh *bool
}
// StatusBar holds one of more Lines of text which are rendered in the status bar.
// Multiple Lines will be cycled through over and over
type StatusBar struct {
Lines []*Line
}
// SubMenuItem is used to hold a Line or SubMenu.
type SubMenuItem interface{}
// SubMenu contains a slice of SubMenuItems which can be Lines or additional
// SubMenus. The Level indicates how nested the submenu is which is used during
// render to prepend the correct number of `--` prefixes.
type SubMenu struct {
Level int
Lines []SubMenuItem
}
// New returns an empty Bitbar menu without any context
func New() Plugin {
return Plugin{}
}
// StatusLine creates a line adding text to the status bar which will be added
// before the main dropdown delimiter (`---`), multiple StatusLines will be
// cycled through over and over.
// *menu.StatusLine("Text for the status bar")
func (p *Plugin) StatusLine(s string) *Line {
l := new(Line)
l.text = s
p.StatusBar.Lines = append(p.StatusBar.Lines, l)
return l
}
// NewSubMenu creates a submenu off the main menu.
// *menu.NewSubMenu()
func (p *Plugin) NewSubMenu() *SubMenu {
p.SubMenu = new(SubMenu)
p.SubMenu.Level = 0
return p.SubMenu
}
// Line creates a line adding text to the dropdown which will be added after
// the main dropdown delimiter (`---`).
// submenu.Line("Submenu item text")
func (d *SubMenu) Line(s string) *Line {
l := new(Line)
l.text = s
d.Lines = append(d.Lines, l)
return l
}
// Image adds a line with an image to the dropdown which will be added after
// the main dropdown delimiter (`---`). Use a 144 DPI resolution to support
// Retina displays.
// line.Image(myImg)
func (d *SubMenu) Image(img image.Image) *Line {
return d.Line("").Image(img)
}
// HR turns a line into a horizontal delimiter, useful for breaking menu items
// into logical groups.
// submenu.Line("").HR()
func (d *SubMenu) HR() *Line {
l := new(Line)
l.hr = true
l.text = "---"
d.Lines = append(d.Lines, l)
return l
}
// NewSubMenu creates a nested submenu off a submenu.
// submenu.NewSubMenu()
func (d *SubMenu) NewSubMenu() *SubMenu {
newSubMenu := new(SubMenu)
newSubMenu.Level = d.Level + 1
d.Lines = append(d.Lines, newSubMenu)
return newSubMenu
}
// Style provides a alternate method for setting the text style related
// options.
// style := bitbar.Style{
// Color: "red",
// Font: "UbuntuMono-Bold",
// Size: 14,
// Length: 20,
// Trim: false,
// Emojize: false,
// Ansi: false,
// }
// line.Style(false)
func (l *Line) Style(s Style) *Line {
l.color = s.Color
l.font = s.Font
l.size = s.Size
l.length = s.Length
l.trim = s.Trim
l.emojize = s.Emojize
l.ansi = s.Ansi
return l
}
// Command provides a alternate method for setting the bash script and
// params along with some related flags via a Command struct.
// cmd := bitbar.Cmd{
// Bash: "/Users/user/BitBar_Plugins/scripts/nginx.restart.sh",
// Params: []string{"--verbose"},
// Terminal: false,
// Refresh: true,
// }
// line.Command(cmd)
func (l *Line) Command(c Cmd) *Line {
l.bash = c.Bash
l.params = c.Params
l.terminal = c.Terminal
l.refresh = c.Refresh
return l
}
// Href adds a URL to the line and makes it clickable.
// line.Href("http://github.com/johnmccabe/bitbar")
func (l *Line) Href(s string) *Line {
l.href = s
return l
}
// Color sets the lines font color, can take a name or hex value.
// line.Color("red")
// line.Color("#ff0000")
func (l *Line) Color(s string) *Line {
l.color = s
return l
}
// Font sets the lines font.
// line.Font("UbuntuMono-Bold")
func (l *Line) Font(s string) *Line {
l.font = s
return l
}
// Size sets the lines font size.
// line.Size(12)
func (l *Line) Size(i int) *Line {
l.size = i
return l
}
// Bash makes makes the line clickable and adds a script that will be run
// on click.
// line.Bash("/Users/user/BitBar_Plugins/scripts/nginx.restart.sh")
func (l *Line) Bash(s string) *Line {
l.bash = s
return l
}
// Params adds arguments which are passed to the script specified by *Line.Bash().
// args := []string{"--verbose"}
// line.Bash("/Users/user/BitBar_Plugins/scripts/nginx.restart.sh").Params(args)
func (l *Line) Params(s []string) *Line {
l.params = s
return l
}
// Terminal sets a flag which controls whether a Terminal is opened when the bash
// script is run.
// line.Bash("/Users/user/BitBar_Plugins/scripts/nginx.restart.sh").Terminal(true)
func (l *Line) Terminal(b bool) *Line {
l.terminal = &b
return l
}
// Refresh controls whether clicking the line results in the plugin being refreshed.
// If the line has a bash script attached then the plugin is refreshed after the
// script finishes.
// line.Bash("/Users/user/BitBar_Plugins/scripts/nginx.restart.sh").Refresh()
// line.Refresh()
func (l *Line) Refresh() *Line {
refreshEnabled := true
l.refresh = &refreshEnabled
return l
}
// DropDown sets a flag which controls whether the line only appears and cycles in the
// status bar but not in the dropdown.
// line.DropDown(false)
func (l *Line) DropDown(b bool) *Line {
l.dropDown = &b
return l
}
// Length truncates the line after the specified number of characters. An elipsis will
// be added to any truncated strings, as well as a tooltip displaying the full string.
// line.DropDown(false)
func (l *Line) Length(i int) *Line {
l.length = i
return l
}
// Trim sets a flag to control whether leading/trailing whitespace is trimmed from the
// title. Defaults to `true`.
// line.Trim(false)
func (l *Line) Trim(b bool) *Line {
l.trim = &b
return l
}
// Alternate sets a flag to mark a line as an alternate to the previous one for when the
// Option key is pressed in the dropdown.
// line.Alternate(false)
func (l *Line) Alternate(b bool) *Line {
l.alternate = &b
return l
}
// TemplateImage sets an image for the line. The image data must be passed as base64
// encoded string and should consist of only black and clear pixels. The alpha channel
// in the image can be used to adjust the opacity of black content, however. This is the
// recommended way to set an image for the statusbar. Use a 144 DPI resolution to support
// Retina displays. The imageformat can be any of the formats supported by Mac OS X.
// line.TemplateImage("iVBORw0KGgoAAAANSUhEUgAAA...")
func (l *Line) TemplateImage(s string) *Line {
l.templateImage = s
return l
}
// Image set an image for the line. Use a 144 DPI resolution to support Retina displays.
// line.Image(myImg)
func (l *Line) Image(img image.Image) *Line {
l.image = toBase64(img)
return l
}
// Emojize sets a flag to control parsing of github style :mushroom: into 🍄.
// line.Emojize(false)
func (l *Line) Emojize(b bool) *Line {
l.emojize = &b
return l
}
// Ansi sets a flag to control parsing of ANSI codes.
// line.Ansi(false)
func (l *Line) Ansi(b bool) *Line {
l.ansi = &b
return l
}
// CopyToClipboard is a helper to copy the specified text to the OSX clipboard.
// line.CopyToClipboard("some text")
func (l *Line) CopyToClipboard(text string) *Line {
line := l.Bash("/bin/bash").
Params([]string{"-c", fmt.Sprintf("'echo -n %s | pbcopy'", text)}).
Terminal(false)
return line
}
// Render the Bitbar menu to Stdout.
func (p *Plugin) Render() {
var output string
for _, line := range p.StatusBar.Lines {
output = output + fmt.Sprintf("%s\n", renderLine(line))
}
output = output + "---\n"
if p.SubMenu != nil {
output = output + renderSubMenu(p.SubMenu)
}
fmt.Println(output)
}
func renderSubMenu(d *SubMenu) string {
var output string
var prefix string
if d.Level > 0 {
prefix = strings.Repeat("--", d.Level) + " "
}
for _, line := range d.Lines {
switch v := line.(type) {
case *Line:
if line.(*Line).hr {
output = output + fmt.Sprintf("%s%s\n", strings.TrimSpace(prefix), renderLine(v))
} else {
output = output + fmt.Sprintf("%s%s\n", prefix, renderLine(v))
}
case *SubMenu:
output = output + renderSubMenu(v)
}
}
return output
}
func renderLine(l *Line) string {
result := []string{l.text}
options := []string{"|"}
options = append(options, renderStyleOptions(l)...)
options = append(options, renderCommandOptions(l)...)
options = append(options, renderImageOptions(l)...)
options = append(options, renderMiscOptions(l)...)
if len(options) > 1 {
result = append(result, options...)
}
return strings.Join(result, " ")
}
func renderImageOptions(l *Line) []string {
imageOptions := []string{}
if len(l.templateImage) > 0 {
imageOptions = append(imageOptions, fmt.Sprintf("templateImage='%s'", l.templateImage))
}
if len(l.image) > 0 {
imageOptions = append(imageOptions, fmt.Sprintf("image='%s'", l.image))
}
return imageOptions
}
func renderMiscOptions(l *Line) []string {
miscOptions := []string{}
if l.href != "" {
miscOptions = append(miscOptions, fmt.Sprintf("href='%s'", l.href))
}
if l.dropDown != nil {
miscOptions = append(miscOptions, fmt.Sprintf("dropdown=%t", *l.dropDown))
}
if l.alternate != nil {
miscOptions = append(miscOptions, fmt.Sprintf("alternate='%t'", *l.alternate))
}
return miscOptions
}
func renderStyleOptions(l *Line) []string {
styleOptions := []string{}
if l.color != "" {
styleOptions = append(styleOptions, fmt.Sprintf("color=\"%s\"", l.color))
}
if l.font != "" {
styleOptions = append(styleOptions, fmt.Sprintf("font=\"%s\"", l.font))
}
if l.size > 0 {
styleOptions = append(styleOptions, fmt.Sprintf("size=%d", l.size))
}
if l.length > 0 {
styleOptions = append(styleOptions, fmt.Sprintf("length=%d", l.length))
}
if l.trim != nil {
styleOptions = append(styleOptions, fmt.Sprintf("trim=%t", *l.trim))
}
if l.emojize != nil {
styleOptions = append(styleOptions, fmt.Sprintf("emojize=%t", *l.emojize))
}
if l.ansi != nil {
styleOptions = append(styleOptions, fmt.Sprintf("ansi=%t", *l.ansi))
}
return styleOptions
}
func renderCommandOptions(l *Line) []string {
commandOptions := []string{}
if l.bash != "" {
commandOptions = append(commandOptions, fmt.Sprintf("bash=\"%s\"", l.bash))
}
if len(l.params) > 0 {
for i, param := range l.params {
commandOptions = append(commandOptions, fmt.Sprintf("param%d=%s", i+1, param))
}
}
if l.terminal != nil {
commandOptions = append(commandOptions, fmt.Sprintf("terminal=%t", *l.terminal))
}
if l.refresh != nil {
commandOptions = append(commandOptions, fmt.Sprintf("refresh=%t", *l.refresh))
}
return commandOptions
}