From c1647646b296c75a8fe0d46e7905599201cb37ac Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 18 Feb 2023 13:50:31 -0400 Subject: [PATCH 1/3] feat: add basic notification/message interface --- .hilbishrc.lua | 15 +++++++++++ nature/hummingbird.lua | 56 ++++++++++++++++++++++++++++++++++++++++++ nature/init.lua | 1 + 3 files changed, 72 insertions(+) create mode 100644 nature/hummingbird.lua diff --git a/.hilbishrc.lua b/.hilbishrc.lua index 5d6382b..1596e02 100644 --- a/.hilbishrc.lua +++ b/.hilbishrc.lua @@ -1,4 +1,5 @@ -- Default Hilbish config +local hilbish = require 'hilbish' local lunacolors = require 'lunacolors' local bait = require 'bait' local ansikit = require 'ansikit' @@ -22,3 +23,17 @@ bait.catch('hilbish.vimMode', function(mode) ansikit.cursorStyle(ansikit.lineCursor) end end) + +--[[ +hilbish.timeout(function() + hilbish.messages.send {title = 'greetings!', text = 'hello world :D'} +end, 2000) +]]-- + +bait.catch('hilbish.notification', function() + hilbish.prompt(lunacolors.blue('• 1 new notification'), 'right') + + hilbish.timeout(function() + hilbish.prompt('', 'right') + end, 3000) +end) diff --git a/nature/hummingbird.lua b/nature/hummingbird.lua new file mode 100644 index 0000000..0d3f0d7 --- /dev/null +++ b/nature/hummingbird.lua @@ -0,0 +1,56 @@ +local bait = require 'bait' +local commander = require 'commander' +local lunacolors = require 'lunacolors' + +local M = {} +local counter = 0 +M._messages = {} +M.icons = { + INFO = '', + SUCCESS = '', + WARN = '', + ERROR = '' +} + +hilbish.messages = {} + +--- Represents a Hilbish message. +--- @class hilbish.message +--- @field icon string Unicode (preferably standard emoji) icon for the message notification. +--- @field title string Title of the message (like an email subject). +--- @field text string Contents of the message. +--- @field channel string Short identifier of the message. `hilbish` and `hilbish.*` is preserved for internal Hilbish messages. +--- @field summary string A short summary of the message. +--- @field read boolean Whether the full message has been read or not. + +function expect(tbl, field) + if not tbl[field] or tbl[field] == '' then + error(string.format('expected field %s in message')) + end +end + +--- Sends a message. +--- @param message hilbish.message +function hilbish.messages.send(message) + expect(message, 'text') + expect(message, 'title') + message.index = counter + 1 + + M._messages[message.index] = message + bait.throw('hilbish.notification', message) +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) + end +end) + +return M diff --git a/nature/init.lua b/nature/init.lua index d1f919c..9e78135 100644 --- a/nature/init.lua +++ b/nature/init.lua @@ -11,6 +11,7 @@ require 'nature.completions' require 'nature.opts' require 'nature.vim' require 'nature.runner' +require 'nature.hummingbird' local shlvl = tonumber(os.getenv 'SHLVL') if shlvl ~= nil then From 34a5b2aedd7a9180c27f806c2bc7a7fd9d9aa17a Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 11 Apr 2023 19:17:10 -0400 Subject: [PATCH 2/3] fix: update message counter --- nature/hummingbird.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nature/hummingbird.lua b/nature/hummingbird.lua index 0d3f0d7..25f33f7 100644 --- a/nature/hummingbird.lua +++ b/nature/hummingbird.lua @@ -34,7 +34,8 @@ end function hilbish.messages.send(message) expect(message, 'text') expect(message, 'title') - message.index = counter + 1 + counter = counter + 1 + message.index = counter M._messages[message.index] = message bait.throw('hilbish.notification', message) From 466ca06f79eea3d0317e9d761f266531bc6ec85e Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 11 Apr 2023 19:31:37 -0400 Subject: [PATCH 3/3] 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)