hyprman/internal/mako.go

113 lines
2.4 KiB
Go
Raw Normal View History

2024-05-21 15:41:20 -05:00
package hyprman
import (
"encoding/json"
"fmt"
"log"
"os/exec"
2024-06-06 13:11:21 -05:00
"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"`
}
2024-06-06 13:25:01 -05:00
type History struct {
Notifications []Notifcation
}
type Notifcation struct {
AppName string
Summary string
Body string
}
2024-06-06 13:11:21 -05:00
2024-06-06 13:25:01 -05:00
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
}
2024-06-06 13:11:21 -05:00
2024-06-06 13:25:01 -05:00
func getHistory() History {
var makoHistory MakoHistory
makoOutput, err := exec.Command("makoctl", "history").CombinedOutput()
if err != nil {
log.Fatal(err)
}
2024-06-06 13:25:01 -05:00
json.Unmarshal(makoOutput, &makoHistory)
return makoHistory.toHistory()
}
2024-06-11 11:53:13 -05:00
func (h *History) truncate(number int) {
h.Notifications = h.Notifications[:min(len(h.Notifications), number)]
}
func ListNotifications(number int, outputJson bool) {
2024-06-06 13:25:01 -05:00
history := getHistory()
2024-06-11 11:53:13 -05:00
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)
2024-06-06 13:25:01 -05:00
}
2024-06-11 11:53:13 -05:00
fmt.Println(string(b))
2024-06-06 13:25:01 -05:00
}
}
2024-06-06 13:25:01 -05:00
func (n Notifcation) Render() {
2024-06-06 13:11:21 -05:00
appStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("3")).
PaddingTop(1)
textStyle := lipgloss.NewStyle().Width(80).PaddingLeft(2).
BorderStyle(lipgloss.ThickBorder()).
BorderLeft(true)
2024-06-06 13:25:01 -05:00
fmt.Println(appStyle.Render(n.AppName))
if len(n.Summary) > 0 {
2024-06-06 13:11:21 -05:00
fmt.Println(textStyle.
BorderForeground(lipgloss.Color("14")).
2024-06-06 13:25:01 -05:00
Render(n.Summary))
}
2024-06-06 13:25:01 -05:00
if len(n.Body) > 0 {
2024-06-06 13:11:21 -05:00
fmt.Println(textStyle.
BorderForeground(lipgloss.Color("2")).
2024-06-06 13:25:01 -05:00
Render(n.Body))
}
}