Advertisement
officedevil

text displayer script for computercraft

Sep 25th, 2024
111
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | Source Code | 0 0
  1. -- function to check for the file
  2. function fileExists(fileName)
  3.     local file = fs.open(fileName, "r")
  4.     if file then
  5.         file.close()
  6.         return true
  7.     end
  8.     return false
  9. end
  10.  
  11. -- name of the file that will display the text
  12. local fileName = "text.txt"
  13.  
  14. -- connect monitor to console
  15. local monitor = peripheral.wrap("top") -- top, bottom, left, right
  16.  
  17. if monitor then
  18.     -- clear monitor and reset text position
  19.     monitor.clear()
  20.     monitor.setCursorPos(1, 1)
  21.     -- text size, text color and monitor background color https://computercraft.info/wiki/Colors_(API)
  22.     monitor.setTextColor(1)
  23.     monitor.setTextScale(1)
  24.     monitor.setBackgroundColor(32768)
  25.  
  26.     -- checking for file
  27.     if fileExists(fileName) then
  28.         -- open file
  29.         local file = fs.open(fileName, "r")
  30.  
  31.         -- reading the file
  32.         local lines = {}
  33.         while true do
  34.             local line = file.readLine()
  35.             if line == nil then break end
  36.             table.insert(lines, line)
  37.         end
  38.  
  39.         -- file close
  40.         file.close()
  41.  
  42.         -- file display
  43.         for i, line in ipairs(lines) do
  44.             -- if the line is not empty then display
  45.             if line ~= "" then
  46.                 monitor.write(line)
  47.                 local x, y = monitor.getCursorPos()
  48.                 monitor.setCursorPos(1, y + 1) -- next line
  49.             end
  50.         end
  51.     else
  52.         monitor.write("Display file not found.")
  53.     end
  54. else
  55.     print("Monitor not connected.")
  56. end
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement