feat: add table of contents to pager

another change in this commit is having a separate render
function for the greenhouse caller to handle ui

this means there isnt a hardcoded space at the bottom to
display command and status line
pull/240/head
sammyette 2023-07-09 22:38:34 -04:00
parent 713f24aa3e
commit 0bb97a6f3b
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 146 additions and 24 deletions

View File

@ -8,9 +8,45 @@ 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)
function gh:resize()
local size = terminal.size()
self.region = {
width = size.width,
height = size.height - 2
}
end
local oldDraw = gh.draw
function gh:draw()
oldDraw(self)
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isToc then
offset = self.tocOffset
workingPage = self.tocPage
end
self.sink:write(ansikit.getCSI((self.region.height + 2) - self.start.. ';1', 'H'))
if not self.isToc then
self.sink:write(string.format('\27[0mPage %d', self.curPage))
if workingPage.title ~= '' then
self.sink:writeln('' .. workingPage.title)
else
self.sink:writeln('')
end
end
self.sink:write(buffer)
end
gh:resize()
gh:keybind('Enter', function(self)
if self.isToc then
self:jump(self.tocPageIdx)
self:toc(true)
end
end)
if sinks['in'].pipe then if sinks['in'].pipe then
local page = Page(sinks['in']:readAll()) local page = Page('', sinks['in']:readAll())
gh:addPage(page) gh:addPage(page)
end end
@ -20,7 +56,7 @@ commander.register('greenhouse', function(args, sinks)
sinks.err:writeln(string.format('could not open file %s', name)) sinks.err:writeln(string.format('could not open file %s', name))
end end
local page = Page(f:read '*a') local page = Page(name, f:read '*a')
gh:addPage(page) gh:addPage(page)
end end

View File

@ -5,7 +5,9 @@
-- and flowerbook. -- and flowerbook.
local ansikit = require 'ansikit' local ansikit = require 'ansikit'
local lunacolors = require 'lunacolors'
local terminal = require 'terminal' local terminal = require 'terminal'
local Page = require 'nature.greenhouse.page'
local Object = require 'nature.object' local Object = require 'nature.object'
local Greenhouse = Object:extend() local Greenhouse = Object:extend()
@ -14,7 +16,7 @@ function Greenhouse:new(sink)
local size = terminal.size() local size = terminal.size()
self.region = size self.region = size
self.start = 1 self.start = 1
self.offset = 1 self.offset = 1 -- vertical text offset
self.sink = sink self.sink = sink
self.pages = {} self.pages = {}
self.curPage = 1 self.curPage = 1
@ -22,8 +24,13 @@ function Greenhouse:new(sink)
['Up'] = function(self) self:scroll 'up' end, ['Up'] = function(self) self:scroll 'up' end,
['Down'] = function(self) self:scroll 'down' end, ['Down'] = function(self) self:scroll 'down' end,
['Ctrl-Left'] = self.previous, ['Ctrl-Left'] = self.previous,
['Ctrl-Right'] = self.next ['Ctrl-Right'] = self.next,
['Ctrl-N'] = function(self) self:toc(true) end,
} }
self.isToc = false
self.tocPage = nil
self.tocPageIdx = 1
self.tocOffset = 1
return self return self
end end
@ -38,22 +45,39 @@ function Greenhouse:updateCurrentPage(text)
end end
function Greenhouse:draw() function Greenhouse:draw()
local lines = self.pages[self.curPage].lines local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isToc then
offset = self.tocOffset
workingPage = self.tocPage
end
local lines = workingPage.lines
self.sink:write(ansikit.getCSI(self.start .. ';1', 'H')) self.sink:write(ansikit.getCSI(self.start .. ';1', 'H'))
self.sink:write(ansikit.getCSI(2, 'J')) self.sink:write(ansikit.getCSI(2, 'J'))
-- the -2 negate is for the command and status line -- the -2 negate is for the command and status line
for i = self.offset, self.offset + (self.region.height - self.start) - 2 do for i = offset, offset + (self.region.height - self.start) do
if i > #lines then break end if i > #lines then break end
self.sink:writeln('\r' .. lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2)) self.sink:writeln('\r' .. lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
end end
self.sink:write '\r' self.sink:write '\r'
self:render()
end
self.sink:write(ansikit.getCSI(self.region.height - self.start.. ';1', 'H')) function Greenhouse:render()
self.sink:writeln(string.format('\27[0mPage %d', self.curPage))
end end
function Greenhouse:scroll(direction) function Greenhouse:scroll(direction)
if self.isToc then
if direction == 'down' then
self:next(true)
elseif direction == 'up' then
self:previous(true)
end
return
end
local lines = self.pages[self.curPage].lines local lines = self.pages[self.curPage].lines
local oldOffset = self.offset local oldOffset = self.offset
@ -67,34 +91,91 @@ function Greenhouse:scroll(direction)
end end
function Greenhouse:update() function Greenhouse:update()
local size = terminal.size() self:resize()
self.region = size if self.isToc then
self:toc()
end
self:draw() self:draw()
end end
function Greenhouse:next() function Greenhouse:resize()
local oldCurrent = self.curPage local size = terminal.size()
self.curPage = math.min(self.curPage + 1, #self.pages) self.region = size
if self.curPage ~= oldCurrent then end
function Greenhouse:next(toc)
local oldCurrent = toc and self.tocPageIdx or self.curPage
local pageIdx = math.min(oldCurrent + 1, #self.pages)
if toc then
self.tocPageIdx = pageIdx
else
self.curPage = pageIdx
end
if pageIdx ~= oldCurrent then
self.offset = 1 self.offset = 1
self:draw() self:update()
end end
end end
function Greenhouse:previous() function Greenhouse:previous(toc)
local oldCurrent = self.curPage local oldCurrent = toc and self.tocPageIdx or self.curPage
self.curPage = math.max(self.curPage - 1, 1) local pageIdx = math.max(self.curPage - 1, 1)
if self.curPage ~= oldCurrent then
self.offset = 1 if toc then
self:draw() self.tocPageIdx = pageIdx
else
self.curPage = pageIdx
end end
if pageIdx ~= oldCurrent then
self.offset = 1
self:update()
end
end
function Greenhouse:jump(idx)
if idx ~= self.curPage then
self.offset = 1
end
self.curPage = idx
self:update()
end end
function Greenhouse:keybind(key, callback) 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:initUi() function Greenhouse:initUi()
local ansikit = require 'ansikit' local ansikit = require 'ansikit'
local bait = require 'bait' local bait = require 'bait'

View File

@ -2,12 +2,17 @@ local Object = require 'nature.object'
local Page = Object:extend() local Page = Object:extend()
function Page:new(text) function Page:new(title, text)
self:setText(text) self:setText(text)
self.title = title or 'Page'
end end
function Page:setText(text) function Page:setText(text)
self.lines = string.split(text, '\n') self.lines = string.split(text, '\n')
end end
function Page:setTitle(title)
self.title = title
end
return Page return Page

View File

@ -155,10 +155,8 @@ func (rl *Instance) ReadChar() string {
case charCtrlG: return "Ctrl-G" case charCtrlG: return "Ctrl-G"
case charBackspace, charBackspace2: return "Backspace" case charBackspace, charBackspace2: return "Backspace"
case charTab: return "Tab" case charTab: return "Tab"
case charCtrlJ: return "Ctrl-J"
case charCtrlK: return "Ctrl-K" case charCtrlK: return "Ctrl-K"
case charCtrlL: return "Ctrl-L" case charCtrlL: return "Ctrl-L"
case charCtrlM: return "Ctrl-M"
case charCtrlN: return "Ctrl-N" case charCtrlN: return "Ctrl-N"
case charCtrlO: return "Ctrl-O" case charCtrlO: return "Ctrl-O"
case charCtrlP: return "Ctrl-P" case charCtrlP: return "Ctrl-P"
@ -172,6 +170,8 @@ func (rl *Instance) ReadChar() string {
case charCtrlX: return "Ctrl-X" case charCtrlX: return "Ctrl-X"
case charCtrlY: return "Ctrl-Y" case charCtrlY: return "Ctrl-Y"
case charCtrlZ: return "Ctrl-Z" case charCtrlZ: return "Ctrl-Z"
case '\r': fallthrough
case '\n': return "Enter"
case charEscape: case charEscape:
switch s { switch s {
case string(charEscape): return "Escape" case string(charEscape): return "Escape"