From 5b4b055c6cbb9532f42901310db5ba26a93a1b3b Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 11 Jul 2023 20:24:38 -0400 Subject: [PATCH] fix(greenhouse): set scroll boundary properly, fix text cutoff this means you can no longer scroll away the text until it isnt seen this commit also fixes text cutoff when using lunacolors (like in the doc command) --- nature/greenhouse/init.lua | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/nature/greenhouse/init.lua b/nature/greenhouse/init.lua index 7e0b2c1..5206089 100644 --- a/nature/greenhouse/init.lua +++ b/nature/greenhouse/init.lua @@ -44,6 +44,21 @@ function Greenhouse:updateCurrentPage(text) page:setText(text) end +local function sub(str, limit) + local overhead = 0 + local function addOverhead(s) + overhead = overhead + string.len(s) + end + + local s = str:gsub('\x1b%[%d+;%d+;%d+;%d+;%d+%w', addOverhead) + :gsub('\x1b%[%d+;%d+;%d+;%d+%w', addOverhead) + :gsub('\x1b%[%d+;%d+;%d+%w',addOverhead) + :gsub('\x1b%[%d+;%d+%w', addOverhead) + :gsub('\x1b%[%d+%w', addOverhead) + + return s:sub(0, limit + overhead) +end + function Greenhouse:draw() local workingPage = self.pages[self.curPage] local offset = self.offset @@ -56,10 +71,9 @@ function Greenhouse:draw() self.sink:write(ansikit.getCSI(self.start .. ';1', 'H')) self.sink:write(ansikit.getCSI(2, 'J')) - -- the -2 negate is for the command and status line - for i = offset, offset + (self.region.height - self.start) do + for i = offset, offset + (self.region.height - 1) do if i > #lines then break end - self.sink:writeln('\r' .. lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2)) + self.sink:writeln('\r' .. sub(lines[i]:gsub('\t', ' '), self.region.width - 2)) end self.sink:write '\r' self:render() @@ -82,7 +96,7 @@ function Greenhouse:scroll(direction) local oldOffset = self.offset if direction == 'down' then - self.offset = math.min(self.offset + 1, #lines) + self.offset = math.min(self.offset + 1, math.max(1, #lines - self.region.height + 1)) elseif direction == 'up' then self.offset = math.max(self.offset - 1, 1) end