mirror of https://github.com/Hilbis/Hilbish
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 commandpull/240/head
parent
8b672f5b95
commit
11da2c0c45
|
@ -10,10 +10,17 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
local gh = Greenhouse(sinks.out)
|
local gh = Greenhouse(sinks.out)
|
||||||
|
|
||||||
local buffer = ''
|
local buffer = ''
|
||||||
|
local display = ''
|
||||||
local command = false
|
local command = false
|
||||||
local commands = {
|
local commands = {
|
||||||
q = function()
|
q = function()
|
||||||
gh.keybinds['Ctrl-D'](gh)
|
gh.keybinds['Ctrl-D'](gh)
|
||||||
|
end,
|
||||||
|
['goto'] = function(args)
|
||||||
|
if not args[1] then
|
||||||
|
return 'nuh uh'
|
||||||
|
end
|
||||||
|
gh:jump(tonumber(args[1]))
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,13 +37,13 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
oldDraw(self)
|
oldDraw(self)
|
||||||
local workingPage = self.pages[self.curPage]
|
local workingPage = self.pages[self.curPage]
|
||||||
local offset = self.offset
|
local offset = self.offset
|
||||||
if self.isToc then
|
if self.isSpecial then
|
||||||
offset = self.tocOffset
|
offset = self.specialOffset
|
||||||
workingPage = self.tocPage
|
workingPage = self.specialPage
|
||||||
end
|
end
|
||||||
|
|
||||||
self.sink:write(ansikit.getCSI((self.region.height + 2) - self.start.. ';1', 'H'))
|
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))
|
self.sink:write(string.format('\27[0mPage %d', self.curPage))
|
||||||
if workingPage.title ~= '' then
|
if workingPage.title ~= '' then
|
||||||
self.sink:writeln(' — ' .. workingPage.title)
|
self.sink:writeln(' — ' .. workingPage.title)
|
||||||
|
@ -44,7 +51,7 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
self.sink:writeln('')
|
self.sink:writeln('')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.sink:write(buffer)
|
self.sink:write(buffer == '' and display or buffer)
|
||||||
end
|
end
|
||||||
function gh:input(c)
|
function gh:input(c)
|
||||||
-- command handling
|
-- command handling
|
||||||
|
@ -52,16 +59,26 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
command = true
|
command = true
|
||||||
end
|
end
|
||||||
if c == 'Escape' then
|
if c == 'Escape' then
|
||||||
|
if command then
|
||||||
command = false
|
command = false
|
||||||
buffer = ''
|
buffer = ''
|
||||||
goto update
|
else
|
||||||
|
if self.isSpecial then gh:special() end
|
||||||
|
end
|
||||||
elseif c == 'Backspace' then
|
elseif c == 'Backspace' then
|
||||||
buffer = buffer:sub(0, -2)
|
buffer = buffer:sub(0, -2)
|
||||||
|
if buffer == '' then
|
||||||
|
command = false
|
||||||
|
else
|
||||||
goto update
|
goto update
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if command then
|
if command then
|
||||||
buffer = buffer .. c
|
ansikit.showCursor()
|
||||||
|
if buffer:match '^:' then buffer = buffer .. c else buffer = c end
|
||||||
|
else
|
||||||
|
ansikit.hideCursor()
|
||||||
end
|
end
|
||||||
|
|
||||||
::update::
|
::update::
|
||||||
|
@ -70,9 +87,9 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
gh:resize()
|
gh:resize()
|
||||||
|
|
||||||
gh:keybind('Enter', function(self)
|
gh:keybind('Enter', function(self)
|
||||||
if self.isToc then
|
if self.isSpecial then
|
||||||
self:jump(self.tocPageIdx)
|
self:jump(self.specialPageIdx)
|
||||||
self:toc(true)
|
self:special(true)
|
||||||
else
|
else
|
||||||
if buffer:len() < 2 then return end
|
if buffer:len() < 2 then return end
|
||||||
|
|
||||||
|
@ -80,7 +97,7 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
local command = commands[splitBuf[1]:sub(2)]
|
local command = commands[splitBuf[1]:sub(2)]
|
||||||
if command then
|
if command then
|
||||||
table.remove(splitBuf, 1)
|
table.remove(splitBuf, 1)
|
||||||
command(splitBuf)
|
buffer = command(splitBuf) or ''
|
||||||
end
|
end
|
||||||
self:update()
|
self:update()
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,10 +27,10 @@ function Greenhouse:new(sink)
|
||||||
['Ctrl-Right'] = self.next,
|
['Ctrl-Right'] = self.next,
|
||||||
['Ctrl-N'] = function(self) self:toc(true) end,
|
['Ctrl-N'] = function(self) self:toc(true) end,
|
||||||
}
|
}
|
||||||
self.isToc = false
|
self.isSpecial = false
|
||||||
self.tocPage = nil
|
self.specialPage = nil
|
||||||
self.tocPageIdx = 1
|
self.specialPageIdx = 1
|
||||||
self.tocOffset = 1
|
self.specialOffset = 1
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
@ -47,9 +47,9 @@ end
|
||||||
function Greenhouse:draw()
|
function Greenhouse:draw()
|
||||||
local workingPage = self.pages[self.curPage]
|
local workingPage = self.pages[self.curPage]
|
||||||
local offset = self.offset
|
local offset = self.offset
|
||||||
if self.isToc then
|
if self.isSpecial then
|
||||||
offset = self.tocOffset
|
offset = self.specialOffset
|
||||||
workingPage = self.tocPage
|
workingPage = self.specialPage
|
||||||
end
|
end
|
||||||
|
|
||||||
local lines = workingPage.lines
|
local lines = workingPage.lines
|
||||||
|
@ -69,7 +69,7 @@ function Greenhouse:render()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:scroll(direction)
|
function Greenhouse:scroll(direction)
|
||||||
if self.isToc then
|
if self.isSpecial then
|
||||||
if direction == 'down' then
|
if direction == 'down' then
|
||||||
self:next(true)
|
self:next(true)
|
||||||
elseif direction == 'up' then
|
elseif direction == 'up' then
|
||||||
|
@ -92,24 +92,56 @@ end
|
||||||
|
|
||||||
function Greenhouse:update()
|
function Greenhouse:update()
|
||||||
self:resize()
|
self:resize()
|
||||||
if self.isToc then
|
if self.isSpecial then
|
||||||
self:toc()
|
self:special()
|
||||||
end
|
end
|
||||||
|
|
||||||
self:draw()
|
self:draw()
|
||||||
end
|
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()
|
function Greenhouse:resize()
|
||||||
local size = terminal.size()
|
local size = terminal.size()
|
||||||
self.region = size
|
self.region = size
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:next(toc)
|
function Greenhouse:next(special)
|
||||||
local oldCurrent = toc and self.tocPageIdx or self.curPage
|
local oldCurrent = special and self.specialPageIdx or self.curPage
|
||||||
local pageIdx = math.min(oldCurrent + 1, #self.pages)
|
local pageIdx = math.min(oldCurrent + 1, #self.pages)
|
||||||
|
|
||||||
if toc then
|
if special then
|
||||||
self.tocPageIdx = pageIdx
|
self.specialPageIdx = pageIdx
|
||||||
else
|
else
|
||||||
self.curPage = pageIdx
|
self.curPage = pageIdx
|
||||||
end
|
end
|
||||||
|
@ -120,12 +152,12 @@ function Greenhouse:next(toc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:previous(toc)
|
function Greenhouse:previous(special)
|
||||||
local oldCurrent = toc and self.tocPageIdx or self.curPage
|
local oldCurrent = special and self.specialPageIdx or self.curPage
|
||||||
local pageIdx = math.max(self.curPage - 1, 1)
|
local pageIdx = math.max(self.curPage - 1, 1)
|
||||||
|
|
||||||
if toc then
|
if special then
|
||||||
self.tocPageIdx = pageIdx
|
self.specialPageIdx = pageIdx
|
||||||
else
|
else
|
||||||
self.curPage = pageIdx
|
self.curPage = pageIdx
|
||||||
end
|
end
|
||||||
|
@ -148,34 +180,6 @@ function Greenhouse:keybind(key, callback)
|
||||||
self.keybinds[key] = callback
|
self.keybinds[key] = callback
|
||||||
end
|
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)
|
function Greenhouse:input(char)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -198,6 +202,7 @@ function Greenhouse:initUi()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
ansikit.screenAlt()
|
ansikit.screenAlt()
|
||||||
|
ansikit.hideCursor()
|
||||||
ansikit.clear(true)
|
ansikit.clear(true)
|
||||||
self:draw()
|
self:draw()
|
||||||
|
|
||||||
|
@ -251,6 +256,7 @@ function Greenhouse:initUi()
|
||||||
while not done do
|
while not done do
|
||||||
--
|
--
|
||||||
end
|
end
|
||||||
|
ansikit.showCursor()
|
||||||
ansikit.screenMain()
|
ansikit.screenMain()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue