feat(greenhouse): add implementation for horizontal scroll (leave unused)

pull/260/head
sammyette 2023-12-18 11:44:47 -04:00
parent 5fb8c88b5e
commit ec4962079f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 23 additions and 6 deletions

View File

@ -18,13 +18,20 @@ function Greenhouse:new(sink)
self.contents = nil -- or can be a table self.contents = nil -- or can be a table
self.start = 1 -- where to start drawing from (should replace with self.region.y) self.start = 1 -- where to start drawing from (should replace with self.region.y)
self.offset = 1 -- vertical text offset self.offset = 1 -- vertical text offset
self.horizOffset = 1
self.sink = sink self.sink = sink
self.pages = {} self.pages = {}
self.curPage = 1 self.curPage = 1
self.step = {
horizontal = 5,
vertical = 1
}
self.separator = '' self.separator = ''
self.keybinds = { self.keybinds = {
['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,
['Left'] = function(self) self:scroll 'left' end,
['Right'] = function(self) self:scroll 'right' 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, ['Ctrl-N'] = function(self) self:toc(true) end,
@ -52,7 +59,7 @@ function Greenhouse:updateCurrentPage(text)
page:setText(text) page:setText(text)
end end
local function sub(str, limit) local function sub(str, offset, limit)
local overhead = 0 local overhead = 0
local function addOverhead(s) local function addOverhead(s)
overhead = overhead + string.len(s) overhead = overhead + string.len(s)
@ -64,8 +71,8 @@ local function sub(str, limit)
:gsub('\x1b%[%d+;%d+%w', addOverhead) :gsub('\x1b%[%d+;%d+%w', addOverhead)
:gsub('\x1b%[%d+%w', addOverhead) :gsub('\x1b%[%d+%w', addOverhead)
return s:sub(0, utf8.offset(str, limit + overhead) or limit + overhead) return s:sub(offset, utf8.offset(str, limit + overhead) or limit + overhead)
--return s:sub(0, limit + overhead) --return s:sub(offset, limit + overhead)
end end
function Greenhouse:draw() function Greenhouse:draw()
@ -91,7 +98,7 @@ function Greenhouse:draw()
if i == offset + self.region.height - 1 then writer = self.sink.write end if i == offset + self.region.height - 1 then writer = self.sink.write end
local line = lines[i]:gsub('{separator}', function() return self.separator:rep(self.region.width - 1) end) local line = lines[i]:gsub('{separator}', function() return self.separator:rep(self.region.width - 1) end)
writer(self.sink, sub(line:gsub('\t', ' '), self.region.width)) writer(self.sink, sub(line:gsub('\t', ' '), self.horizOffset, self.region.width))
end end
writer(self.sink, '\27[0m') writer(self.sink, '\27[0m')
self:render() self:render()
@ -113,13 +120,23 @@ function Greenhouse:scroll(direction)
local lines = self.pages[self.curPage].lines local lines = self.pages[self.curPage].lines
local oldOffset = self.offset local oldOffset = self.offset
local oldHorizOffset = self.horizOffset
if direction == 'down' then if direction == 'down' then
self.offset = math.min(self.offset + 1, math.max(1, #lines - self.region.height)) self.offset = math.min(self.offset + self.step.vertical, math.max(1, #lines - self.region.height))
elseif direction == 'up' then elseif direction == 'up' then
self.offset = math.max(self.offset - 1, 1) self.offset = math.max(self.offset - self.step.vertical, 1)
end end
--[[
if direction == 'left' then
self.horizOffset = math.max(self.horizOffset - self.step.horizontal, 1)
elseif direction == 'right' then
self.horizOffset = self.horizOffset + self.step.horizontal
end
]]--
if self.offset ~= oldOffset then self:draw() end if self.offset ~= oldOffset then self:draw() end
if self.horizOffset ~= oldHorizOffset then self:draw() end
end end
function Greenhouse:update() function Greenhouse:update()