-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
widget_command.go
83 lines (71 loc) · 1.76 KB
/
widget_command.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
package main
import (
"image"
"image/color"
"os/exec"
"strings"
"time"
)
// CommandWidget is a widget displaying the output of command(s).
type CommandWidget struct {
*BaseWidget
commands []string
fonts []string
frames []image.Rectangle
colors []color.Color
}
// NewCommandWidget returns a new CommandWidget.
func NewCommandWidget(bw *BaseWidget, opts WidgetConfig) *CommandWidget {
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second)
var commands, fonts, frameReps []string
_ = ConfigValue(opts.Config["command"], &commands)
_ = ConfigValue(opts.Config["font"], &fonts)
_ = ConfigValue(opts.Config["layout"], &frameReps)
var colors []color.Color
_ = ConfigValue(opts.Config["color"], &colors)
layout := NewLayout(int(bw.dev.Pixels))
frames := layout.FormatLayout(frameReps, len(commands))
for i := 0; i < len(commands); i++ {
if len(fonts) < i+1 {
fonts = append(fonts, "regular")
}
if len(colors) < i+1 {
colors = append(colors, DefaultColor)
}
}
return &CommandWidget{
BaseWidget: bw,
commands: commands,
fonts: fonts,
frames: frames,
colors: colors,
}
}
// Update renders the widget.
func (w *CommandWidget) Update() error {
size := int(w.dev.Pixels)
img := image.NewRGBA(image.Rect(0, 0, size, size))
for i := 0; i < len(w.commands); i++ {
str, err := runCommand(w.commands[i])
if err != nil {
return err
}
font := fontByName(w.fonts[i])
drawString(img,
w.frames[i],
font,
str,
w.dev.DPI,
-1,
w.colors[i],
image.Pt(-1, -1))
}
return w.render(w.dev, img)
}
func runCommand(command string) (string, error) {
output, err := exec.Command("sh", "-c", command).Output()
if err != nil {
return "", err
}
return strings.TrimSuffix(string(output), "\n"), nil
}