From df70082a81a70811edc9ef58903b046879bee003 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 16 Oct 2021 15:42:55 -0400 Subject: [PATCH] feat: add doc command (closes #76) the `doc` command is a way to see hilbish documentation from in the shell. for usage, just run the command --- preload.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/preload.lua b/preload.lua index 2f5e2f2..93704d7 100644 --- a/preload.lua +++ b/preload.lua @@ -44,6 +44,57 @@ commander.register('exit', function() os.exit(0) end) +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.]] + if #args > 0 then + local mod = '' + for i = 1, #args do + mod = mod .. tostring(args[i]) .. ' ' + end + mod = mod:gsub('^%s*(.-)%s*$', '%1') + + local f = io.open(moddocPath .. mod .. '.txt', 'rb') + if not f then + print('Could not find docs for module named ' .. mod .. '.') + return 1 + end + + local desc = (mod == 'global' and globalDesc or getmetatable(require(mod)).__doc) + local funcdocs = f:read '*a' + local backtickOccurence = 0 + print(desc .. '\n\n' .. lunacolors.format(funcdocs:sub(1, #funcdocs - 1):gsub('`', function() + backtickOccurence = backtickOccurence + 1 + if backtickOccurence % 2 == 0 then + return '{reset}' + else + return '{invert}' + end + end))) + f:close() + + return + end + local modules = fs.readdir(moddocPath) + + io.write [[ +Welcome to Hilbish's doc tool! Here you can find documentation for builtin +functions and other things. + +Usage: doc + +Available modules: ]] + + local mods = '' + for i = 1, #modules do + mods = mods .. tostring(modules[i]):gsub('.txt', '') .. ', ' + end + print(mods) + + return +end) + do local virt_G = { }