hyprman/internal/mako.go

113 lines
2.4 KiB
Go

package hyprman
import (
"encoding/json"
"fmt"
"log"
"os/exec"
"github.com/charmbracelet/lipgloss"
)
type MakoHistory struct {
Type string `json:"type"`
Data [][]MakoNotification `json:"data"`
}
type MakoNotification struct {
AppName MakoNotificationData `json:"app-name"`
Summary MakoNotificationData
Body MakoNotificationData
// AppIcon MakoNotificationData `json:"app-icon"`
// Category MakoNotificationData
// DesktopEntry MakoNotificationData `json:"desktop-entry"`
// Id MakoNotificationData
// Urgency MakoNotificationData
// Actions MakoNotificationData
}
type MakoNotificationData struct {
Type string `json:"type"`
Data string `json:"data"`
}
type History struct {
Notifications []Notifcation
}
type Notifcation struct {
AppName string
Summary string
Body string
}
func (mn MakoNotification) toNotification() Notifcation {
var n Notifcation
n.AppName = mn.AppName.Data
n.Summary = mn.Summary.Data
n.Body = mn.Body.Data
return n
}
func (mh MakoHistory) toHistory() History {
var h History
var notifications []Notifcation
for _, mn := range mh.Data[0] {
notifications = append(notifications, mn.toNotification())
}
h.Notifications = notifications
return h
}
func getHistory() History {
var makoHistory MakoHistory
makoOutput, err := exec.Command("makoctl", "history").CombinedOutput()
if err != nil {
log.Fatal(err)
}
json.Unmarshal(makoOutput, &makoHistory)
return makoHistory.toHistory()
}
func (h *History) truncate(number int) {
h.Notifications = h.Notifications[:min(len(h.Notifications), number)]
}
func ListNotifications(number int, outputJson bool) {
history := getHistory()
history.truncate(number)
if !outputJson {
for _, n := range history.Notifications {
n.Render()
}
} else {
b, err := json.MarshalIndent(history.Notifications, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
}
func (n Notifcation) Render() {
appStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("3")).
PaddingTop(1)
textStyle := lipgloss.NewStyle().Width(80).PaddingLeft(2).
BorderStyle(lipgloss.ThickBorder()).
BorderLeft(true)
fmt.Println(appStyle.Render(n.AppName))
if len(n.Summary) > 0 {
fmt.Println(textStyle.
BorderForeground(lipgloss.Color("14")).
Render(n.Summary))
}
if len(n.Body) > 0 {
fmt.Println(textStyle.
BorderForeground(lipgloss.Color("2")).
Render(n.Body))
}
}