-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command widget that displays the output of commands
- Loading branch information
1 parent
d06d32a
commit 69571de
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"image" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/muesli/streamdeck" | ||
) | ||
|
||
// CommandWidget is a widget displaying the output of command(s) | ||
type CommandWidget struct { | ||
BaseWidget | ||
command string | ||
font string | ||
} | ||
|
||
func NewCommandWidget(bw BaseWidget, opts WidgetConfig) (*CommandWidget, error) { | ||
bw.setInterval(opts.Interval, 1000) | ||
|
||
var command, font string | ||
ConfigValue(opts.Config["command"], &command) | ||
ConfigValue(opts.Config["font"], &font) | ||
|
||
return &CommandWidget{ | ||
BaseWidget: bw, | ||
command: command, | ||
font: font, | ||
}, nil | ||
} | ||
|
||
// Update renders the widget | ||
func (w *CommandWidget) Update(dev *streamdeck.Device) error { | ||
size := int(dev.Pixels) | ||
margin := size / 18 | ||
height := size - (margin * 2) | ||
img := image.NewRGBA(image.Rect(0, 0, size, size)) | ||
|
||
commands := strings.Split(w.command, ";") | ||
fonts := strings.Split(w.font, ";") | ||
|
||
if len(commands) == 0 || len(w.command) == 0 { | ||
return fmt.Errorf("no command(s) supplied") | ||
} | ||
for len(fonts) < len(commands) { | ||
fonts = append(fonts, "regular") | ||
} | ||
|
||
for i := 0; i < len(commands); i++ { | ||
str, err := runCommand(commands[i]) | ||
if err != nil { | ||
return err | ||
} | ||
font := fontByName(fonts[i]) | ||
lower := margin + (height/len(commands))*i | ||
upper := margin + (height/len(commands))*(i+1) | ||
|
||
drawString(img, image.Rect(0, lower, size, upper), | ||
font, | ||
str, | ||
dev.DPI, | ||
-1, | ||
image.Pt(-1, -1)) | ||
} | ||
|
||
return w.render(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 | ||
} |