Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ImageCommand #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ func NewWidget(dev *streamdeck.Device, base string, kc KeyConfig, bg image.Image
case "command":
return NewCommandWidget(bw, kc.Widget), nil

case "imageCommand":
return NewCommandImageWidget(bw, kc.Widget), nil

case "weather":
return NewWeatherWidget(bw, kc.Widget)
}
Expand Down
54 changes: 54 additions & 0 deletions widget_command_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"image"
"os"
"time"
)

type CommandImageWidget struct {
*BaseWidget
commands []string
}

// NewCommandImageWidget returns a new CommandImageWidget.
func NewCommandImageWidget(bw *BaseWidget, opts WidgetConfig) *CommandImageWidget {
bw.setInterval(time.Duration(opts.Interval)*time.Millisecond, time.Second)

var commands []string
_ = ConfigValue(opts.Config["command"], &commands)

return &CommandImageWidget{
BaseWidget: bw,
commands: commands,
}
}

func (w *CommandImageWidget) Update() error {
size := int(w.dev.Pixels)
margin := size / 18
height := size - (margin * 2)
img := image.NewRGBA(image.Rect(0, 0, size, size))

str, err3 := runCommand(w.commands[0])
if err3 != nil {
fmt.Fprintf(os.Stderr, "Running command failed: %s\n", err3)

return err3
}
icon, err := loadImage(str)
if err != nil {
fmt.Fprintf(os.Stderr, "Loading Image failed: %s\n", err)
} else {
err2 := drawImage(img,
icon,
height,
image.Pt(-1, -1))
if err2 != nil {
return w.render(w.dev, img)
}
}

return w.render(w.dev, img)
}