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 03:07:32 +00:00
|
|
|
|
|
|
|
local buffer = ''
|
2023-07-10 03:39:11 +00:00
|
|
|
local display = ''
|
2023-07-10 03:07:32 +00:00
|
|
|
local command = false
|
|
|
|
local commands = {
|
|
|
|
q = function()
|
|
|
|
gh.keybinds['Ctrl-D'](gh)
|
2023-07-10 03:39:11 +00:00
|
|
|
end,
|
|
|
|
['goto'] = function(args)
|
|
|
|
if not args[1] then
|
|
|
|
return 'nuh uh'
|
|
|
|
end
|
|
|
|
gh:jump(tonumber(args[1]))
|
2023-07-10 03:07:32 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
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
|
2023-07-10 03:07:32 +00:00
|
|
|
|
2023-07-12 02:29:15 +00:00
|
|
|
function gh:render()
|
2023-07-10 02:38:34 +00:00
|
|
|
local workingPage = self.pages[self.curPage]
|
|
|
|
local offset = self.offset
|
2023-07-10 03:39:11 +00:00
|
|
|
if self.isSpecial then
|
|
|
|
offset = self.specialOffset
|
|
|
|
workingPage = self.specialPage
|
2023-07-10 02:38:34 +00:00
|
|
|
end
|
|
|
|
|
2023-07-12 02:29:15 +00:00
|
|
|
self.sink:write(ansikit.getCSI(self.region.height + 1 .. ';1', 'H'))
|
2023-07-10 03:39:11 +00:00
|
|
|
if not self.isSpecial then
|
2023-07-10 02:38:34 +00:00
|
|
|
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
|
2023-07-10 03:39:11 +00:00
|
|
|
self.sink:write(buffer == '' and display or buffer)
|
2023-07-10 02:38:34 +00:00
|
|
|
end
|
2023-07-10 03:07:32 +00:00
|
|
|
function gh:input(c)
|
|
|
|
-- command handling
|
|
|
|
if c == ':' and not command then
|
|
|
|
command = true
|
|
|
|
end
|
|
|
|
if c == 'Escape' then
|
2023-07-10 03:39:11 +00:00
|
|
|
if command then
|
|
|
|
command = false
|
|
|
|
buffer = ''
|
|
|
|
else
|
|
|
|
if self.isSpecial then gh:special() end
|
|
|
|
end
|
2023-07-10 03:07:32 +00:00
|
|
|
elseif c == 'Backspace' then
|
|
|
|
buffer = buffer:sub(0, -2)
|
2023-07-10 03:39:11 +00:00
|
|
|
if buffer == '' then
|
|
|
|
command = false
|
|
|
|
else
|
|
|
|
goto update
|
|
|
|
end
|
2023-07-10 03:07:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if command then
|
2023-07-10 03:39:11 +00:00
|
|
|
ansikit.showCursor()
|
|
|
|
if buffer:match '^:' then buffer = buffer .. c else buffer = c end
|
|
|
|
else
|
|
|
|
ansikit.hideCursor()
|
2023-07-10 03:07:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
::update::
|
|
|
|
gh:update()
|
|
|
|
end
|
2023-07-10 02:38:34 +00:00
|
|
|
gh:resize()
|
|
|
|
|
|
|
|
gh:keybind('Enter', function(self)
|
2023-07-10 03:39:11 +00:00
|
|
|
if self.isSpecial then
|
|
|
|
self:jump(self.specialPageIdx)
|
2023-07-10 22:00:49 +00:00
|
|
|
self:special(false)
|
2023-07-10 03:07:32 +00:00
|
|
|
else
|
|
|
|
if buffer:len() < 2 then return end
|
|
|
|
|
|
|
|
local splitBuf = string.split(buffer, " ")
|
|
|
|
local command = commands[splitBuf[1]:sub(2)]
|
|
|
|
if command then
|
|
|
|
table.remove(splitBuf, 1)
|
2023-07-10 03:39:11 +00:00
|
|
|
buffer = command(splitBuf) or ''
|
2023-07-10 03:07:32 +00:00
|
|
|
end
|
|
|
|
self:update()
|
2023-07-10 02:38:34 +00:00
|
|
|
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-07-10 22:00:49 +00:00
|
|
|
ansikit.hideCursor()
|
2023-04-12 00:44:29 +00:00
|
|
|
gh:initUi()
|
2023-02-18 20:57:27 +00:00
|
|
|
end)
|