Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --GoLua
- --By Robert Miles (MineRobber___T)
- local resolution = {term.getSize()}
- local mainTerm = term.current()
- local executionWindow = window.create(mainTerm,1,7,resolution[1],resolution[2]-5)
- term.setCursorPos(1,6)
- term.setBackgroundColor(colors.white)
- term.clearLine()
- executionWindow.setBackgroundColor(colors.white)
- executionWindow.setTextColor(colors.black)
- executionWindow.clear()
- --Internal functions
- local function drawHeader(message,backtarget)
- backtarget = backtarget and backtarget or executionWindow
- --if no printer attached, make sure people know
- if not message and not peripheral.find("printer") then message = "Attach a printer to use history print feature." end
- term.redirect(mainTerm)
- if term.isColor() then
- paintutils.drawFilledBox(1,1,resolution[1],5,colors.green)
- else
- paintutils.drawFilledBox(1,1,resolution[1],5,colors.white)
- term.setTextColor(colors.black)
- end
- term.setCursorPos(1,2)
- print("GoLua v0.1")
- print("Use exit() to exit.")
- print((message and message or "Use printHistory() to print usage history."))
- term.redirect(executionWindow)
- end
- function printHistory()
- local function displayError(t,pt)
- drawHeader(t,pt)
- sleep(3)
- drawHeader(nil,pt)
- end
- local printer = peripheral.find("printer")
- if not printer then
- displayError("Error finding printer.")
- else
- if printer.getInkLevel()<1 then displayError("Out of ink.") return end
- if printer.getPaperLevel()<1 then displayError("Out of paper.") return end
- local printerterm = {}
- local function defineValues(o)
- for k,v in pairs(o) do printerterm[k] = v end
- end
- defineValues(term)
- defineValues(printer)
- printerterm.scroll = function()
- while not printer.newPage() do
- if printer.getInkLevel()<1 then displayError("Out of ink, please refill",printerterm) end
- if printer.getPaperLevel()<1 then displayError("Out of paper, please refill",printerterm) end
- end
- printer.setPageTitle("GoLua Printout")
- end
- term.redirect(printerterm)
- local ok,err = pcall(function()
- term.scroll()
- for i=1,#tPrintableHistory do
- if i==#tPrintableHistory then
- write(line) --prevent unnessecary line
- --break on last line
- else
- print(line)
- end
- end
- end)
- if not ok then displayError(err) return end
- local cp = {term.getCursorPos()}
- local pw,ph = printer.getPageSize()
- if cp[1]>1 and cp[2]<ph then
- cp[2] = cp[2]+1
- term.setCursorPos(1,cp[2])
- print(string.rep("-",pw))
- print("GoLua by MineRobber___T")
- end
- term.redirect(executionWindow)
- while not printer.endPage() do displayError("Output tray full please empty") end
- return "Printed output history."
- end
- end
- local nLine = 1
- local function addToHistory(sIn,bInfo,bInOut,bErr)
- if bInfo then
- table.insert(tPrintableHistory,sIn)
- else
- sLog = bInOut and "In [" or "Out ["
- sLog = sLog..nLine.."]: "
- if bErr then sLog = sLog.."Error: " end
- sLog = sLog..sIn
- addToHistory(sLog,true) --recursiveness ftw
- end
- end
- local bRunning = true
- local tEnv = {
- ["exit"] = function() bRunning = false end,
- ["_echo"] = function(...) return ... end,
- ["log"] = function(...)
- local sIn = table.concat({...}," ")
- --addToHistory(sIn,true)
- end
- }
- setmetatable( tEnv, { __index = _ENV } )
- local tCommandHistory = {}
- --functionality
- drawHeader()
- while bRunning do
- write( "lua> " )
- local s = read( nil, tCommandHistory, function( sLine )
- if settings.get( "lua.autocomplete" ) then
- local nStartPos = string.find( sLine, "[a-zA-Z0-9_%.:]+$" )
- if nStartPos then
- sLine = string.sub( sLine, nStartPos )
- end
- if #sLine > 0 then
- return textutils.complete( sLine, tEnv )
- end
- end
- return nil
- end )
- if s:match("%S") and tCommandHistory[#tCommandHistory] ~= s then
- table.insert( tCommandHistory, s )
- --addToHistory(s,false,true)
- end
- local nForcePrint = 0
- local func, e = load( s, "lua", "t", tEnv )
- local func2, e2 = load( "return _echo("..s..");", "lua", "t", tEnv )
- if not func then
- if func2 then
- func = func2
- e = nil
- nForcePrint = 1
- end
- else
- if func2 then
- func = func2
- end
- end
- if func then
- local tResults = table.pack( pcall( func ) )
- if tResults[1] then
- local function display(...)
- print(...)
- --addToHistory(table.concat({...}," "),false,false)
- end
- local n = 1
- while n < tResults.n or (n <= nForcePrint) do
- local value = tResults[ n + 1 ]
- if type( value ) == "table" then
- local metatable = getmetatable( value )
- if type(metatable) == "table" and type(metatable.__tostring) == "function" then
- display( tostring( value ) )
- else
- local ok, serialised = pcall( textutils.serialise, value )
- if ok then
- display( serialised )
- else
- display( tostring( value ) )
- end
- end
- else
- display( tostring( value ) )
- end
- n = n + 1
- end
- else
- printError( tResults[2] )
- --addToHistory(tResults[2],false,false,true)
- end
- else
- printError( e )
- --addToHistory(e,false,false,true)
- end
- --nLine = nLine + 1
- if s:find("printHistory()") then tPrintableHistory = {} end
- end
- term.redirect(mainTerm)
- executionWindow.setVisible(false)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("Thanks for using GoLua!")
Advertisement
Add Comment
Please, Sign In to add comment