Advertisement
RDS_YES

file log

Jan 20th, 2023 (edited)
722
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.52 KB | None | 0 0
  1. local color1 = colors.lightGray
  2. local color2 = colors.cyan
  3. local _, height = term.getSize()
  4. local y = 1
  5. local path_name = "lc/".."input.txt"
  6. local temp_lines = {}
  7. local lines = {}
  8. local file = io.open(path_name)
  9. if file == nil then error("file is nil",0) end
  10.  
  11. local function reverse()
  12.     local x = 1
  13.     for i = #temp_lines, 1, -1 do
  14.         lines[x] = temp_lines[i]
  15.         x = x + 1
  16.     end
  17. end
  18.  
  19. local function make_lines()
  20.     local x = 1
  21.     for line in file:lines() do
  22.         temp_lines[x] = line
  23.         x = x + 1
  24.     end
  25.     file:close()
  26.     reverse()
  27. end
  28.  
  29. local function render()
  30.     term.clear()
  31.     local x = height
  32.     local xx = y % 2
  33.     for i = y, (y + height - 1) do
  34.         if lines[i] ~= nil then
  35.             term.setCursorPos(1,x)
  36.             if xx == 1 then
  37.                 term.setTextColor(color1)
  38.                 xx = 0
  39.             elseif xx == 0 then
  40.                 term.setTextColor(color2)
  41.                 xx = 1
  42.             end
  43.             term.write(lines[i])
  44.             x = x - 1
  45.         end
  46.     end
  47. end
  48.  
  49. local function scroll(key)
  50.     if keys.getName(key) == "up" or keys.getName(key) == "w" then
  51.         y = y + 1
  52.     elseif keys.getName(key) == "down" or keys.getName(key) == "s" then
  53.         y = y - 1
  54.     end
  55.     if y < 1 then
  56.         y = 1
  57.     elseif y > #lines - height + 1 then
  58.         y = #lines - height + 1
  59.     end
  60. end
  61.  
  62. make_lines()
  63.  
  64. while true do
  65.     sleep(0)
  66.     render()
  67.     local event, key, is_held = os.pullEvent("key")
  68.     scroll(key)
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement