From 43ddab699fc04df13e1bdc36254f83c084ed976f Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 22 Nov 2021 18:59:28 -0500 Subject: [PATCH] feat: add in shell documentation for module properties --- hilbish.go | 14 +++++++------- preload.lua | 25 ++++++++++++++++++++----- rl.go | 1 - util/util.go | 25 ++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/hilbish.go b/hilbish.go index eef61c9..fed1d17 100644 --- a/hilbish.go +++ b/hilbish.go @@ -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)) diff --git a/preload.lua b/preload.lua index af47631..ce3f6b0 100644 --- a/preload.lua +++ b/preload.lua @@ -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 diff --git a/rl.go b/rl.go index b5f821e..f040c37 100644 --- a/rl.go +++ b/rl.go @@ -8,7 +8,6 @@ package main // this is normal readline import ( - "fmt" "os" "path/filepath" "strings" diff --git a/util/util.go b/util/util.go index a0ed738..c26d514 100644 --- a/util/util.go +++ b/util/util.go @@ -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.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) }