feat(nature/greenhouse): support setting multiple pages

pull/240/head
sammyette 2023-02-19 16:53:39 -04:00
parent 8b5b9b3c1c
commit 179bec7ae5
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 72 additions and 14 deletions

View File

@ -4,17 +4,21 @@ local commander = require 'commander'
local hilbish = require 'hilbish'
local terminal = require 'terminal'
local Greenhouse = require 'nature.greenhouse'
local Page = require 'nature.greenhouse.page'
commander.register('greenhouse', function(args, sinks)
local fname = args[1]
local gh = Greenhouse(sinks.out)
local done = false
local f <close> = io.open(fname, 'r')
for _, name in ipairs(args) do
local f <close> = io.open(name, 'r')
if not f then
sinks.err:writeln(string.format('could not open file %s', fname))
sinks.err:writeln(string.format('could not open file %s', name))
end
local gh = Greenhouse(sinks.out)
gh:setText(f:read '*a')
local page = Page(f:read '*a')
gh:addPage(page)
end
bait.catch('signal.sigint', function()
done = true
@ -44,6 +48,21 @@ commander.register('greenhouse', function(args, sinks)
elseif c2 == 65 then -- arrow up
gh:scroll 'up'
end
if c2 == 49 then
local c3 = read()
if c3 == 59 then
local c4 = read()
if c4 == 53 then
local c5 = read()
if c5 == 67 then
gh:next()
elseif c5 == 68 then
gh:previous()
end
end
end
end
end
goto continue
end

View File

@ -16,28 +16,43 @@ function Greenhouse:new(sink)
self.start = 1
self.offset = 1
self.sink = sink
self.pages = {}
self.curPage = 1
return self
end
function Greenhouse:setText(text)
self.lines = string.split(text, '\n')
function Greenhouse:addPage(page)
table.insert(self.pages, page)
end
function Greenhouse:updateCurrentPage(text)
local page = self.pages[self.curPage]
page:setText(text)
end
function Greenhouse:draw()
local lines = self.pages[self.curPage].lines
self.sink:write(ansikit.getCSI(self.start .. ';1', 'H'))
self.sink:write(ansikit.getCSI(2, 'J'))
for i = self.offset, self.offset + (self.region.height - self.start) - 1 do
self.sink:write(ansikit.getCSI(2, 'K'))
self.sink:writeln('\r' .. self.lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
-- the -2 negate is for the command and status line
for i = self.offset, self.offset + (self.region.height - self.start) - 2 do
if i > #lines then break end
self.sink:writeln('\r' .. lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
end
self.sink:write '\r'
self.sink:write(ansikit.getCSI(self.region.height - self.start.. ';1', 'H'))
self.sink:writeln(string.format('Page %d', self.curPage))
end
function Greenhouse:scroll(direction)
local lines = self.pages[self.curPage].lines
local oldOffset = self.offset
if direction == 'down' then
self.offset = math.min(self.offset + 1, #self.lines)
self.offset = math.min(self.offset + 1, #lines)
elseif direction == 'up' then
self.offset = math.max(self.offset - 1, 1)
end
@ -52,4 +67,15 @@ function Greenhouse:update()
self:draw()
end
function Greenhouse:next()
self.curPage = math.min(self.curPage + 1, #self.pages)
self.sink:write(ansikit.getCSI(2, 'J'))
self:draw()
end
function Greenhouse:previous()
self.curPage = math.max(self.curPage - 1, 1)
self:draw()
end
return Greenhouse

View File

@ -0,0 +1,13 @@
local Object = require 'nature.object'
local Page = Object:extend()
function Page:new(text)
self:setText(text)
end
function Page:setText(text)
self.lines = string.split(text, '\n')
end
return Page