hyprman/internal/mako.go
2024-06-06 13:25:01 -05:00

103 lines
2.1 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 ListNotifications(number int) {
history := getHistory()
for i, n := range history.Notifications {
if i > number-1 {
break
}
n.Render()
}
}
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))
}
}