Skip to content

Commit

Permalink
Write errors to Stderr and remove a few fatal calls
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Jan 21, 2022
1 parent 9bf033d commit 2a212f3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
19 changes: 10 additions & 9 deletions deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func emulateKeyPresses(keys string) {
// emulates a (multi-)key press.
func emulateKeyPress(keys string) {
if keyboard == nil {
fmt.Println("Keyboard emulation is disabled!")
fmt.Fprintln(os.Stderr, "Keyboard emulation is disabled!")
return
}

Expand All @@ -150,7 +150,7 @@ func emulateKeyPress(keys string) {
k = formatKeycodes(strings.TrimSpace(k))
kc, err := strconv.Atoi(k)
if err != nil {
fatalf("%s is not a valid keycode: %s\n", k, err)
fmt.Fprintf(os.Stderr, "%s is not a valid keycode: %s\n", k, err)
}

if i+1 < len(kk) {
Expand All @@ -166,7 +166,7 @@ func emulateKeyPress(keys string) {
func emulateClipboard(text string) {
err := clipboard.WriteAll(text)
if err != nil {
fatalf("Pasting to clipboard failed: %s\n", err)
fmt.Fprintf(os.Stderr, "Pasting to clipboard failed: %s\n", err)
}

// paste the string
Expand All @@ -177,7 +177,7 @@ func emulateClipboard(text string) {
func executeDBusMethod(object, path, method, args string) {
call := dbusConn.Object(object, dbus.ObjectPath(path)).Call(method, 0, args)
if call.Err != nil {
fmt.Printf("dbus call failed: %s\n", call.Err)
fmt.Fprintf(os.Stderr, "dbus call failed: %s\n", call.Err)
}
}

Expand All @@ -190,12 +190,12 @@ func executeCommand(cmd string) {
args := strings.Split(cmd, " ")
c := exec.Command(args[0], args[1:]...) //nolint:gosec
if err := c.Start(); err != nil {
fmt.Printf("command failed: %s\n", err)
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
return
}

if err := c.Wait(); err != nil {
fmt.Printf("command failed: %s\n", err)
fmt.Fprintf(os.Stderr, "Command failed: %s\n", err)
}
}

Expand All @@ -215,11 +215,12 @@ func (d *Deck) triggerAction(dev *streamdeck.Device, index uint8, hold bool) {
if a.Deck != "" {
d, err := LoadDeck(dev, filepath.Dir(d.File), a.Deck)
if err != nil {
fatal(err)
fmt.Fprintln(os.Stderr, "Can't load deck:", err)
return
}
err = dev.Clear()
if err != nil {
if err := dev.Clear(); err != nil {
fatal(err)
return
}

deck = d
Expand Down
3 changes: 2 additions & 1 deletion desktop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"image"
"os"
"time"

"github.com/jezek/xgb"
Expand Down Expand Up @@ -198,7 +199,7 @@ func (x Xorg) name(w xproto.Window) (string, error) {
func (x Xorg) icon(w xproto.Window) (image.Image, error) {
icon, err := xgraphics.FindIcon(x.util, w, 128, 128)
if err != nil {
fmt.Printf("Could not find icon for window %d\n", w)
fmt.Fprintf(os.Stderr, "Could not find icon for window %d\n", w)
return nil, err
}

Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ func eventLoop(dev *streamdeck.Device, tch chan interface{}) error {

func closeDevice(dev *streamdeck.Device) {
if err := dev.Reset(); err != nil {
fmt.Fprintln(os.Stderr, "unable to reset Stream Deck")
fmt.Fprintln(os.Stderr, "Unable to reset Stream Deck")
}
if err := dev.Close(); err != nil {
fmt.Fprintln(os.Stderr, "unable to close Stream Deck")
fmt.Fprintln(os.Stderr, "Unable to close Stream Deck")
}
}

Expand Down Expand Up @@ -216,15 +216,15 @@ func run() error {
defer xorg.Close()
xorg.TrackWindows(tch, time.Second)
} else {
fmt.Printf("Could not connect to X server: %s\n", err)
fmt.Println("Tracking window manager will be disabled!")
fmt.Fprintf(os.Stderr, "Could not connect to X server: %s\n", err)
fmt.Fprintln(os.Stderr, "Tracking window manager will be disabled!")
}

// initialize virtual keyboard
keyboard, err = uinput.CreateKeyboard("/dev/uinput", []byte("Deckmaster"))
if err != nil {
fmt.Printf("Could not create virtual input device (/dev/uinput): %s\n", err)
fmt.Println("Emulating keyboard events will be disabled!")
fmt.Fprintf(os.Stderr, "Could not create virtual input device (/dev/uinput): %s\n", err)
fmt.Fprintln(os.Stderr, "Emulating keyboard events will be disabled!")
} else {
defer keyboard.Close() //nolint:errcheck
}
Expand Down
3 changes: 2 additions & 1 deletion widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func drawString(img *image.RGBA, bounds image.Rectangle, ttf *truetype.Font, tex

c.SetSrc(image.NewUniform(color))
if _, err := c.DrawString(text, freetype.Pt(pt.X, pt.Y)); err != nil {
fatal(err)
fmt.Fprintf(os.Stderr, "Can't render string: %s\n", err)
return
}
}
4 changes: 2 additions & 2 deletions widget_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (w *TopWidget) Update() error {
case "cpu":
cpuUsage, err := cpu.Percent(0, false)
if err != nil {
fatal(err)
return fmt.Errorf("can't retrieve CPU usage: %s", err)
}

value = cpuUsage[0]
Expand All @@ -59,7 +59,7 @@ func (w *TopWidget) Update() error {
case "memory":
memory, err := mem.VirtualMemory()
if err != nil {
fatal(err)
return fmt.Errorf("can't retrieve memory usage: %s", err)
}
value = memory.UsedPercent
label = "MEM"
Expand Down

0 comments on commit 2a212f3

Please sign in to comment.