From 466ca06f79eea3d0317e9d761f266531bc6ec85e Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 11 Apr 2023 19:31:37 -0400 Subject: [PATCH] feat: implement message managing functions --- nature/hummingbird.lua | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/nature/hummingbird.lua b/nature/hummingbird.lua index 25f33f7..52a81aa 100644 --- a/nature/hummingbird.lua +++ b/nature/hummingbird.lua @@ -36,21 +36,53 @@ function hilbish.messages.send(message) expect(message, 'title') counter = counter + 1 message.index = counter + message.read = false M._messages[message.index] = message bait.throw('hilbish.notification', message) end +function hilbish.messages.read(idx) + local msg = M._messages[idx] + if msg then + M._messages[idx].read = true + end +end + +function hilbish.messages.readAll(idx) + for _, msg in ipairs(hilbish.messages.all()) do + hilbish.messages.read(msg.index) + end +end + +function hilbish.messages.delete(idx) + local msg = M._messages[idx] + if not msg then + error(string.format('invalid message index %d', idx or -1)) + end + + M._messages[idx] = nil +end + +function hilbish.messages.clear() + for _, msg in ipairs(hilbish.messages.all()) do + hilbish.messages.delete(msg.index) + end +end + function hilbish.messages.all() return M._messages end commander.register('messages', function(_, sinks) - for _, msg in ipairs(hilbish.messages.all()) do - local heading = lunacolors.format(string.format('Message {cyan}#%d{reset}: %s', msg.index, msg.title)) - sinks.out:writeln(heading) - sinks.out:writeln(string.rep('=', string.len(heading))) - sinks.out:writeln(msg.text) + for idx = counter, 1, -1 do + local msg = M._messages[idx] + if msg then + local heading = lunacolors.format(string.format('Message {cyan}#%d{reset}: %s', msg.index, msg.title)) + sinks.out:writeln(heading) + sinks.out:writeln(string.rep('=', string.len(heading))) + sinks.out:writeln(msg.text) + end end end)