kd2bwzgen

GoLua

Jul 18th, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.98 KB | None | 0 0
  1. --GoLua
  2. --By Robert Miles (MineRobber___T)
  3.  
  4. local resolution = {term.getSize()}
  5.  
  6. local mainTerm = term.current()
  7. local executionWindow = window.create(mainTerm,1,7,resolution[1],resolution[2]-5)
  8. term.setCursorPos(1,6)
  9. term.setBackgroundColor(colors.white)
  10. term.clearLine()
  11. executionWindow.setBackgroundColor(colors.white)
  12. executionWindow.setTextColor(colors.black)
  13. executionWindow.clear()
  14.  
  15. --Internal functions
  16. local function drawHeader(message,backtarget)
  17.   backtarget = backtarget and backtarget or executionWindow
  18.   --if no printer attached, make sure people know
  19.   if not message and not peripheral.find("printer") then message = "Attach a printer to use history print feature." end
  20.   term.redirect(mainTerm)
  21.   if term.isColor() then
  22.     paintutils.drawFilledBox(1,1,resolution[1],5,colors.green)
  23.   else
  24.     paintutils.drawFilledBox(1,1,resolution[1],5,colors.white)
  25.     term.setTextColor(colors.black)
  26.   end
  27.   term.setCursorPos(1,2)
  28.   print("GoLua v0.1")
  29.   print("Use exit() to exit.")
  30.   print((message and message or "Use printHistory() to print usage history."))
  31.   term.redirect(executionWindow)
  32. end
  33.  
  34. function printHistory()
  35.   local function displayError(t,pt)
  36.     drawHeader(t,pt)
  37.     sleep(3)
  38.     drawHeader(nil,pt)
  39.   end
  40.   local printer = peripheral.find("printer")
  41.   if not printer then
  42.     displayError("Error finding printer.")
  43.   else
  44.     if printer.getInkLevel()<1 then displayError("Out of ink.") return end
  45.     if printer.getPaperLevel()<1 then displayError("Out of paper.") return end
  46.     local printerterm = {}
  47.     local function defineValues(o)
  48.       for k,v in pairs(o) do printerterm[k] = v end
  49.     end
  50.     defineValues(term)
  51.     defineValues(printer)
  52.     printerterm.scroll = function()
  53.       while not printer.newPage() do
  54.         if printer.getInkLevel()<1 then displayError("Out of ink, please refill",printerterm) end
  55.         if printer.getPaperLevel()<1 then displayError("Out of paper, please refill",printerterm) end
  56.       end
  57.       printer.setPageTitle("GoLua Printout")
  58.     end
  59.     term.redirect(printerterm)
  60.     local ok,err = pcall(function()
  61.       term.scroll()
  62.       for i=1,#tPrintableHistory do
  63.         if i==#tPrintableHistory then
  64.           write(line) --prevent unnessecary line
  65.                       --break on last line
  66.         else
  67.           print(line)
  68.         end
  69.       end
  70.     end)
  71.     if not ok then displayError(err) return end
  72.     local cp = {term.getCursorPos()}
  73.     local pw,ph = printer.getPageSize()
  74.     if cp[1]>1 and cp[2]<ph then
  75.       cp[2] = cp[2]+1
  76.       term.setCursorPos(1,cp[2])
  77.       print(string.rep("-",pw))
  78.       print("GoLua by MineRobber___T")
  79.     end
  80.     term.redirect(executionWindow)
  81.     while not printer.endPage() do displayError("Output tray full please empty") end
  82.     return "Printed output history."
  83.   end
  84. end
  85.  
  86. local nLine = 1
  87. local function addToHistory(sIn,bInfo,bInOut,bErr)
  88.     if bInfo then
  89.         table.insert(tPrintableHistory,sIn)
  90.     else
  91.         sLog = bInOut and "In [" or "Out ["
  92.         sLog = sLog..nLine.."]: "
  93.         if bErr then sLog = sLog.."Error: " end
  94.         sLog = sLog..sIn
  95.         addToHistory(sLog,true) --recursiveness ftw
  96.     end
  97. end
  98.  
  99. local bRunning = true
  100.  
  101. local tEnv = {
  102.   ["exit"] = function() bRunning = false end,
  103.   ["_echo"] = function(...) return ... end,
  104.   ["log"] = function(...)
  105.     local sIn = table.concat({...}," ")
  106.     --addToHistory(sIn,true)
  107.   end
  108. }
  109.  
  110. setmetatable( tEnv, { __index = _ENV } )
  111.  
  112. local tCommandHistory = {}
  113.  
  114. --functionality
  115. drawHeader()
  116. while bRunning do
  117.     write( "lua> " )
  118.  
  119.     local s = read( nil, tCommandHistory, function( sLine )
  120.         if settings.get( "lua.autocomplete" ) then
  121.             local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
  122.             if nStartPos then
  123.                 sLine = string.sub( sLine, nStartPos )
  124.             end
  125.             if #sLine > 0 then
  126.                 return textutils.complete( sLine, tEnv )
  127.             end
  128.         end
  129.         return nil
  130.     end )
  131.     if s:match("%S") and tCommandHistory[#tCommandHistory] ~= s then
  132.         table.insert( tCommandHistory, s )
  133.         --addToHistory(s,false,true)
  134.     end
  135.    
  136.     local nForcePrint = 0
  137.     local func, e = load( s, "lua", "t", tEnv )
  138.     local func2, e2 = load( "return _echo("..s..");", "lua", "t", tEnv )
  139.     if not func then
  140.         if func2 then
  141.             func = func2
  142.             e = nil
  143.             nForcePrint = 1
  144.         end
  145.     else
  146.         if func2 then
  147.             func = func2
  148.         end
  149.     end
  150.    
  151.     if func then
  152.         local tResults = table.pack( pcall( func ) )
  153.         if tResults[1] then
  154.             local function display(...)
  155.                 print(...)
  156.                 --addToHistory(table.concat({...}," "),false,false)
  157.             end
  158.             local n = 1
  159.             while n < tResults.n or (n <= nForcePrint) do
  160.                 local value = tResults[ n + 1 ]
  161.                 if type( value ) == "table" then
  162.                     local metatable = getmetatable( value )
  163.                     if type(metatable) == "table" and type(metatable.__tostring) == "function" then
  164.                         display( tostring( value ) )
  165.                     else
  166.                         local ok, serialised = pcall( textutils.serialise, value )
  167.                         if ok then
  168.                             display( serialised )
  169.                         else
  170.                             display( tostring( value ) )
  171.                         end
  172.                     end
  173.                 else
  174.                     display( tostring( value ) )
  175.                 end
  176.                 n = n + 1
  177.             end
  178.         else
  179.             printError( tResults[2] )
  180.             --addToHistory(tResults[2],false,false,true)
  181.         end
  182.     else
  183.         printError( e )
  184.         --addToHistory(e,false,false,true)
  185.     end
  186.     --nLine = nLine + 1
  187.     if s:find("printHistory()") then tPrintableHistory = {} end
  188. end
  189. term.redirect(mainTerm)
  190. executionWindow.setVisible(false)
  191. term.setBackgroundColor(colors.black)
  192. term.setTextColor(colors.white)
  193. term.clear()
  194. term.setCursorPos(1,1)
  195. print("Thanks for using GoLua!")
Advertisement
Add Comment
Please, Sign In to add comment