mirror of https://github.com/Hilbis/Hilbish
feat(nature/greenhouse): support setting multiple pages
parent
8b5b9b3c1c
commit
179bec7ae5
|
@ -4,17 +4,21 @@ local commander = require 'commander'
|
||||||
local hilbish = require 'hilbish'
|
local hilbish = require 'hilbish'
|
||||||
local terminal = require 'terminal'
|
local terminal = require 'terminal'
|
||||||
local Greenhouse = require 'nature.greenhouse'
|
local Greenhouse = require 'nature.greenhouse'
|
||||||
|
local Page = require 'nature.greenhouse.page'
|
||||||
|
|
||||||
commander.register('greenhouse', function(args, sinks)
|
commander.register('greenhouse', function(args, sinks)
|
||||||
local fname = args[1]
|
local gh = Greenhouse(sinks.out)
|
||||||
local done = false
|
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
|
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
|
end
|
||||||
|
|
||||||
local gh = Greenhouse(sinks.out)
|
local page = Page(f:read '*a')
|
||||||
gh:setText(f:read '*a')
|
gh:addPage(page)
|
||||||
|
end
|
||||||
|
|
||||||
bait.catch('signal.sigint', function()
|
bait.catch('signal.sigint', function()
|
||||||
done = true
|
done = true
|
||||||
|
@ -44,6 +48,21 @@ commander.register('greenhouse', function(args, sinks)
|
||||||
elseif c2 == 65 then -- arrow up
|
elseif c2 == 65 then -- arrow up
|
||||||
gh:scroll 'up'
|
gh:scroll 'up'
|
||||||
end
|
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
|
end
|
||||||
goto continue
|
goto continue
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,28 +16,43 @@ function Greenhouse:new(sink)
|
||||||
self.start = 1
|
self.start = 1
|
||||||
self.offset = 1
|
self.offset = 1
|
||||||
self.sink = sink
|
self.sink = sink
|
||||||
|
self.pages = {}
|
||||||
|
self.curPage = 1
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:setText(text)
|
function Greenhouse:addPage(page)
|
||||||
self.lines = string.split(text, '\n')
|
table.insert(self.pages, page)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Greenhouse:updateCurrentPage(text)
|
||||||
|
local page = self.pages[self.curPage]
|
||||||
|
page:setText(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:draw()
|
function Greenhouse:draw()
|
||||||
|
local lines = self.pages[self.curPage].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'))
|
||||||
|
|
||||||
for i = self.offset, self.offset + (self.region.height - self.start) - 1 do
|
-- the -2 negate is for the command and status line
|
||||||
self.sink:write(ansikit.getCSI(2, 'K'))
|
for i = self.offset, self.offset + (self.region.height - self.start) - 2 do
|
||||||
self.sink:writeln('\r' .. self.lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
|
if i > #lines then break end
|
||||||
|
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.sink:write(ansikit.getCSI(self.region.height - self.start.. ';1', 'H'))
|
||||||
|
self.sink:writeln(string.format('Page %d', self.curPage))
|
||||||
end
|
end
|
||||||
|
|
||||||
function Greenhouse:scroll(direction)
|
function Greenhouse:scroll(direction)
|
||||||
|
local lines = self.pages[self.curPage].lines
|
||||||
|
|
||||||
local oldOffset = self.offset
|
local oldOffset = self.offset
|
||||||
if direction == 'down' then
|
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
|
elseif direction == 'up' then
|
||||||
self.offset = math.max(self.offset - 1, 1)
|
self.offset = math.max(self.offset - 1, 1)
|
||||||
end
|
end
|
||||||
|
@ -52,4 +67,15 @@ function Greenhouse:update()
|
||||||
self:draw()
|
self:draw()
|
||||||
end
|
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
|
return Greenhouse
|
|
@ -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
|
Loading…
Reference in New Issue