TwoThe

OC clock

Sep 8th, 2014
3,983
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require "component"
  2. local event = require "event"
  3. local term = require "term"
  4. local gpu = component.gpu
  5.  
  6. local key_space = require("keyboard").keys.space
  7. local running = true
  8. local x, y, width, height
  9. local outputWidth = 8 -- 00:00:00 == 8 chars
  10.  
  11. local events = setmetatable({}, {__index = function() return function() end end})
  12. function events.key_up(keyboard, char, code, player)
  13.   if (code == key_space) then
  14.     running = false
  15.   end  
  16. end
  17.  
  18. function events.screen_resized(screenAddress, newWidth, newHeight)
  19.   width, height = newWidth, newHeight
  20.   x = math.max(1, math.floor((width - outputWidth) / 2))
  21.   y = math.max(1, math.floor(height / 2))
  22.  
  23.   term.clear()
  24. end
  25.  
  26. function clockTimer()
  27.   gpu.set(x, y, os.date("%H:%M:%S", os.time()))
  28. end
  29.  
  30. function handleEvent(event, ...)
  31.   if (event) then
  32.     events[event](...)
  33.   end  
  34. end
  35.  
  36. local prevWidth, prevHeight = gpu.getResolution()
  37. if (not gpu.setResolution(outputWidth, 1)) then
  38.   events.screen_resized(false, prevWidth, prevHeight)
  39. end
  40.  
  41. local timerEvent = event.timer(1/20, clockTimer, math.huge)
  42.  
  43. while running do
  44.   handleEvent(event.pull())
  45. end
  46.  
  47. event.cancel(timerEvent)
  48. gpu.setResolution(prevWidth, prevHeight)
RAW Paste Data