From 36dd2598229b951620b287a68272670f6f976f1d Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Thu, 6 Jun 2024 13:25:01 -0500 Subject: [PATCH] simplify mako impl --- internal/mako.go | 72 +++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 26 deletions(-) diff --git a/internal/mako.go b/internal/mako.go index b5c7cef..bc3d621 100644 --- a/internal/mako.go +++ b/internal/mako.go @@ -31,24 +31,55 @@ type MakoNotificationData struct { Data string `json:"data"` } -// type Notifcation +type History struct { + Notifications []Notifcation +} -// type History struct { -// notifications []Notifcation -// } +type Notifcation struct { + AppName string + Summary string + Body string +} -func getHistory() MakoHistory { - var history MakoHistory +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, &history) - return history + json.Unmarshal(makoOutput, &makoHistory) + return makoHistory.toHistory() } -// TODO: replace with simple Render function -func displayNotification(n MakoNotification) { +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")). @@ -57,26 +88,15 @@ func displayNotification(n MakoNotification) { BorderStyle(lipgloss.ThickBorder()). BorderLeft(true) - fmt.Println(appStyle.Render(n.AppName.Data)) - if len(n.Summary.Data) > 0 { + fmt.Println(appStyle.Render(n.AppName)) + if len(n.Summary) > 0 { fmt.Println(textStyle. BorderForeground(lipgloss.Color("14")). - Render(n.Summary.Data)) + Render(n.Summary)) } - if len(n.Body.Data) > 0 { + if len(n.Body) > 0 { fmt.Println(textStyle. BorderForeground(lipgloss.Color("2")). - Render(n.Body.Data)) - } -} - - -func ListNotifications(number int) { - history := getHistory() - for i, notification := range history.Data[0] { - displayNotification(notification) - if i > number-1 { - break - } + Render(n.Body)) } }