Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local history = {}
- local function listTableKeys(tbl)
- local list = {}
- for key, _ in pairs(tbl) do
- table.insert(list, key)
- end
- return list
- end
- local function executeCode(code)
- local func, syntaxError = load(code, "user_input", "t", _G)
- if func then
- local success, runtimeError = pcall(func)
- if not success then
- print("Error: " .. runtimeError)
- end
- else
- print("Syntax error: " .. syntaxError)
- end
- end
- -- Load initial file
- if args[1] then
- local file = fs.open(args[1], "r")
- if file then
- local script = file.readAll()
- file.close()
- executeCode(script)
- else
- print("Could not open file: " .. args[1])
- end
- end
- -- Completion function for read
- local function complete(line)
- local results = {}
- local part = line:match("%S+$") or ""
- for k, v in pairs(_G) do
- if k:sub(1, #part) == part then
- table.insert(results, k)
- end
- end
- return results
- end
- -- Interactive shell loop
- local choices = listTableKeys(_G)
- while true do
- write("qt> ")
- local cmd = read(nil, complete, history)
- table.insert(history, cmd)
- choices = listTableKeys(_G)
- executeCode(cmd)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement