feat: add in shell documentation for module properties

dev
TorchedSammy 2021-11-22 18:59:28 -05:00
parent 538ba3547f
commit 43ddab699f
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 49 additions and 16 deletions

View File

@ -32,13 +32,13 @@ func HilbishLoader(L *lua.LState) int {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
} }
L.SetField(mod, "ver", lua.LString(version)) util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
L.SetField(mod, "user", lua.LString(username)) util.SetField(L, mod, "user", lua.LString(username), "Username of user")
L.SetField(mod, "host", lua.LString(host)) util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
L.SetField(mod, "home", lua.LString(homedir)) util.SetField(L, mod, "home", lua.LString(homedir), "Home directory of the user")
L.SetField(mod, "dataDir", lua.LString(dataDir)) util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files")
L.SetField(mod, "interactive", lua.LBool(interactive)) util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
L.SetField(mod, "login", lua.LBool(interactive)) util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
xdg := L.NewTable() xdg := L.NewTable()
L.SetField(xdg, "config", lua.LString(confDir)) L.SetField(xdg, "config", lua.LString(confDir))

View File

@ -49,6 +49,15 @@ commander.register('doc', function(args)
local moddocPath = hilbish.dataDir .. '/docs/' local moddocPath = hilbish.dataDir .. '/docs/'
local globalDesc = [[ local globalDesc = [[
These are the global Hilbish functions that are always available and not part of a module.]] These are the global Hilbish functions that are always available and not part of a module.]]
local modDocFormat = [[
%s
# Variables
%s
# Functions
]]
if #args > 0 then if #args > 0 then
local mod = args[1] local mod = args[1]
@ -82,18 +91,24 @@ These are the global Hilbish functions that are always available and not part of
end end
local desc = '' local desc = ''
local ok = pcall(require, mod) local ok = pcall(require, mod)
if ok then
desc = (mod == 'global' and globalDesc or getmetatable(require(mod)).__doc) .. '\n\n'
end
local backtickOccurence = 0 local backtickOccurence = 0
print(desc .. lunacolors.format(funcdocs:sub(1, #funcdocs - 1):gsub('`', function() local formattedFuncs = lunacolors.format(funcdocs:sub(1, #funcdocs - 1):gsub('`', function()
backtickOccurence = backtickOccurence + 1 backtickOccurence = backtickOccurence + 1
if backtickOccurence % 2 == 0 then if backtickOccurence % 2 == 0 then
return '{reset}' return '{reset}'
else else
return '{underline}{green}' return '{underline}{green}'
end end
end))) end))
if ok then
local modmt = getmetatable(require(mod))
local props = table.map(modmt.__docProp, function(v, k)
return lunacolors.underline(lunacolors.blue(k)) .. ' > ' .. v
end)
desc = string.format(modDocFormat, (mod == 'global' and globalDesc or modmt.__doc), table.concat(props, "\n"))
end
print(desc .. formattedFuncs)
f:close() f:close()
return return

1
rl.go
View File

@ -8,7 +8,6 @@ package main
// this is normal readline // this is normal readline
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"

View File

@ -1,10 +1,29 @@
package util package util
import "github.com/yuin/gopher-lua" import "github.com/yuin/gopher-lua"
import "fmt"
// Document adds a documentation string to a module.
// It is accessible via the __doc metatable.
func Document(L *lua.LState, module lua.LValue, doc string) { func Document(L *lua.LState, module lua.LValue, doc string) {
mt := L.NewTable() mt := L.GetMetatable(module)
L.SetField(mt, "__doc", lua.LString(doc)) if mt == lua.LNil {
mt = L.NewTable()
docProp := L.NewTable()
L.SetField(mt, "__docProp", docProp)
L.SetMetatable(module, mt) L.SetMetatable(module, mt)
}
L.SetField(mt, "__doc", lua.LString(doc))
}
// SetField sets a field in a table, adding docs for it.
// It is accessible via the __docProp metatable. It is a table of the names of the fields.
func SetField(L *lua.LState, module lua.LValue, field string, value lua.LValue, doc string) {
mt := L.GetMetatable(module)
docProp := L.GetTable(mt, lua.LString("__docProp"))
fmt.Println("docProp", docProp)
L.SetField(docProp, field, lua.LString(doc))
L.SetField(module, field, value)
} }