Advertisement
Terminator_NL

CC Lua for opencomputers

Jun 28th, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local package = require("package")
  3. local term = require("term")
  4. local serialization = require("serialization")
  5. local shell = require("shell")
  6.  
  7. local args, options = shell.parse(...)
  8. local env = setmetatable({}, {__index = _ENV})
  9.  
  10. if #args > 0 then
  11.   local script, reason = loadfile(args[1], nil, env)
  12.   if not script then
  13.     io.stderr:write(tostring(reason) .. "\n")
  14.     os.exit(false)
  15.   end
  16.   local result, reason = pcall(script, table.unpack(args, 2))
  17.   if not result then
  18.     io.stderr:write(reason)
  19.     os.exit(false)
  20.   end
  21. end
  22.  
  23. if #args == 0 or options.i then
  24.   local function optrequire(...)
  25.     local success, module = pcall(require, ...)
  26.     if success then
  27.       return module
  28.     end
  29.   end
  30.   setmetatable(env, {__index = function(t, k)
  31.     return _ENV[k] or optrequire(k)
  32.   end})
  33.  
  34.   local history = {}
  35.   component.gpu.setForeground(0xFFFF00)
  36.   term.write("Interactive lua prompt:\nto exit the lua interpeter use: exit()\n")
  37.   component.gpu.setForeground(0xFFFFFF)
  38.   while term.isAvailable() do
  39.     local foreground = component.gpu.setForeground(0xffff00)
  40.     term.write(tostring(env._PROMPT or "lua> "))
  41.     component.gpu.setForeground(foreground)
  42.     local command = term.read(history)
  43.     if command == nil then -- eof
  44.       return
  45.     end
  46.     while #history > 10 do
  47.       table.remove(history, 1)
  48.     end
  49.     local code, reason
  50.     if string.find(command,"=") == nil then
  51.       command = "="..command
  52.     end
  53.     res = command == "=exit()\n"
  54.     if res == true then
  55.       break
  56.     else
  57.       if string.sub(command,1,1) == "=" then
  58.         code, reason = load("return " .. string.sub(command, 2), "=stdin", "t", env)
  59.       else
  60.         code, reason = load(command, "=stdin", "t", env)
  61.       end
  62.     end
  63.     if code then
  64.       local result = table.pack(xpcall(code, debug.traceback))
  65.       if not result[1] then
  66.         if type(result[2]) == "table" and result[2].reason == "terminated" then
  67.           os.exit(result[2].code)
  68.         end
  69.         io.stderr:write(tostring(result[2]) .. "\n")
  70.       else
  71.         for i = 2, result.n do
  72.           term.write(serialization.serialize(result[i], true) .. "\t", true)
  73.         end
  74.         if term.getCursor() > 1 then
  75.           term.write("\n")
  76.         end
  77.       end
  78.     else
  79.       io.stderr:write(tostring(reason) .. "\n")
  80.     end
  81.   end
  82. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement