Advertisement
mathiaas

qtlua

Apr 22nd, 2024 (edited)
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | None | 0 0
  1. local args = {...}
  2. local history = {}
  3.  
  4. local function listTableKeys(tbl)
  5.     local list = {}
  6.     for key, _ in pairs(tbl) do
  7.         table.insert(list, key)
  8.     end
  9.     return list
  10. end
  11.  
  12.  
  13. local function executeCode(code)
  14.     local func, syntaxError = load(code, "user_input", "t", _G)
  15.     if func then
  16.         local success, runtimeError = pcall(func)
  17.         if not success then
  18.             print("Error: " .. runtimeError)
  19.         end
  20.     else
  21.         print("Syntax error: " .. syntaxError)
  22.     end
  23. end
  24.  
  25. -- Load initial file
  26. if args[1] then
  27.     local file = fs.open(args[1], "r")
  28.     if file then
  29.         local script = file.readAll()
  30.         file.close()
  31.         executeCode(script)
  32.     else
  33.         print("Could not open file: " .. args[1])
  34.     end
  35. end
  36.  
  37. -- Completion function for read
  38. local function complete(line)
  39.     local results = {}
  40.     local part = line:match("%S+$") or ""
  41.     for k, v in pairs(_G) do
  42.         if k:sub(1, #part) == part then
  43.             table.insert(results, k)
  44.         end
  45.     end
  46.     return results
  47. end
  48.  
  49. -- Interactive shell loop
  50.  
  51. local choices = listTableKeys(_G)
  52. while true do
  53.     write("qt> ")
  54.     local cmd = read(nil, complete, history)
  55.     table.insert(history, cmd)
  56.     choices = listTableKeys(_G)
  57.     executeCode(cmd)
  58. end
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement