hyprman/src/mako.nim
2024-10-01 15:42:36 -05:00

57 lines
1.8 KiB
Nim

import std/[algorithm, strutils, json, osproc, wordwrap, terminal]
import hwylterm
type
MakoNotificationData = object
`type`, data: string
MakoNotification = object
`app-name`, summary, body: MakoNotificationData
MakoHistory = object
`type`: string
data: seq[seq[MakoNotification]]
Notification = object
appName, summary, body: string
History = object
notifications: seq[Notification]
func toNotification(mn: MakoNotification): Notification =
result.appName = mn.`app-name`.data
result.summary = mn.summary.data
result.body = mn.body.data
func toHistory(mh: MakoHistory): History =
for mn in mh.data[0]:
result.notifications.add mn.toNotification()
func filter(h: History, reverse: bool, count: int): History =
result.notifications = h.notifications
if reverse:
result.notifications.reverse()
let high = min(count, h.notifications.len - 1)
result.notifications =
result.notifications[0..high]
proc getMakoHistory(): MakoHistory =
let (output, errCode) = execCmdEx("makoctl history")
if errCode != 0: quit output, errCode
result = parseJson(output).to(MakoHistory)
proc bb(n: Notification): BbString =
template border(style: string): untyped = "[" & style & "]" & "┃ [/]"
var raw: string
raw.add border("magenta") & "[yellow]" & n.appName & "[/]\n"
raw.add border("green") & "[b]" & n.summary & "[/]\n"
for line in n.body.wrapWords(maxLineWidth = terminalWidth()-2).splitLines():
raw.add border("default") & line & "\n"
result = bb(raw)
proc makoCmd*(config = "", count = 10, json = false, reverse = false) =
## interact with mako
let history = getMakoHistory().toHistory().filter(reverse, count)
if json:
echo $(%* history)
else:
for n in history.notifications:
echo $bb(n)