mikematt1616

Help.lua a help system for LuaRT

Feb 11th, 2021 (edited)
48
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local console = require("console")
  2. local sys = require("sys")
  3.  
  4. local helptext = {}
  5. local explanation =
  6. [[For more detail about a component of {{name}} listed above named xxx, use the code
  7.  
  8.     local help = require("help")
  9.     local text = help("{{name}}.xxx")
  10.    
  11. in your program to get a string containing the detailed help,
  12. or if using the interactive help console type {{name}}.xxx at the prompt.]]
  13.  
  14. local thisfile = "C:\\luaRT\\lib\\help.lua"
  15.  
  16.  
  17.  
  18. local cons = {}
  19.  
  20. local path = {}
  21. for p in (package.path..";"):gmatch("(.-);") do
  22.     p = p:gsub("%.([w]?lua)", ".help"):gsub("%.dll", ".help")
  23.     if p ~= "" and not path[p] then
  24.         table.insert(path, p)
  25.         path[p] = true
  26.     end
  27. end
  28. for cp in (package.cpath..";"):gmatch("(.-);") do
  29.     cp = cp:gsub("%.([w]?lua)", ".help"):gsub("%.dll", ".help")
  30.     if cp ~= "" and not path[cp] then
  31.         table.insert(path, cp)
  32.         path[cp] = true
  33.     end
  34. end
  35.  
  36. local function help(name)
  37.     if not name then
  38.         return cons:help()
  39.     end
  40.     local h = helptext[name]
  41.     if h then return h end
  42.     local fname = name:gsub("%.", "\\")
  43.     local file, p, text, expl
  44.     for i = 1, #path do
  45.         p = path[i]:gsub("%?", fname)
  46.         file = sys.File(p)
  47.         if file.exists then
  48.             file:open()
  49.             text = tostring(file:read())
  50.             file:close()
  51.             if text:find("{{explanation}}") then
  52.                 expl = explanation:gsub("{{name}}", name)
  53.                 text = text:gsub("{{explanation}}", expl, 1)
  54.             end
  55.             helptext[name] = text
  56.             return text
  57.         end
  58.     end
  59.     text = "No help available for " .. name
  60.     helptext[name] = text
  61.     return text
  62. end
  63.  
  64. helptext.help =
  65. [[The program help.lua provides a function which returns help information about a library or library component.
  66. It may loaded into your program by the code local help = rquire help, or may be executed directly
  67. by the LuaRT interpreter.
  68.  
  69. Syntax: help(name)
  70.  
  71. The optional argument name is a string formatted like the argument to the require function.
  72. If name is omitted an interactive help console will be opened. If a console is already open, it will cleared.
  73. The help console will be launched if help.lua is run directly.
  74.  
  75. Returns the content of a help file if one is available or "No help available for " .. name.
  76. ]]
  77.  
  78. function cons:help()
  79.     console.clear()
  80.     console.title = "* * * Lua RT Interactive Help Console * * *"
  81.     local name
  82.     while true do
  83.         console.write("Library or component name (type quit to exit): ")
  84.         name = console.readln()
  85.         if name == "quit" then break end
  86.         console.clear()
  87.         console.writeln(help(name).."\n\n")
  88.     end
  89. end
  90.  
  91.  
  92.  
  93. if arg[2] == thisfile then
  94.     cons:help()
  95. else
  96.     return help
  97. end
RAW Paste Data