feat(greenhouse): add goto command

made some other minor changes (in terms of how much it
matters to the user)

the toc page is now a "special page"
in a next commit itll also be used for a help page

cursor gets hidden unless typing a command
pull/240/head
sammyette 2023-07-09 23:39:11 -04:00
parent 8b672f5b95
commit 11da2c0c45
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 83 additions and 60 deletions

View File

@ -10,10 +10,17 @@ commander.register('greenhouse', function(args, sinks)
local gh = Greenhouse(sinks.out)
local buffer = ''
local display = ''
local command = false
local commands = {
q = function()
gh.keybinds['Ctrl-D'](gh)
end,
['goto'] = function(args)
if not args[1] then
return 'nuh uh'
end
gh:jump(tonumber(args[1]))
end
}
@ -30,13 +37,13 @@ commander.register('greenhouse', function(args, sinks)
oldDraw(self)
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isToc then
offset = self.tocOffset
workingPage = self.tocPage
if self.isSpecial then
offset = self.specialOffset
workingPage = self.specialPage
end
self.sink:write(ansikit.getCSI((self.region.height + 2) - self.start.. ';1', 'H'))
if not self.isToc then
if not self.isSpecial then
self.sink:write(string.format('\27[0mPage %d', self.curPage))
if workingPage.title ~= '' then
self.sink:writeln('' .. workingPage.title)
@ -44,7 +51,7 @@ commander.register('greenhouse', function(args, sinks)
self.sink:writeln('')
end
end
self.sink:write(buffer)
self.sink:write(buffer == '' and display or buffer)
end
function gh:input(c)
-- command handling
@ -52,16 +59,26 @@ commander.register('greenhouse', function(args, sinks)
command = true
end
if c == 'Escape' then
command = false
buffer = ''
goto update
if command then
command = false
buffer = ''
else
if self.isSpecial then gh:special() end
end
elseif c == 'Backspace' then
buffer = buffer:sub(0, -2)
goto update
if buffer == '' then
command = false
else
goto update
end
end
if command then
buffer = buffer .. c
ansikit.showCursor()
if buffer:match '^:' then buffer = buffer .. c else buffer = c end
else
ansikit.hideCursor()
end
::update::
@ -70,9 +87,9 @@ commander.register('greenhouse', function(args, sinks)
gh:resize()
gh:keybind('Enter', function(self)
if self.isToc then
self:jump(self.tocPageIdx)
self:toc(true)
if self.isSpecial then
self:jump(self.specialPageIdx)
self:special(true)
else
if buffer:len() < 2 then return end
@ -80,7 +97,7 @@ commander.register('greenhouse', function(args, sinks)
local command = commands[splitBuf[1]:sub(2)]
if command then
table.remove(splitBuf, 1)
command(splitBuf)
buffer = command(splitBuf) or ''
end
self:update()
end

View File

@ -27,10 +27,10 @@ function Greenhouse:new(sink)
['Ctrl-Right'] = self.next,
['Ctrl-N'] = function(self) self:toc(true) end,
}
self.isToc = false
self.tocPage = nil
self.tocPageIdx = 1
self.tocOffset = 1
self.isSpecial = false
self.specialPage = nil
self.specialPageIdx = 1
self.specialOffset = 1
return self
end
@ -47,9 +47,9 @@ end
function Greenhouse:draw()
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isToc then
offset = self.tocOffset
workingPage = self.tocPage
if self.isSpecial then
offset = self.specialOffset
workingPage = self.specialPage
end
local lines = workingPage.lines
@ -69,7 +69,7 @@ function Greenhouse:render()
end
function Greenhouse:scroll(direction)
if self.isToc then
if self.isSpecial then
if direction == 'down' then
self:next(true)
elseif direction == 'up' then
@ -92,24 +92,56 @@ end
function Greenhouse:update()
self:resize()
if self.isToc then
self:toc()
if self.isSpecial then
self:special()
end
self:draw()
end
function Greenhouse:special()
self.isSpecial = not self.isSpecial
end
function Greenhouse:toc(toggle)
if not self.isSpecial then
self.specialPageIdx = self.curPage
end
if toggle then self.isSpecial = not self.isSpecial end
-- Generate a special page for our table of contents
local tocText = string.format([[
%s
]], lunacolors.cyan(lunacolors.bold '―― Table of Contents ――'))
local genericPageCount = 1
for i, page in ipairs(self.pages) do
local title = page.title
if title == 'Page' then
title = 'Page #' .. genericPageCount
genericPageCount = genericPageCount + 1
end
if i == self.specialPageIdx then
title = lunacolors.invert(title)
end
tocText = tocText .. title .. '\n'
end
self.specialPage = Page('TOC', tocText)
self:draw()
end
function Greenhouse:resize()
local size = terminal.size()
self.region = size
end
function Greenhouse:next(toc)
local oldCurrent = toc and self.tocPageIdx or self.curPage
function Greenhouse:next(special)
local oldCurrent = special and self.specialPageIdx or self.curPage
local pageIdx = math.min(oldCurrent + 1, #self.pages)
if toc then
self.tocPageIdx = pageIdx
if special then
self.specialPageIdx = pageIdx
else
self.curPage = pageIdx
end
@ -120,12 +152,12 @@ function Greenhouse:next(toc)
end
end
function Greenhouse:previous(toc)
local oldCurrent = toc and self.tocPageIdx or self.curPage
function Greenhouse:previous(special)
local oldCurrent = special and self.specialPageIdx or self.curPage
local pageIdx = math.max(self.curPage - 1, 1)
if toc then
self.tocPageIdx = pageIdx
if special then
self.specialPageIdx = pageIdx
else
self.curPage = pageIdx
end
@ -148,34 +180,6 @@ function Greenhouse:keybind(key, callback)
self.keybinds[key] = callback
end
function Greenhouse:toc(toggle)
if not self.isToc then
self.tocPageIdx = self.curPage
end
if toggle then self.isToc = not self.isToc end
-- Generate a special page for our table of contents
local tocText = string.format([[
%s
]], lunacolors.cyan(lunacolors.bold '―― Table of Contents ――'))
local genericPageCount = 1
for i, page in ipairs(self.pages) do
local title = page.title
if title == 'Page' then
title = 'Page #' .. genericPageCount
genericPageCount = genericPageCount + 1
end
if i == self.tocPageIdx then
title = lunacolors.invert(title)
end
tocText = tocText .. title .. '\n'
end
self.tocPage = Page('TOC', tocText)
self:draw()
end
function Greenhouse:input(char)
end
@ -198,6 +202,7 @@ function Greenhouse:initUi()
end)
ansikit.screenAlt()
ansikit.hideCursor()
ansikit.clear(true)
self:draw()
@ -251,6 +256,7 @@ function Greenhouse:initUi()
while not done do
--
end
ansikit.showCursor()
ansikit.screenMain()
end