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
}
L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(username))
L.SetField(mod, "host", lua.LString(host))
L.SetField(mod, "home", lua.LString(homedir))
L.SetField(mod, "dataDir", lua.LString(dataDir))
L.SetField(mod, "interactive", lua.LBool(interactive))
L.SetField(mod, "login", lua.LBool(interactive))
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
util.SetField(L, mod, "user", lua.LString(username), "Username of user")
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
util.SetField(L, mod, "home", lua.LString(homedir), "Home directory of the user")
util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files")
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
xdg := L.NewTable()
L.SetField(xdg, "config", lua.LString(confDir))

View File

@ -49,6 +49,15 @@ commander.register('doc', function(args)
local moddocPath = hilbish.dataDir .. '/docs/'
local globalDesc = [[
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
local mod = args[1]
@ -82,18 +91,24 @@ These are the global Hilbish functions that are always available and not part of
end
local desc = ''
local ok = pcall(require, mod)
if ok then
desc = (mod == 'global' and globalDesc or getmetatable(require(mod)).__doc) .. '\n\n'
end
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
if backtickOccurence % 2 == 0 then
return '{reset}'
else
return '{underline}{green}'
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()
return

1
rl.go
View File

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

View File

@ -1,10 +1,29 @@
package util
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) {
mt := L.NewTable()
L.SetField(mt, "__doc", lua.LString(doc))
mt := L.GetMetatable(module)
if mt == lua.LNil {
mt = L.NewTable()
docProp := L.NewTable()
L.SetField(mt, "__docProp", docProp)
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)
}