2023-02-18 20:57:27 +00:00
|
|
|
local ansikit = require 'ansikit'
|
|
|
|
local bait = require 'bait'
|
|
|
|
local commander = require 'commander'
|
|
|
|
local hilbish = require 'hilbish'
|
|
|
|
local terminal = require 'terminal'
|
|
|
|
local Greenhouse = require 'nature.greenhouse'
|
2023-02-19 20:53:39 +00:00
|
|
|
local Page = require 'nature.greenhouse.page'
|
2023-02-18 20:57:27 +00:00
|
|
|
|
|
|
|
commander.register('greenhouse', function(args, sinks)
|
2023-02-19 20:53:39 +00:00
|
|
|
local gh = Greenhouse(sinks.out)
|
2023-07-10 02:38:34 +00:00
|
|
|
function gh:resize()
|
|
|
|
local size = terminal.size()
|
|
|
|
self.region = {
|
|
|
|
width = size.width,
|
|
|
|
height = size.height - 2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
local oldDraw = gh.draw
|
|
|
|
function gh:draw()
|
|
|
|
oldDraw(self)
|
|
|
|
local workingPage = self.pages[self.curPage]
|
|
|
|
local offset = self.offset
|
|
|
|
if self.isToc then
|
|
|
|
offset = self.tocOffset
|
|
|
|
workingPage = self.tocPage
|
|
|
|
end
|
|
|
|
|
|
|
|
self.sink:write(ansikit.getCSI((self.region.height + 2) - self.start.. ';1', 'H'))
|
|
|
|
if not self.isToc then
|
|
|
|
self.sink:write(string.format('\27[0mPage %d', self.curPage))
|
|
|
|
if workingPage.title ~= '' then
|
|
|
|
self.sink:writeln(' — ' .. workingPage.title)
|
|
|
|
else
|
|
|
|
self.sink:writeln('')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.sink:write(buffer)
|
|
|
|
end
|
|
|
|
gh:resize()
|
|
|
|
|
|
|
|
gh:keybind('Enter', function(self)
|
|
|
|
if self.isToc then
|
|
|
|
self:jump(self.tocPageIdx)
|
|
|
|
self:toc(true)
|
|
|
|
end
|
|
|
|
end)
|
2023-02-18 20:57:27 +00:00
|
|
|
|
2023-03-25 22:12:51 +00:00
|
|
|
if sinks['in'].pipe then
|
2023-07-10 02:38:34 +00:00
|
|
|
local page = Page('', sinks['in']:readAll())
|
2023-03-25 22:12:51 +00:00
|
|
|
gh:addPage(page)
|
|
|
|
end
|
|
|
|
|
2023-02-19 20:53:39 +00:00
|
|
|
for _, name in ipairs(args) do
|
|
|
|
local f <close> = io.open(name, 'r')
|
|
|
|
if not f then
|
|
|
|
sinks.err:writeln(string.format('could not open file %s', name))
|
|
|
|
end
|
|
|
|
|
2023-07-10 02:38:34 +00:00
|
|
|
local page = Page(name, f:read '*a')
|
2023-02-19 20:53:39 +00:00
|
|
|
gh:addPage(page)
|
|
|
|
end
|
2023-02-18 22:58:16 +00:00
|
|
|
|
2023-04-12 00:44:29 +00:00
|
|
|
gh:initUi()
|
2023-02-18 20:57:27 +00:00
|
|
|
end)
|