Advertisement
LDDestroier

Realtime file viewer (ComputerCraft)

Nov 25th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. --[[
  2.  pastebin get uKMMLFYX artie
  3.  std PB uKMMLFYX artie
  4. --]]
  5.  
  6. local tArg = {...}
  7. local filename = tArg[1]
  8.  
  9. if not fs.exists(filename) then
  10.     return printError("That doesn't exist")
  11. elseif fs.isDir(filename) then
  12.     return printError("That's a folder, smartass")
  13. end
  14.  
  15. local scrollX = 1
  16. local scrollY = 1
  17.  
  18. local scr_x, scr_y = term.getSize()
  19.  
  20. local fileToTable = function(name)
  21.     if not fs.exists(name) then
  22.         return false, {}
  23.     end
  24.     local output = {}
  25.     local file = fs.open(name,"r")
  26.     local line = ""
  27.     repeat
  28.         line = file.readLine()
  29.         if line then
  30.             output[#output+1] = line
  31.         end
  32.     until not line
  33.     file.close()
  34.     return true, output
  35. end
  36.  
  37. local zom = function(num)
  38.     return num > 0 and num or 0
  39. end
  40.  
  41. local t = term.current().setVisible
  42.  
  43. local render = function(scrollX,scrollY,tbl)
  44.     if t then t(false) end
  45.     for y = 1, scr_y do
  46.         term.setCursorPos(1,y)
  47.         term.clearLine()
  48.         if tbl[y+scrollY] then
  49.             term.write((" "):rep(zom(-(scrollX-1))) ..tbl[y+scrollY]:sub(zom(scrollX)))
  50.         end
  51.     end
  52.     if t then t(true) end
  53. end
  54.  
  55. local _,contbl
  56.  
  57. local doIt = function()
  58.     while true do
  59.         _,contbl = fileToTable(filename)
  60.         render(scrollX,scrollY,contbl)
  61.         sleep(0)
  62.     end
  63. end
  64.  
  65. local scrolling = function()
  66.     while true do
  67.         local evt, key = os.pullEvent("key")
  68.         if key == keys.left then
  69.             scrollX = scrollX - 1
  70.         elseif key == keys.right then
  71.             scrollX = scrollX + 1
  72.         elseif key == keys.up then
  73.             scrollY = scrollY - 1
  74.         elseif key == keys.down then
  75.             scrollY = scrollY + 1
  76.         elseif key == keys.q then
  77.             return
  78.         end
  79.     end
  80. end
  81.  
  82. parallel.waitForAny(scrolling,doIt)
  83. term.setCursorPos(1,scr_y)
  84. print("")
  85. sleep(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement