2024-10-01 15:42:36 -05:00
|
|
|
import std/[algorithm, strutils, json, osproc, wordwrap, terminal]
|
|
|
|
import hwylterm
|
2024-11-07 16:46:17 -06:00
|
|
|
export hwylterm, json
|
|
|
|
|
|
|
|
# TODO: use jsony in this module
|
2024-10-20 07:46:02 -05:00
|
|
|
|
2024-10-01 15:42:36 -05:00
|
|
|
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
|
2024-11-07 16:46:17 -06:00
|
|
|
notifications*: seq[Notification]
|
2024-10-01 15:42:36 -05:00
|
|
|
|
|
|
|
func toNotification(mn: MakoNotification): Notification =
|
|
|
|
result.appName = mn.`app-name`.data
|
|
|
|
result.summary = mn.summary.data
|
2024-10-20 07:46:02 -05:00
|
|
|
result.body = mn.body.data
|
2024-10-01 15:42:36 -05:00
|
|
|
|
2024-11-07 16:46:17 -06:00
|
|
|
func toHistory*(mh: MakoHistory): History =
|
2024-10-01 15:42:36 -05:00
|
|
|
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]
|
|
|
|
|
2024-11-07 16:46:17 -06:00
|
|
|
proc getMakoHistory*(): MakoHistory =
|
2024-10-01 15:42:36 -05:00
|
|
|
let (output, errCode) = execCmdEx("makoctl history")
|
|
|
|
if errCode != 0: quit output, errCode
|
|
|
|
result = parseJson(output).to(MakoHistory)
|
|
|
|
|
2024-11-07 16:46:17 -06:00
|
|
|
proc getHistory*(reverse: bool, count: int): History =
|
|
|
|
getMakoHistory().toHistory().filter(reverse, count)
|
|
|
|
|
|
|
|
proc bb*(n: Notification): BbString =
|
2024-10-01 15:42:36 -05:00
|
|
|
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)
|
|
|
|
|