mirror of https://github.com/Hilbis/Hilbish
feat(greenhouse): add command handling
parent
0bb97a6f3b
commit
8b672f5b95
|
@ -8,6 +8,15 @@ local Page = require 'nature.greenhouse.page'
|
|||
|
||||
commander.register('greenhouse', function(args, sinks)
|
||||
local gh = Greenhouse(sinks.out)
|
||||
|
||||
local buffer = ''
|
||||
local command = false
|
||||
local commands = {
|
||||
q = function()
|
||||
gh.keybinds['Ctrl-D'](gh)
|
||||
end
|
||||
}
|
||||
|
||||
function gh:resize()
|
||||
local size = terminal.size()
|
||||
self.region = {
|
||||
|
@ -15,6 +24,7 @@ commander.register('greenhouse', function(args, sinks)
|
|||
height = size.height - 2
|
||||
}
|
||||
end
|
||||
|
||||
local oldDraw = gh.draw
|
||||
function gh:draw()
|
||||
oldDraw(self)
|
||||
|
@ -36,12 +46,43 @@ commander.register('greenhouse', function(args, sinks)
|
|||
end
|
||||
self.sink:write(buffer)
|
||||
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:keybind('Enter', function(self)
|
||||
if self.isToc then
|
||||
self:jump(self.tocPageIdx)
|
||||
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)
|
||||
|
||||
|
|
|
@ -176,6 +176,9 @@ function Greenhouse:toc(toggle)
|
|||
self:draw()
|
||||
end
|
||||
|
||||
function Greenhouse:input(char)
|
||||
end
|
||||
|
||||
function Greenhouse:initUi()
|
||||
local ansikit = require 'ansikit'
|
||||
local bait = require 'bait'
|
||||
|
@ -201,12 +204,14 @@ function Greenhouse:initUi()
|
|||
hilbish.goro(function()
|
||||
while not done do
|
||||
local c = read()
|
||||
if c == 'Ctrl-D' then
|
||||
self:keybind('Ctrl-D', function()
|
||||
done = true
|
||||
end
|
||||
end)
|
||||
|
||||
if self.keybinds[c] then
|
||||
self.keybinds[c](self)
|
||||
else
|
||||
self:input(c)
|
||||
end
|
||||
|
||||
--[[
|
||||
|
|
|
@ -188,5 +188,5 @@ func (rl *Instance) ReadChar() string {
|
|||
}
|
||||
}
|
||||
|
||||
return "???"
|
||||
return s
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue