feat(greenhouse): add command handling

pull/240/head
sammyette 2023-07-09 23:07:32 -04:00
parent 0bb97a6f3b
commit 8b672f5b95
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 49 additions and 3 deletions

View File

@ -8,6 +8,15 @@ local Page = require 'nature.greenhouse.page'
commander.register('greenhouse', function(args, sinks) commander.register('greenhouse', function(args, sinks)
local gh = Greenhouse(sinks.out) local gh = Greenhouse(sinks.out)
local buffer = ''
local command = false
local commands = {
q = function()
gh.keybinds['Ctrl-D'](gh)
end
}
function gh:resize() function gh:resize()
local size = terminal.size() local size = terminal.size()
self.region = { self.region = {
@ -15,6 +24,7 @@ commander.register('greenhouse', function(args, sinks)
height = size.height - 2 height = size.height - 2
} }
end end
local oldDraw = gh.draw local oldDraw = gh.draw
function gh:draw() function gh:draw()
oldDraw(self) oldDraw(self)
@ -36,12 +46,43 @@ commander.register('greenhouse', function(args, sinks)
end end
self.sink:write(buffer) self.sink:write(buffer)
end end
function gh:input(c)
-- command handling
if c == ':' and not command then
command = true
end
if c == 'Escape' then
command = false
buffer = ''
goto update
elseif c == 'Backspace' then
buffer = buffer:sub(0, -2)
goto update
end
if command then
buffer = buffer .. c
end
::update::
gh:update()
end
gh:resize() gh:resize()
gh:keybind('Enter', function(self) gh:keybind('Enter', function(self)
if self.isToc then if self.isToc then
self:jump(self.tocPageIdx) self:jump(self.tocPageIdx)
self:toc(true) self:toc(true)
else
if buffer:len() < 2 then return end
local splitBuf = string.split(buffer, " ")
local command = commands[splitBuf[1]:sub(2)]
if command then
table.remove(splitBuf, 1)
command(splitBuf)
end
self:update()
end end
end) end)

View File

@ -176,6 +176,9 @@ function Greenhouse:toc(toggle)
self:draw() self:draw()
end end
function Greenhouse:input(char)
end
function Greenhouse:initUi() function Greenhouse:initUi()
local ansikit = require 'ansikit' local ansikit = require 'ansikit'
local bait = require 'bait' local bait = require 'bait'
@ -201,12 +204,14 @@ function Greenhouse:initUi()
hilbish.goro(function() hilbish.goro(function()
while not done do while not done do
local c = read() local c = read()
if c == 'Ctrl-D' then self:keybind('Ctrl-D', function()
done = true done = true
end end)
if self.keybinds[c] then if self.keybinds[c] then
self.keybinds[c](self) self.keybinds[c](self)
else
self:input(c)
end end
--[[ --[[

View File

@ -188,5 +188,5 @@ func (rl *Instance) ReadChar() string {
} }
} }
return "???" return s
} }