bigtwisty

previewOn

Sep 8th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.28 KB | None | 0 0
  1. args = {...}
  2. if #args < 2 then
  3.   print("Usage: readOn <direction> <filename>")
  4.   return
  5. end
  6.  
  7. if not fs.exists(shell.resolve(args[2])) then
  8.   print("File does not exist: ",args[2])
  9.   return
  10. end
  11.  
  12. if peripheral.getType(args[1])~="monitor" then
  13.   print("No monitor in direction: ",args[1])
  14.   return
  15. end
  16.  
  17. local reader = coroutine.create( function()
  18.   shell.run("preview", args[2])
  19. end )
  20.  
  21. function sendEvent(...)
  22.   coroutine.resume(reader, ...)
  23. end
  24.  
  25. local mon = peripheral.wrap(args[1])
  26. if not mon.isColor() then
  27.   print("previewOn requires an advanced monitor.")
  28.   return
  29. end
  30.  
  31. mon.setTextScale(0.5)
  32. _,height = mon.getSize()
  33. midscreen = height / 2
  34.  
  35. print("Reading ", args[2], " on ", args[1], " monitor.")
  36. print("Click top half to scroll up, bottom to scroll down.")
  37. print("Press q to quit")
  38.  
  39. term.redirect(mon)
  40.  
  41. sendEvent()
  42.  
  43. while coroutine.status(reader)~="dead" do
  44.   e = { os.pullEventRaw() }
  45.   if e[1] == "char" and e[2] == "q" or e[1] == "monitor_resize" then
  46.     sendEvent("key", 29)
  47.     sendEvent("key", 28)
  48.   elseif e[1] == "monitor_touch" then
  49.     dir = e[4] > midscreen and 1 or -1
  50.     for i=1,height-1 do
  51.       sendEvent("mouse_scroll",dir, 1,1)
  52.     end
  53.     sendEvent("mouse_click", 1, 1, 1)
  54.   else
  55.     sendEvent(unpack(e))
  56.   end
  57. end
  58.  
  59. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment