hlctl/cmdlets/notify/notify.go

85 lines
1.6 KiB
Go

package notify
import (
"errors"
"fmt"
"strconv"
"strings"
"sectorinf.com/emilis/hlctl/cmdlets"
"sectorinf.com/emilis/hlctl/config"
"sectorinf.com/emilis/hlctl/ctllog"
"sectorinf.com/emilis/hlctl/notifyctl"
)
var log = ctllog.Logger{}.New("notify", ctllog.ColorRGB{}.FromHex("ff003e"))
type command struct{}
var Command command
func (command) Name() string {
return "notify"
}
func (command) Usage() cmdlets.Usage {
return cmdlets.Usage{
Name: Command.Name(),
Short: "Draws notifications using dzen2",
Long: []string{
"`notify` Will create notifications using the theme colors",
"that hlwm was set over",
},
Flags: map[string]string{
"-s": "Amount of seconds to persist for, default 1",
},
Examples: []string{
"notify -s 3 \"Hello world\"",
"notify Hey what's up chicken head",
},
}
}
func (command) Invoke(args ...string) error {
var (
secs string = "1"
)
cleanArgs := make([]string, 0, len(args))
for i := 0; i < len(args); i++ {
arg := args[i]
if len(arg) > 1 && arg[0] == '-' {
var next string
if i+1 < len(args) {
next = args[i+1]
}
switch arg[1] {
case 's':
secs = next
i++
default:
cleanArgs = append(cleanArgs, arg)
}
} else {
cleanArgs = append(cleanArgs, arg)
}
}
if len(cleanArgs) == 0 {
return errors.New("no message")
}
message := strings.Join(cleanArgs, " ")
cfg := config.Load()
secsNum, err := strconv.ParseUint(secs, 10, 64)
if err != nil {
return fmt.Errorf("seconds[%s]: %w", secs, err)
}
c := cfg.Theme.Colors
return notifyctl.Display(
message,
uint(secsNum),
c.Text,
c.Normal,
cfg.Font,
)
}