Skip to content

Commit 69571de

Browse files
zMoooooritzmuesli
authored andcommitted
Add command widget that displays the output of commands
1 parent d06d32a commit 69571de

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

widget.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func NewWidget(base string, kc KeyConfig, bg image.Image) (Widget, error) {
111111

112112
case "top":
113113
return NewTopWidget(*bw, kc.Widget), nil
114+
115+
case "command":
116+
return NewCommandWidget(*bw, kc.Widget)
114117
}
115118

116119
// unknown widget ID

widget_command.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"image"
6+
"os/exec"
7+
"strings"
8+
9+
"github.com/muesli/streamdeck"
10+
)
11+
12+
// CommandWidget is a widget displaying the output of command(s)
13+
type CommandWidget struct {
14+
BaseWidget
15+
command string
16+
font string
17+
}
18+
19+
func NewCommandWidget(bw BaseWidget, opts WidgetConfig) (*CommandWidget, error) {
20+
bw.setInterval(opts.Interval, 1000)
21+
22+
var command, font string
23+
ConfigValue(opts.Config["command"], &command)
24+
ConfigValue(opts.Config["font"], &font)
25+
26+
return &CommandWidget{
27+
BaseWidget: bw,
28+
command: command,
29+
font: font,
30+
}, nil
31+
}
32+
33+
// Update renders the widget
34+
func (w *CommandWidget) Update(dev *streamdeck.Device) error {
35+
size := int(dev.Pixels)
36+
margin := size / 18
37+
height := size - (margin * 2)
38+
img := image.NewRGBA(image.Rect(0, 0, size, size))
39+
40+
commands := strings.Split(w.command, ";")
41+
fonts := strings.Split(w.font, ";")
42+
43+
if len(commands) == 0 || len(w.command) == 0 {
44+
return fmt.Errorf("no command(s) supplied")
45+
}
46+
for len(fonts) < len(commands) {
47+
fonts = append(fonts, "regular")
48+
}
49+
50+
for i := 0; i < len(commands); i++ {
51+
str, err := runCommand(commands[i])
52+
if err != nil {
53+
return err
54+
}
55+
font := fontByName(fonts[i])
56+
lower := margin + (height/len(commands))*i
57+
upper := margin + (height/len(commands))*(i+1)
58+
59+
drawString(img, image.Rect(0, lower, size, upper),
60+
font,
61+
str,
62+
dev.DPI,
63+
-1,
64+
image.Pt(-1, -1))
65+
}
66+
67+
return w.render(dev, img)
68+
}
69+
70+
func runCommand(command string) (string, error) {
71+
output, err := exec.Command("sh", "-c", command).Output()
72+
if err != nil {
73+
return "", err
74+
}
75+
return strings.TrimSuffix(string(output), "\n"), nil
76+
}

0 commit comments

Comments
 (0)