Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hilbish native pager #240

Merged
merged 45 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e454ab0
feat: add greenhouse pager
TorchedSammy Feb 18, 2023
45eb3d1
fix(nature/greenhouse): draw pager body properly
TorchedSammy Feb 18, 2023
e368ba3
fix(nature/greenhouse): add limit to text offset for scrolling
TorchedSammy Feb 18, 2023
bb9a6fe
fix(nature/greenhouse): handle raw mode and term resize
TorchedSammy Feb 18, 2023
8b5b9b3
fix(nature/greenhouse): dont redraw if scroll pos stays the same
TorchedSammy Feb 18, 2023
179bec7
feat(nature/greenhouse): support setting multiple pages
TorchedSammy Feb 19, 2023
ec0692e
fix(nature/greenhouse): reset line offset when switching pages
TorchedSammy Feb 19, 2023
7531bbf
chore: merge from master
TorchedSammy Mar 25, 2023
d196799
feat(commands/greenhouse): support reading from pipes
TorchedSammy Mar 25, 2023
076118d
feat: make doc command use pager
TorchedSammy Apr 12, 2023
f5fe85e
chore: merge from master
TorchedSammy Apr 14, 2023
f9546e1
docs: update doc strings for sink read functions
TorchedSammy Apr 14, 2023
4a3ceed
chore: update branch
TorchedSammy Jul 9, 2023
b631563
feat: add getChar function to editor interface
TorchedSammy Jul 9, 2023
e0d59a3
ci: run docgen on prs and every branch
TorchedSammy Jul 9, 2023
d5e3185
ci: fix docs action
TorchedSammy Jul 9, 2023
47582e1
ci: stop using deprecated method of setting branch name
TorchedSammy Jul 9, 2023
99a121b
docs: [ci] generate new docs
TorchedSammy Jul 9, 2023
2dcae51
ci: remove duplicate run command
TorchedSammy Jul 9, 2023
e84a2fb
chore: merge from upstream
TorchedSammy Jul 9, 2023
157984e
ci: revert "ci: run docgen on prs and every branch"
TorchedSammy Jul 9, 2023
e013ecd
ci: fix branch name
TorchedSammy Jul 9, 2023
e4c3076
ci: add missing quote
TorchedSammy Jul 9, 2023
60f2672
feat: add more keys for readchar function
TorchedSammy Jul 10, 2023
713f24a
feat(greenhouse): add functions for custom keybinds
TorchedSammy Jul 10, 2023
0bb97a6
feat: add table of contents to pager
TorchedSammy Jul 10, 2023
8b672f5
feat(greenhouse): add command handling
TorchedSammy Jul 10, 2023
11da2c0
feat(greenhouse): add goto command
TorchedSammy Jul 10, 2023
4c6758c
chore: merge from master (update branch)
TorchedSammy Jul 10, 2023
1f5ab90
docs: update
TorchedSammy Jul 10, 2023
ade14a6
fix(greenhouse): fix toc disappearing on any key press
TorchedSammy Jul 10, 2023
9b39d5f
fix(commands/greenhouse): set nil page title
TorchedSammy Jul 12, 2023
5b4b055
fix(greenhouse): set scroll boundary properly, fix text cutoff
TorchedSammy Jul 12, 2023
48a06f8
fix: doc command not displaying correct subdocs when using shorthand …
TorchedSammy Jul 12, 2023
7d0c3f3
fix: set old moddocpath
TorchedSammy Jul 12, 2023
b9bb144
fix(greenhouse): print contents properly
TorchedSammy Jul 12, 2023
19bb05f
feat: pager improvements
TorchedSammy Jul 12, 2023
264043d
fix: show usage when no args are passed to greenhouse
TorchedSammy Oct 25, 2023
554fb00
fix(greenhouse): add enter bind to goto page on table of contents
TorchedSammy Oct 25, 2023
7288f85
feat(commands/doc): add subdocs as additional pages
TorchedSammy Oct 25, 2023
003100d
fix: dont concat subdocs list if on main page
TorchedSammy Oct 25, 2023
8fabdf4
fix: correct no files check to compensate for pipes
TorchedSammy Oct 25, 2023
d8a7a43
fix(commands/greenhouse): remove extra space in title, set pipe page …
TorchedSammy Oct 25, 2023
ae356f6
docs: add details to changelog
TorchedSammy Oct 25, 2023
e288826
chore: merge from master
TorchedSammy Oct 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
- `pipe` property to check if a sink with input is a pipe (like stdin)
- Add fuzzy search to history search (enable via `hilbish.opts.fuzzy = true`)
- Show indexes on cdr list
- Fix doc command not displaying correct subdocs when using shorthand api doc access (`doc api hilbish.jobs` as an example)
- `hilbish.messages` interface (details in [#219])
- `hilbish.notification` signal when a message/notification is sent
- `notifyJobFinish` opt to send a notification when background jobs are
completed.
- Allow numbered arg substitutions in aliases.
- Example: `hilbish.alias('hello', 'echo %1 says hello')` allows the user to run `hello hilbish`
which will output `hilbish says hello`.
- Greenhouse
- Greenhouse is a pager library and program. Basic usage is `greenhouse <file>`
- Using this also brings enhancements to the `doc` command like easy
navigation of neighboring doc files.

### Fixed
- Return the prefix when calling `hilbish.completions.call`
Expand Down
5 changes: 4 additions & 1 deletion docs/api/hilbish/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ A call with no argument will toggle the value.
Flush writes all buffered input to the sink.

#### read() -> string
Reads input from the sink.
Reads a liine of input from the sink.

#### readAll() -> string
Reads all input from the sink.

#### write(str)
Writes data to a sink.
Expand Down
4 changes: 4 additions & 0 deletions docs/api/hilbish/hilbish.editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Returns the text that is at the register.
### insert(text)
Inserts text into the line.

### getChar() -> string
Reads a keystroke from the user. This is in a format
of something like Ctrl-L..

### setVimRegister(register, text)
Sets the vim register at `register` to hold the passed text.

11 changes: 11 additions & 0 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func editorLoader(rtm *rt.Runtime) *rt.Table {
"setVimRegister": {editorSetRegister, 1, false},
"getVimRegister": {editorGetRegister, 2, false},
"getLine": {editorGetLine, 0, false},
"readChar": {editorReadChar, 0, false},
}

mod := rt.NewTable()
Expand Down Expand Up @@ -94,3 +95,13 @@ func editorGetLine(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {

return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
}

// #interface editor
// getChar() -> string
// Reads a keystroke from the user. This is in a format
// of something like Ctrl-L..
func editorReadChar(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
buf := lr.rl.ReadChar()

return c.PushingNext1(t.Runtime, rt.StringValue(string(buf))), nil
}
10 changes: 9 additions & 1 deletion emmyLuaDocs/hilbish.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function hilbish.editor.getVimRegister(register) end
--- Inserts text into the line.
function hilbish.editor.insert(text) end

--- Reads a keystroke from the user. This is in a format
--- of something like Ctrl-L..
function hilbish.editor.getChar() end

--- Sets the vim register at `register` to hold the passed text.
--- @param register string
--- @param text string
Expand Down Expand Up @@ -196,10 +200,14 @@ function hilbish:autoFlush(auto) end
--- Flush writes all buffered input to the sink.
function hilbish:flush() end

--- Reads input from the sink.
--- Reads a liine of input from the sink.
--- @returns string
function hilbish:read() end

--- Reads all input from the sink.
--- @returns string
function hilbish:readAll() end

--- Writes data to a sink.
function hilbish:write(str) end

Expand Down
150 changes: 103 additions & 47 deletions nature/commands/doc.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local ansikit = require 'ansikit'
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/'
Expand All @@ -9,11 +12,6 @@ commander.register('doc', function(args, sinks)
-- hilbish git
moddocPath = './docs/'
end
local apidocHeader = [[
# %s
{grayBg} {white}{italic}%s {reset}

]]

local modules = table.map(fs.readdir(moddocPath), function(f)
return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', '')))
Expand All @@ -25,25 +23,51 @@ to Hilbish.

Usage: doc <section> [subdoc]
Available sections: ]] .. table.concat(modules, ', ')
local f
local function handleYamlInfo(d)
local vals = {}
local docs = d

local valsStr = docs:match '%-%-%-\n([^%-%-%-]+)\n'
print(valsStr)
if valsStr then
docs = docs:sub(valsStr:len() + 10, #docs)

-- parse vals
local lines = string.split(valsStr, '\n')
for _, line in ipairs(lines) do
local key = line:match '(%w+): '
local val = line:match '^%w+: (.-)$'

if key then
vals[key] = val
end
end
end

--docs = docs:sub(1, #docs - 1)
return docs, vals
end

if #args > 0 then
local mod = args[1]

local f = io.open(moddocPath .. mod .. '.md', 'rb')
f = io.open(moddocPath .. mod .. '.md', 'rb')
local funcdocs = nil
local subdocName = args[2]
if not f then
-- assume subdir
-- dataDir/docs/<mod>/<mod>.md
moddocPath = moddocPath .. mod .. '/'
if not subdocName then
subdocName = '_index'
end
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
local oldmoddocPath = moddocPath
if not f then
f = io.open(moddocPath .. subdocName:match '%w+' .. '/' .. subdocName .. '.md', 'rb')
moddocPath = moddocPath .. subdocName:match '%w+' .. '/'
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
end
if not f then
moddocPath = moddocPath .. subdocName .. '/'
moddocPath = oldmoddocPath .. subdocName .. '/'
subdocName = args[3] or '_index'
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
end
Expand All @@ -52,49 +76,81 @@ Available sections: ]] .. table.concat(modules, ', ')
return 1
end
end
funcdocs = f:read '*a':gsub('-([%d]+)', '%1')
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' and f ~= 'index.md' end)
local subdocs = table.map(moddocs, function(fname)
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
end)
if #moddocs ~= 0 then
funcdocs = funcdocs .. '\nSubdocs: ' .. table.concat(subdocs, ', ')
end

local valsStr = funcdocs:match '%-%-%-\n([^%-%-%-]+)\n'
local vals = {}
if valsStr then
local _, endpos = funcdocs:find('---\n' .. valsStr .. '\n---\n\n', 1, true)
funcdocs = funcdocs:sub(endpos + 1, #funcdocs)
end

-- parse vals
local lines = string.split(valsStr, '\n')
for _, line in ipairs(lines) do
local key = line:match '(%w+): '
local val = line:match '^%w+: (.-)$'
local moddocs = table.filter(fs.readdir(moddocPath), function(f) return f ~= '_index.md' and f ~= 'index.md' end)
local subdocs = table.map(moddocs, function(fname)
return lunacolors.underline(lunacolors.blue(string.gsub(fname, '.md', '')))
end)

if key then
vals[key] = val
end
end
local gh = Greenhouse(sinks.out)
function gh:resize()
local size = terminal.size()
self.region = {
width = size.width,
height = size.height - 3
}
end
gh:resize()

function gh:render()
local workingPage = self.pages[self.curPage]
local offset = self.offset
if self.isSpecial then
offset = self.specialOffset
workingPage = self.specialPage
end
if mod == 'api' then
funcdocs = string.format(apidocHeader, vals.title, vals.description or 'no description.') .. funcdocs

self.sink:write(ansikit.getCSI(self.region.height + 2 .. ';1', 'H'))
if not self.isSpecial then
if args[1] == 'api' then
self.sink:writeln(lunacolors.reset(string.format('%s', workingPage.title)))
self.sink:write(lunacolors.format(string.format('{grayBg} ↳ {white}{italic}%s {reset}', workingPage.description or 'No description.')))
else
self.sink:write(lunacolors.reset(string.format('Viewing doc page %s', moddocPath)))
end
end
doc = funcdocs:sub(1, #funcdocs - 1)
f:close()
end

local backtickOccurence = 0
sinks.out:writeln(lunacolors.format(doc:gsub('`', function()
backtickOccurence = backtickOccurence + 1
if backtickOccurence % 2 == 0 then
return '{reset}'
else
return '{underline}{green}'
local function formatDocText(d)
return lunacolors.format(d:gsub('`', function()
backtickOccurence = backtickOccurence + 1
if backtickOccurence % 2 == 0 then
return '{reset}'
else
return '{underline}{green}'
end
end):gsub('\n#+.-\n', function(t)
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
return '{bold}{yellow}' .. signature .. '{reset}'
end))
end


local doc, vals = handleYamlInfo(#args == 0 and doc or formatDocText(f:read '*a':gsub('-([%d]+)', '%1')))
if #moddocs ~= 0 and f then
doc = doc .. '\nSubdocs: ' .. table.concat(subdocs, ', ') .. '\n\n'
end
if f then f:close() end

local page = Page(vals.title, doc)
page.description = vals.description
gh:addPage(page)

-- add subdoc pages
for _, sdName in ipairs(moddocs) do
local sdFile = fs.join(sdName, '_index.md')
if sdName:match '.md$' then
sdFile = sdName
end
end):gsub('\n#+.-\n', function(t)
local signature = t:gsub('<.->(.-)</.->', '{underline}%1'):gsub('\\', '<')
return '{bold}{yellow}' .. signature .. '{reset}'
end)))

local f = io.open(moddocPath .. sdFile, 'rb')
local doc, vals = handleYamlInfo(f:read '*a':gsub('-([%d]+)', '%1'))
local page = Page(vals.title, formatDocText(doc))
page.description = vals.description
gh:addPage(page)
end
ansikit.hideCursor()
gh:initUi()
end)
Loading
Loading