fix(nature/greenhouse): handle raw mode and term resize

pull/240/head
sammyette 2023-02-18 18:58:16 -04:00
parent e368ba3e0a
commit bb9a6fe39e
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 15 additions and 4 deletions

View File

@ -13,12 +13,16 @@ commander.register('greenhouse', function(args, sinks)
sinks.err:writeln(string.format('could not open file %s', fname)) sinks.err:writeln(string.format('could not open file %s', fname))
end end
local gh = Greenhouse(sinks.out)
gh:setText(f:read '*a')
bait.catch('signal.sigint', function() bait.catch('signal.sigint', function()
done = true done = true
end) end)
local gh = Greenhouse(sinks.out) bait.catch('signal.resize', function()
gh:setText(f:read '*a') gh:update()
end)
ansikit.screenAlt() ansikit.screenAlt()
ansikit.clear(true) ansikit.clear(true)

View File

@ -1,4 +1,4 @@
-- Greenhouse is a simple text scrolling handler for terminal program. -- Greenhouse is a simple text scrolling handler for terminal programs.
-- The idea is that it can be set a region to do its scrolling and paging -- The idea is that it can be set a region to do its scrolling and paging
-- job and then the user can draw whatever outside it. -- job and then the user can draw whatever outside it.
-- This reduces code duplication for the message viewer -- This reduces code duplication for the message viewer
@ -29,8 +29,9 @@ function Greenhouse:draw()
for i = self.offset, self.offset + (self.region.height - self.start) - 1 do for i = self.offset, self.offset + (self.region.height - self.start) - 1 do
self.sink:write(ansikit.getCSI(2, 'K')) self.sink:write(ansikit.getCSI(2, 'K'))
self.sink:writeln(self.lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2)) self.sink:writeln('\r' .. self.lines[i]:gsub('\t', ' '):sub(0, self.region.width - 2))
end end
self.sink:write '\r'
end end
function Greenhouse:scroll(direction) function Greenhouse:scroll(direction)
@ -39,6 +40,12 @@ function Greenhouse:scroll(direction)
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
end
function Greenhouse:update()
local size = terminal.size()
self.region = size
self:draw() self:draw()
end end