hyprman/src/mako.nim

53 lines
1.7 KiB
Nim

import std/[algorithm, strutils, json, osproc, wordwrap, terminal]
import hwylterm
export hwylterm, json
# TODO: use jsony in this module
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 getHistory*(reverse: bool, count: int): History =
getMakoHistory().toHistory().filter(reverse, count)
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)