newtab/dunstctl/dunstctl.go

83 lines
2.6 KiB
Go

package dunstctl
import (
"encoding/json"
"os/exec"
"strconv"
)
type DunstNotificationData[T any] struct {
Type string `json:"type"`
Data T `json:"data"`
}
type DunstNotification struct {
Body DunstNotificationData[string] `json:"body"`
Message DunstNotificationData[string] `json:"message"`
Summary DunstNotificationData[string] `json:"summary"`
AppName DunstNotificationData[string] `json:"appname"`
Category DunstNotificationData[string] `json:"category"`
IconPath DunstNotificationData[string] `json:"icon_path"`
DefaultActionName DunstNotificationData[string] `json:"default_action_name"`
Id DunstNotificationData[int64] `json:"id"`
Timestamp DunstNotificationData[int64] `json:"timestamp"`
Timeout DunstNotificationData[int64] `json:"timeout"`
Progress DunstNotificationData[int64] `json:"progress"`
}
type Notification struct {
Body string `json:"body"`
Message string `json:"message"`
Summary string `json:"summary"`
AppName string `json:"appname"`
Category string `json:"category"`
IconPath string `json:"icon_path"`
DefaultActionName string `json:"default_action_name"`
Id int64 `json:"id"`
Timestamp int64 `json:"timestamp"`
Timeout int64 `json:"timeout"`
Progress int64 `json:"progress"`
}
func (notif DunstNotification) CleanUp() Notification {
return Notification{
Body: notif.Body.Data,
Message: notif.Message.Data,
Summary: notif.Summary.Data,
AppName: notif.AppName.Data,
Category: notif.Category.Data,
IconPath: notif.IconPath.Data,
DefaultActionName: notif.DefaultActionName.Data,
Id: notif.Id.Data,
Timestamp: notif.Timestamp.Data,
Timeout: notif.Timeout.Data,
Progress: notif.Progress.Data,
}
}
func GetHistory() (history []Notification, err error) {
cmd := exec.Command("dunstctl", "history")
cmdOutput, err := cmd.Output()
if err != nil {
return
}
var historyWrapper = DunstNotificationData[[][]DunstNotification]{}
err = json.Unmarshal(cmdOutput, &historyWrapper)
for _, i := range historyWrapper.Data {
cleanedUp := []Notification{}
for _, j := range i {
cleanedUp = append(cleanedUp, j.CleanUp())
}
history = append(history, cleanedUp...)
}
return
}
func CloseNotification(id int64) (err error) {
cmd := exec.Command("dunstctl", "history-rm", strconv.FormatInt(id, 10))
_, err = cmd.Output()
return
}