Advertisement
HydrantHunter

Monitor Message Scroller

Nov 13th, 2016
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. --# Basic Monitor Message Scroller
  2. --# by Dog aka HyrdantHunter
  3. --# pastebin: sM3Q6qaY
  4. term.setBackgroundColor(colors.black)
  5. term.setTextColor(colors.white)
  6. local tArgs = { ... }
  7. local scrollSpeed = 0.1  --# higher is slower
  8. local monX, monY, text, centerMessage, flashMessage, colorReset
  9. local mon = {
  10.   peripheral.find("monitor",
  11.     function(name, object)
  12.       object.setTextColor(colors.white)
  13.       object.setBackgroundColor(colors.black)
  14.       object.clear()
  15.       return object.isColor()
  16.     end
  17.   )
  18. }
  19.  
  20. local function flashText()
  21.   for i = 1, 12 do
  22.     for j = 1, #mon do
  23.       mon[j].setTextColor(2 ^ math.random(0, 14)) --# skipping 15 (black)
  24.       mon[j].setCursorPos(math.floor((monX - #text) / 2) + 1, 1)
  25.       mon[j].write(text)
  26.       if colorReset then mon[j].setTextColor(colors.white) end
  27.     end
  28.     sleep(0.1)
  29.   end
  30. end
  31.  
  32. local function scrollText()
  33.   local subValue, txtLen = 1, #text
  34.   while true do
  35.     if centerMessage then
  36.       for i = monX, math.floor((monX - txtLen) / 2) + 1, -1 do
  37.         for j = 1, #mon do
  38.           mon[j].setCursorPos(i, 1)
  39.           mon[j].write(text:sub(1, (subValue < txtLen) and subValue or txtLen))
  40.           if subValue > txtLen then mon[j].write(" ") end
  41.         end
  42.         subValue = subValue + 1
  43.         sleep(scrollSpeed)
  44.       end
  45.       sleep(1 - scrollSpeed)
  46.       if flashMessage then flashText() sleep(0.5) end
  47.       subValue = 1
  48.       for i = 1, #mon do
  49.         mon[i].clear()
  50.       end
  51.     else
  52.       for i = monX, 0 - #text, -1 do
  53.         for j = 1, #mon do
  54.           mon[j].setCursorPos(i > 0 and i or 1, 1)
  55.           if i > 0 then     --# if i == 0 then do nothing
  56.             mon[j].write(text:sub(1, (subValue < txtLen) and subValue or txtLen))
  57.           elseif i < 0 then --# if i == 0 then do nothing
  58.             mon[j].write(text:sub(-(i - 1)))
  59.           end
  60.           if subValue > txtLen and i ~= 0 then mon[j].write(" ") end
  61.         end
  62.         subValue = subValue + 1
  63.         if i ~= 0 then sleep(scrollSpeed) end
  64.       end
  65.       subValue = 1
  66.     end
  67.   end
  68. end
  69.  
  70. local function userInput()
  71.   while true do
  72.     local _, char = os.pullEvent("char")
  73.     if char == "q" then
  74.       for i = 1, #mon do
  75.         mon[i].setTextColor(colors.white)
  76.         mon[i].clear()
  77.       end
  78.       return
  79.     end
  80.   end
  81. end
  82.  
  83. local function inputMessage()
  84.   while true do
  85.     term.clear()
  86.     term.setCursorPos(1, 1)
  87.     term.write("Message to be displayed...")
  88.     term.setCursorPos(1, 3)
  89.     local message = read()
  90.     if message:lower() == "quit" or message:lower() == "exit" then error("\n", 0) end
  91.     if message ~= "" then
  92.       term.setCursorPos(1, 5)
  93.       term.write("Text Scale? [0.5 to 5] ")
  94.       while true do
  95.         local tScale = tonumber(read())
  96.         if tScale and tScale >= 0.5 and tScale <= 5 then
  97.           for i = 1, #mon do
  98.             mon[i].setTextScale(tScale)
  99.             monX, monY = mon[i].getSize()
  100.           end
  101.           break
  102.         end
  103.       end
  104.       if #message <= monX then
  105.         term.setCursorPos(1, 7)
  106.         term.write("Center message? [Y/N] ")
  107.         centerMessage = read():sub(1, 1):lower() == "y"
  108.         if centerMessage then
  109.           term.setCursorPos(1, 9)
  110.           term.write("Flash message? [Y/N] ")
  111.           flashMessage = read():sub(1, 1):lower() == "y"
  112.           if flashMessage then
  113.             term.setCursorPos(1, 11)
  114.             term.write("Reset color? [Y/N] ")
  115.             colorReset = read():sub(1, 1):lower() == "y"
  116.           end
  117.         end
  118.       end
  119.       return message
  120.     end
  121.   end
  122. end
  123.  
  124. local function termDisplay()
  125.   term.clear()
  126.   term.setCursorPos(1, 1)
  127.   term.write("Message being displayed...")
  128.   term.setCursorPos(1, 3)
  129.   write(text)
  130. end
  131.  
  132. if tArgs[1] then
  133.   text = tArgs[1]
  134.   if not tArgs[2] then error("Text Scale must be specified as the second argument", 0) end
  135.   local tScale = tonumber(tArgs[2])
  136.   if tScale and tScale >= 0.5 and tScale <= 5 then
  137.     for i = 1, #mon do
  138.       mon[i].setTextScale(tScale)
  139.       monX, monY = mon[i].getSize()
  140.     end
  141.   else
  142.     error("Text Scale must be between 0.5 and 5.0", 0)
  143.   end
  144.   if tArgs[3] then
  145.     centerMessage = tArgs[3]:sub(1, 1):lower() == "y"
  146.     if tArgs[4] then
  147.       flashMessage = tArgs[4]:sub(1, 1):lower() == "y"
  148.       if tArgs[5] then
  149.         colorReset = tArgs[5]:sub(1, 1):lower() == "y"
  150.       end
  151.     end
  152.   end
  153.   while true do
  154.     termDisplay()
  155.     parallel.waitForAny(scrollText, userInput)
  156.     text = inputMessage()
  157.   end
  158. else
  159.   while true do
  160.     text = inputMessage()
  161.     termDisplay()
  162.     parallel.waitForAny(scrollText, userInput)
  163.   end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement