Guest User

Pause a program (WIP)

a guest
Oct 25th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.81 KB | None | 0 0
  1. local w,h = term.getSize()
  2. local args = {...}
  3.  
  4. -- Log API by HPWebcamAble
  5.  
  6. local log = {}
  7. local name
  8.  
  9. function create(setName)
  10.   name = setName
  11.   if fs.exists(name) then
  12.     fs.delete(name)
  13.   end
  14. end
  15.  
  16. function save(text)
  17.   if not name then return false end
  18.   local f = fs.open(name,"a")
  19.   if not f then return false end
  20.   f.writeLine(text)
  21.   f.close()
  22. end
  23.  
  24. function info(text)
  25.   text = "[Info] "..text
  26.   table.insert(log,text)
  27.   save(text)
  28. end
  29.  
  30. function logError(text)
  31.   text = "[ERROR] "..text
  32.   table.insert(log,text)
  33.   save(text)
  34. end
  35. -- End Log API
  36.  
  37. create("debug")
  38. info("Log initialized")
  39.  
  40. local function usages()
  41.   local name = shell.getRunningProgram()
  42.   print("Usages:")
  43.   print(name.." <program>")
  44. end
  45.  
  46. local function printC(text,y,screen)--Prints text centered at y on screen (term or monitor)
  47.   if not text then error("expected string,number",2) end
  48.   y = tonumber(y)
  49.   if not y then error("expected string,number",2) end
  50.   screen = screen or term
  51.   local tLenght = #tostring(text)
  52.   local sStart = math.ceil(w/2-tLenght/2)
  53.   local sEnd = sStart + tLenght
  54.   screen.setCursorPos(sStart,y)
  55.   screen.write(text)
  56.   return sStart,sEnd
  57. end
  58.  
  59. local function color( text,back )
  60.   if text then term.setTextColor(text) end
  61.   if back then term.setBackgroundColor(back) end
  62. end
  63.  
  64. local function getPaintColor( color )
  65.     if type(color) == "number" then
  66.         local value = math.floor( math.log(color) / math.log(2) ) + 1
  67.         if value >= 1 and value <= 16 then
  68.             return string.sub( "0123456789abcdef", value, value )
  69.         end
  70.     end
  71.     return " "
  72. end
  73.  
  74. local function createBuffer(backColor)
  75.   local tempBuffer = {}
  76.   for a = 1, w do
  77.     tempBuffer[a] = {}
  78.     for b = 1, h do
  79.       tempBuffer[a][b] = {textCol = colors.white, backCol = backColor, text = ""}
  80.     end
  81.   end
  82.   return tempBuffer
  83. end
  84.  
  85. local function main()
  86.   if not args[1] then
  87.     usages()
  88.     return
  89.   end
  90.  
  91.   local buffer = createBuffer(colors.black)
  92.  
  93.   local newShell = {}
  94.   for a,b in pairs(shell) do
  95.     newShell[a] = b
  96.   end
  97.   newShell.getRunningProgram = function()
  98.     return fs.getName(args[1])
  99.   end
  100.  
  101.   local newTerm = {}
  102.   for a,b in pairs(term) do
  103.     newTerm[a] = b
  104.   end
  105.   newTerm.write = function(text)
  106.     local x,y = term.getCursorPos()
  107.     term.write(text)
  108.     for i = 1, #text do
  109.       buffer[x+i-1][y] = {textCol = term.getTextColor(), backCol = term.getBackgroundColor(), text = text:sub(i,i)}
  110.     end
  111.   end
  112.   newTerm.clear = function()
  113.     buffer = createBuffer(term.getBackgroundColor())
  114.     term.clear()
  115.   end
  116.  
  117.  
  118.   local program = coroutine.create( setfenv( loadfile(args[1]) , setmetatable({shell = newShell,term = newTerm},{__index = _G}) ) )
  119.  
  120.   coroutine.resume(program)
  121.  
  122.   local paused = false
  123.   while coroutine.status(program) ~= "dead" do
  124.     local eventData = { os.pullEvent() }
  125.  
  126.     -- Toggle pause.
  127.     if eventData[1] == "key" and eventData[2] == keys.home then
  128.       paused = not paused
  129.      
  130.       if paused then
  131.         local f = fs.open("debug2","w")
  132.         for x = 1, #buffer do
  133.           for y = 1, #buffer[x] do
  134.             f.write(buffer[x][y])
  135.           end
  136.           f.write("\n")
  137.         end
  138.         f.close()
  139.       end
  140.  
  141.       -- Neutralize event.
  142.       eventData = {}
  143.     end
  144.  
  145.     -- Distribute events if the program isn't paused.
  146.     if not paused then
  147.       coroutine.resume(program, unpack(eventData))
  148.     elseif eventData[1] == "mouse_click" then
  149.       -- Construct horizontal bar
  150.       local toWriteText = ""
  151.       local toWriteBackCol = ""
  152.       local toWriteTextCol = ""
  153.       for i = 1, w do
  154.         if i == eventData[3] then
  155.           toWriteText = toWriteText.."X"
  156.         else
  157.           toWriteText = toWriteText.."-"
  158.         end
  159.         local back = buffer[i][eventData[4]].backCol
  160.         toWriteBackCol = toWriteBackCol..getPaintColor(back)
  161.         if back == colors.black then
  162.           toWriteTextCol = toWriteTextCol..getPaintColor(colors.white)
  163.         else
  164.           toWriteTextCol = toWriteTextCol..getPaintColor(colors.black)
  165.         end
  166.       end
  167.       term.setCursorPos(1,eventData[4])
  168.       term.blit(toWriteText,toWriteTextCol,toWriteBackCol)
  169.      
  170.       -- Construct vertical bar
  171.      
  172.     end
  173.   end
  174. end
  175.  
  176. local state, err = pcall(main)
  177.  
  178. if not state then
  179.   if string.find(err,"Terminated") then
  180.     color(colors.white,colors.black)
  181.     term.clear()
  182.     term.setCursorPos(1,1)
  183.     print("Program Terminated")
  184.   elseif err:find("FORCEQUIT") then
  185.     -- Do nothing
  186.   else
  187.     color(colors.black,colors.red)
  188.     term.write("X")
  189.     printC("Error - Press 'Enter'",1)
  190.     repeat
  191.       local event,key = os.pullEvent("key")
  192.     until key == keys.enter
  193.     color(colors.white,colors.black)
  194.     term.clear()
  195.     printC("An Error Occured:",1)
  196.     term.setCursorPos(1,3)
  197.     print(err or "Unknown")
  198.   end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment