Skip to content

Commit 80deee9

Browse files
committed
fix(greenhouse): set scroll boundary properly
this means you can no longer scroll away the text until it isnt seen
1 parent 9b39d5f commit 80deee9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

nature/greenhouse/init.lua

+18-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ function Greenhouse:updateCurrentPage(text)
4444
page:setText(text)
4545
end
4646

47+
local function sub(str, limit)
48+
local overhead = 0
49+
local function addOverhead(s)
50+
overhead = overhead + string.len(s)
51+
end
52+
53+
local s = str:gsub('\x1b%[%d+;%d+;%d+;%d+;%d+%w', addOverhead)
54+
:gsub('\x1b%[%d+;%d+;%d+;%d+%w', addOverhead)
55+
:gsub('\x1b%[%d+;%d+;%d+%w',addOverhead)
56+
:gsub('\x1b%[%d+;%d+%w', addOverhead)
57+
:gsub('\x1b%[%d+%w', addOverhead)
58+
59+
return s:sub(0, limit + overhead)
60+
end
61+
4762
function Greenhouse:draw()
4863
local workingPage = self.pages[self.curPage]
4964
local offset = self.offset
@@ -56,10 +71,9 @@ function Greenhouse:draw()
5671
self.sink:write(ansikit.getCSI(self.start .. ';1', 'H'))
5772
self.sink:write(ansikit.getCSI(2, 'J'))
5873

59-
-- the -2 negate is for the command and status line
60-
for i = offset, offset + (self.region.height - self.start) do
74+
for i = offset, offset + (self.region.height - 1) do
6175
if i > #lines then break end
62-
self.sink:writeln('\r' .. lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
76+
self.sink:writeln('\r' .. sub(lines[i]:gsub('\t', ' '), self.region.width - 2))
6377
end
6478
self.sink:write '\r'
6579
self:render()
@@ -82,7 +96,7 @@ function Greenhouse:scroll(direction)
8296

8397
local oldOffset = self.offset
8498
if direction == 'down' then
85-
self.offset = math.min(self.offset + 1, #lines)
99+
self.offset = math.min(self.offset + 1, math.max(1, #lines - self.region.height + 1))
86100
elseif direction == 'up' then
87101
self.offset = math.max(self.offset - 1, 1)
88102
end

0 commit comments

Comments
 (0)