From 076118d2371abdcc2e79353236917bd91e66a87c Mon Sep 17 00:00:00 2001 From: sammyette Date: Tue, 11 Apr 2023 20:44:29 -0400 Subject: [PATCH] feat: make doc command use pager --- nature/commands/doc.lua | 7 ++- nature/commands/greenhouse.lua | 69 +---------------------------- nature/greenhouse/init.lua | 80 +++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 70 deletions(-) diff --git a/nature/commands/doc.lua b/nature/commands/doc.lua index d37e677..841f912 100644 --- a/nature/commands/doc.lua +++ b/nature/commands/doc.lua @@ -1,6 +1,8 @@ local commander = require 'commander' local fs = require 'fs' local lunacolors = require 'lunacolors' +local Greenhouse = require 'nature.greenhouse' +local Page = require 'nature.greenhouse.page' commander.register('doc', function(args, sinks) local moddocPath = hilbish.dataDir .. '/docs/' @@ -85,8 +87,9 @@ Available sections: ]] .. table.concat(modules, ', ') f:close() end + local gh = Greenhouse(sinks.out) local backtickOccurence = 0 - sinks.out:writeln(lunacolors.format(doc:gsub('`', function() + local page = Page(lunacolors.format(doc:gsub('`', function() backtickOccurence = backtickOccurence + 1 if backtickOccurence % 2 == 0 then return '{reset}' @@ -97,4 +100,6 @@ Available sections: ]] .. table.concat(modules, ', ') local signature = t:gsub('<.->(.-)', '{underline}%1'):gsub('\\', '<') return '{bold}{yellow}' .. signature .. '{reset}' end))) + gh:addPage(page) + gh:initUi() end) diff --git a/nature/commands/greenhouse.lua b/nature/commands/greenhouse.lua index 351b120..7b38d33 100644 --- a/nature/commands/greenhouse.lua +++ b/nature/commands/greenhouse.lua @@ -8,7 +8,6 @@ local Page = require 'nature.greenhouse.page' commander.register('greenhouse', function(args, sinks) local gh = Greenhouse(sinks.out) - local done = false if sinks['in'].pipe then local page = Page(sinks['in']:readAll()) @@ -25,71 +24,5 @@ commander.register('greenhouse', function(args, sinks) gh:addPage(page) end - bait.catch('signal.sigint', function() - done = true - end) - - bait.catch('signal.resize', function() - gh:update() - end) - - ansikit.screenAlt() - ansikit.clear(true) - gh:draw() - - hilbish.goro(function() - while not done do - local c = read() - if c == 3 then - done = true - end - - if c == 27 then - local c1 = read() - if c1 == 91 then - local c2 = read() - if c2 == 66 then -- arrow down - gh:scroll 'down' - elseif c2 == 65 then -- arrow up - gh:scroll 'up' - 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 - goto continue - end - print('\nchar:') - print(c) - - ::continue:: - end - end) - - while not done do - -- - end - ansikit.clear() - ansikit.screenMain() + gh:initUi() end) - -function read() - terminal.saveState() - terminal.setRaw() - local c = io.read(1) - - terminal.restoreState() - return c:byte() -end diff --git a/nature/greenhouse/init.lua b/nature/greenhouse/init.lua index 0b710c2..3cd4133 100644 --- a/nature/greenhouse/init.lua +++ b/nature/greenhouse/init.lua @@ -44,7 +44,7 @@ function Greenhouse:draw() 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)) + self.sink:writeln(string.format('\27[0mPage %d', self.curPage)) end function Greenhouse:scroll(direction) @@ -85,4 +85,82 @@ function Greenhouse:previous() end end +function Greenhouse:initUi() + local ansikit = require 'ansikit' + local bait = require 'bait' + local commander = require 'commander' + local hilbish = require 'hilbish' + local terminal = require 'terminal' + local Page = require 'nature.greenhouse.page' + local done = false + + bait.catch('signal.sigint', function() + ansikit.clear() + done = true + end) + + bait.catch('signal.resize', function() + self:update() + end) + + ansikit.screenAlt() + ansikit.clear(true) + self:draw() + + hilbish.goro(function() + while not done do + local c = read() + if c == 3 then + done = true + end + + if c == 27 then + local c1 = read() + if c1 == 91 then + local c2 = read() + if c2 == 66 then -- arrow down + self:scroll 'down' + elseif c2 == 65 then -- arrow up + self:scroll 'up' + 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 + self:next() + elseif c5 == 68 then + self:previous() + end + end + end + end + end + goto continue + end + print('\nchar:') + print(c) + + ::continue:: + end + end) + + while not done do + -- + end + ansikit.screenMain() +end + +function read() + terminal.saveState() + terminal.setRaw() + local c = io.read(1) + + terminal.restoreState() + return c:byte() +end + return Greenhouse