Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w,h = term.getSize()
- local args = {...}
- -- Log API by HPWebcamAble
- local log = {}
- local name
- function create(setName)
- name = setName
- if fs.exists(name) then
- fs.delete(name)
- end
- end
- function save(text)
- if not name then return false end
- local f = fs.open(name,"a")
- if not f then return false end
- f.writeLine(text)
- f.close()
- end
- function info(text)
- text = "[Info] "..text
- table.insert(log,text)
- save(text)
- end
- function logError(text)
- text = "[ERROR] "..text
- table.insert(log,text)
- save(text)
- end
- -- End Log API
- create("debug")
- info("Log initialized")
- local function usages()
- local name = shell.getRunningProgram()
- print("Usages:")
- print(name.." <program>")
- end
- local function printC(text,y,screen)--Prints text centered at y on screen (term or monitor)
- if not text then error("expected string,number",2) end
- y = tonumber(y)
- if not y then error("expected string,number",2) end
- screen = screen or term
- local tLenght = #tostring(text)
- local sStart = math.ceil(w/2-tLenght/2)
- local sEnd = sStart + tLenght
- screen.setCursorPos(sStart,y)
- screen.write(text)
- return sStart,sEnd
- end
- local function color( text,back )
- if text then term.setTextColor(text) end
- if back then term.setBackgroundColor(back) end
- end
- local function getPaintColor( color )
- if type(color) == "number" then
- local value = math.floor( math.log(color) / math.log(2) ) + 1
- if value >= 1 and value <= 16 then
- return string.sub( "0123456789abcdef", value, value )
- end
- end
- return " "
- end
- local function createBuffer(backColor)
- local tempBuffer = {}
- for a = 1, w do
- tempBuffer[a] = {}
- for b = 1, h do
- tempBuffer[a][b] = {textCol = colors.white, backCol = backColor, text = ""}
- end
- end
- return tempBuffer
- end
- local function main()
- if not args[1] then
- usages()
- return
- end
- local buffer = createBuffer(colors.black)
- local newShell = {}
- for a,b in pairs(shell) do
- newShell[a] = b
- end
- newShell.getRunningProgram = function()
- return fs.getName(args[1])
- end
- local newTerm = {}
- for a,b in pairs(term) do
- newTerm[a] = b
- end
- newTerm.write = function(text)
- local x,y = term.getCursorPos()
- term.write(text)
- for i = 1, #text do
- buffer[x+i-1][y] = {textCol = term.getTextColor(), backCol = term.getBackgroundColor(), text = text:sub(i,i)}
- end
- end
- newTerm.clear = function()
- buffer = createBuffer(term.getBackgroundColor())
- term.clear()
- end
- local program = coroutine.create( setfenv( loadfile(args[1]) , setmetatable({shell = newShell,term = newTerm},{__index = _G}) ) )
- coroutine.resume(program)
- local paused = false
- while coroutine.status(program) ~= "dead" do
- local eventData = { os.pullEvent() }
- -- Toggle pause.
- if eventData[1] == "key" and eventData[2] == keys.home then
- paused = not paused
- if paused then
- local f = fs.open("debug2","w")
- for x = 1, #buffer do
- for y = 1, #buffer[x] do
- f.write(buffer[x][y])
- end
- f.write("\n")
- end
- f.close()
- end
- -- Neutralize event.
- eventData = {}
- end
- -- Distribute events if the program isn't paused.
- if not paused then
- coroutine.resume(program, unpack(eventData))
- elseif eventData[1] == "mouse_click" then
- -- Construct horizontal bar
- local toWriteText = ""
- local toWriteBackCol = ""
- local toWriteTextCol = ""
- for i = 1, w do
- if i == eventData[3] then
- toWriteText = toWriteText.."X"
- else
- toWriteText = toWriteText.."-"
- end
- local back = buffer[i][eventData[4]].backCol
- toWriteBackCol = toWriteBackCol..getPaintColor(back)
- if back == colors.black then
- toWriteTextCol = toWriteTextCol..getPaintColor(colors.white)
- else
- toWriteTextCol = toWriteTextCol..getPaintColor(colors.black)
- end
- end
- term.setCursorPos(1,eventData[4])
- term.blit(toWriteText,toWriteTextCol,toWriteBackCol)
- -- Construct vertical bar
- end
- end
- end
- local state, err = pcall(main)
- if not state then
- if string.find(err,"Terminated") then
- color(colors.white,colors.black)
- term.clear()
- term.setCursorPos(1,1)
- print("Program Terminated")
- elseif err:find("FORCEQUIT") then
- -- Do nothing
- else
- color(colors.black,colors.red)
- term.write("X")
- printC("Error - Press 'Enter'",1)
- repeat
- local event,key = os.pullEvent("key")
- until key == keys.enter
- color(colors.white,colors.black)
- term.clear()
- printC("An Error Occured:",1)
- term.setCursorPos(1,3)
- print(err or "Unknown")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment