feat: pager improvements

- make use of full terminal space properly in doc and greenhouse
commands
- add some info in doc command like currently viewed doc
and description at the bottom instead of top of doc (this makes it persistent)
- add contents function to Greenhouse to allow showing custom contents
in the table of contents (this will be used for lazy loading in the future)
pull/240/head
sammyette 2023-07-11 22:29:15 -04:00
parent b9bb14497f
commit 19bb05f001
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 61 additions and 25 deletions

View File

@ -1,3 +1,4 @@
local ansikit = require 'ansikit'
local commander = require 'commander'
local fs = require 'fs'
local lunacolors = require 'lunacolors'
@ -11,11 +12,6 @@ commander.register('doc', function(args, sinks)
-- hilbish git
moddocPath = './docs/'
end
local apidocHeader = [[
# %s
{grayBg} {white}{italic}%s {reset}
]]
local modules = table.map(fs.readdir(moddocPath), function(f)
return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', '')))
@ -27,6 +23,8 @@ to Hilbish.
Usage: doc <section> [subdoc]
Available sections: ]] .. table.concat(modules, ', ')
local vals = {}
if #args > 0 then
local mod = args[1]
@ -60,11 +58,10 @@ Available sections: ]] .. table.concat(modules, ', ')
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
end)
if #moddocs ~= 0 then
funcdocs = funcdocs .. '\nSubdocs: ' .. table.concat(subdocs, ', ')
funcdocs = funcdocs .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end
local valsStr = funcdocs:match '%-%-%-\n([^%-%-%-]+)\n'
local vals = {}
if valsStr then
local _, endpos = funcdocs:find('---\n' .. valsStr .. '\n---\n\n', 1, true)
funcdocs = funcdocs:sub(endpos + 1, #funcdocs)
@ -80,14 +77,38 @@ Available sections: ]] .. table.concat(modules, ', ')
end
end
end
if mod == 'api' then
funcdocs = string.format(apidocHeader, vals.title, vals.description or 'no description.') .. funcdocs
end
doc = funcdocs:sub(1, #funcdocs - 1)
f:close()
end
local gh = Greenhouse(sinks.out)
function gh:resize()
local size = terminal.size()
self.region = {
width = size.width,
height = size.height - 3
}
end
gh:resize()
function gh:render()
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isSpecial then
offset = self.specialOffset
workingPage = self.specialPage
end
self.sink:write(ansikit.getCSI(self.region.height + 2 .. ';1', 'H'))
if not self.isSpecial then
if args[1] == 'api' then
self.sink:writeln(lunacolors.reset(string.format('%s', vals.title)))
self.sink:write(lunacolors.format(string.format('{grayBg} ↳ {white}{italic}%s {reset}', vals.description)))
else
self.sink:write(lunacolors.reset(string.format('Viewing doc page %s', moddocPath)))
end
end
end
local backtickOccurence = 0
local page = Page(nil, lunacolors.format(doc:gsub('`', function()
backtickOccurence = backtickOccurence + 1
@ -101,5 +122,6 @@ Available sections: ]] .. table.concat(modules, ', ')
return '{bold}{yellow}' .. signature .. '{reset}'
end)))
gh:addPage(page)
ansikit.hideCursor()
gh:initUi()
end)

View File

@ -32,9 +32,7 @@ commander.register('greenhouse', function(args, sinks)
}
end
local oldDraw = gh.draw
function gh:draw()
oldDraw(self)
function gh:render()
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isSpecial then
@ -42,7 +40,7 @@ commander.register('greenhouse', function(args, sinks)
workingPage = self.specialPage
end
self.sink:write(ansikit.getCSI((self.region.height + 2) - self.start.. ';1', 'H'))
self.sink:write(ansikit.getCSI(self.region.height + 1 .. ';1', 'H'))
if not self.isSpecial then
self.sink:write(string.format('\27[0mPage %d', self.curPage))
if workingPage.title ~= '' then

View File

@ -15,7 +15,8 @@ local Greenhouse = Object:extend()
function Greenhouse:new(sink)
local size = terminal.size()
self.region = size
self.start = 1
self.contents = nil -- or can be a table
self.start = 1 -- where to start drawing from (should replace with self.region.y)
self.offset = 1 -- vertical text offset
self.sink = sink
self.pages = {}
@ -131,6 +132,9 @@ end
function Greenhouse:updateSpecial()
end
function Greenhouse:contents()
end
function Greenhouse:toc(toggle)
if not self.isSpecial then
self.specialPageIdx = self.curPage
@ -143,17 +147,29 @@ function Greenhouse:toc(toggle)
]], 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
local contents = self:contents()
if contents then
for i, c in ipairs(contents) do
local title = c.title
if c.active then
title = lunacolors.invert(title)
end
tocText = tocText .. title .. '\n'
tocText = tocText .. title .. '\n'
end
else
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
end
self.specialPage = Page('TOC', tocText)
function self:updateSpecial()