t8rt0t

logviewer.lua

May 19th, 2024 (edited)
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. if monitor then
  3.     monitor.setTextScale(1)
  4.     term.redirect(monitor)
  5. end
  6. local maxX,maxY = term.getSize()
  7. local scroll = 0
  8. shell.openTab("sh")
  9.  
  10. local function refresh_log()
  11.     local file = fs.open("disk/latest.log","r")
  12.     if not file then return end
  13.     for i=1,scroll do
  14.         file.readLine()
  15.     end
  16.     term.clear()
  17.     term.setCursorPos(1,1)
  18.     local color = colors.green
  19.     for i=1,maxY do
  20.         local str = file.readLine()
  21.         term.setTextColor(color)
  22.         if str then print(str) end
  23.         if color == colors.green then color = colors.yellow else color = colors.green end
  24.     end
  25.     if file.readLine() then
  26.         term.setTextColor(colors.white)
  27.         term.setCursorPos(maxX,maxY)
  28.         term.write("v")
  29.     end
  30.     if scroll > 0 then
  31.         term.setTextColor(colors.white)
  32.         term.setCursorPos(maxX,1)
  33.         term.write("^")
  34.     end
  35.     file.close()
  36. end
  37.  
  38. local function countLines()
  39.     local file = fs.open("disk/latest.log","r")
  40.     if not file then return end
  41.     local str = file.readAll() or ""
  42.     local _,count = str:gsub("\n","")
  43.     return count
  44. end
  45.  
  46. local function ui_refresh()
  47.     os.startTimer(5)
  48.     while true do
  49.         local e,a,b,c = os.pullEvent()
  50.         if e == "timer" then
  51.             refresh_log()
  52.             os.startTimer(5)
  53.         elseif e == "monitor_touch" then
  54.             if c > maxY/2 then
  55.                 scroll = scroll + 1
  56.             else
  57.                 scroll = scroll - 1
  58.             end
  59.             if b == maxX and c == 1 then
  60.                 scroll = 0
  61.             elseif b == maxX and c == maxY then
  62.                 scroll = scroll + maxY
  63.             end
  64.             scroll = math.max(math.min(scroll,scroll),0)
  65.         elseif e == "mouse_click" then
  66.             if b > maxY/2 then
  67.                 scroll = scroll + 1
  68.             else
  69.                 scroll = scroll - 1
  70.             end
  71.             if a == maxX and b == 1 then
  72.                 scroll = 0
  73.             elseif a == maxX and b == maxY then
  74.                 scroll = scroll + maxY
  75.             end
  76.             scroll = math.max(math.min(scroll,scroll),0)
  77.         elseif e == "monitor_resize" then
  78.             maxX,maxY = term.getSize()
  79.         end
  80.     end
  81. end
  82.  
  83. ui_refresh()
Advertisement
Add Comment
Please, Sign In to add comment