From 68c90481fcd0c51f1231ca3cfeba7e243042a21b Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 11 Jun 2024 11:53:13 -0500 Subject: [PATCH] add --json flag to hyprman mako --- cmd/eww.go | 4 ++-- cmd/mako.go | 12 +++++++----- cmd/root.go | 3 ++- internal/mako.go | 20 +++++++++++++++----- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/cmd/eww.go b/cmd/eww.go index fba7a41..5299826 100644 --- a/cmd/eww.go +++ b/cmd/eww.go @@ -8,8 +8,8 @@ var ewwCmd = &cobra.Command{ Use: "eww", Short: "eww integration", PersistentPreRun: func(cmd *cobra.Command, args []string) { - hm.LoadConfig(configPath) - }, + hm.LoadConfig(configPath) + }, } func init() { diff --git a/cmd/mako.go b/cmd/mako.go index 1f7a7b5..e3add22 100644 --- a/cmd/mako.go +++ b/cmd/mako.go @@ -1,24 +1,26 @@ package cmd import ( - "git.dayl.in/daylin/hyprman/internal" "github.com/spf13/cobra" + + hyprman "git.dayl.in/daylin/hyprman/internal" ) var makoCmd = &cobra.Command{ Use: "mako", Short: "mako integration", Run: func(cmd *cobra.Command, args []string) { - hyprman.ListNotifications(number) + hyprman.ListNotifications(number, json) }, } - var ( - number int + number int + json bool ) func init() { rootCmd.AddCommand(makoCmd) - makoCmd.Flags().IntVarP(&number, "number", "n", 10, "number of notifications") + makoCmd.Flags().IntVarP(&number, "number", "n", 10, "number of notifications") + makoCmd.Flags().BoolVar(&json, "json", false, "output data as json") } diff --git a/cmd/root.go b/cmd/root.go index 697e7ff..9ab4c9d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,9 +3,10 @@ package cmd import ( "os" - "git.dayl.in/daylin/hyprman/internal" cc "github.com/ivanpirog/coloredcobra" "github.com/spf13/cobra" + + hyprman "git.dayl.in/daylin/hyprman/internal" ) func Execute() { diff --git a/internal/mako.go b/internal/mako.go index bc3d621..35280a8 100644 --- a/internal/mako.go +++ b/internal/mako.go @@ -69,13 +69,23 @@ func getHistory() History { return makoHistory.toHistory() } -func ListNotifications(number int) { +func (h *History) truncate(number int) { + h.Notifications = h.Notifications[:min(len(h.Notifications), number)] +} + +func ListNotifications(number int, outputJson bool) { history := getHistory() - for i, n := range history.Notifications { - if i > number-1 { - break + history.truncate(number) + if !outputJson { + for _, n := range history.Notifications { + n.Render() } - n.Render() + } else { + b, err := json.MarshalIndent(history.Notifications, "", " ") + if err != nil { + log.Fatal(err) + } + fmt.Println(string(b)) } }