Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Basic Monitor Message Scroller
- --# by Dog aka HyrdantHunter
- --# pastebin: sM3Q6qaY
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- local tArgs = { ... }
- local scrollSpeed = 0.1 --# higher is slower
- local monX, monY, text, centerMessage, flashMessage, colorReset
- local mon = {
- peripheral.find("monitor",
- function(name, object)
- object.setTextColor(colors.white)
- object.setBackgroundColor(colors.black)
- object.clear()
- return object.isColor()
- end
- )
- }
- local function flashText()
- for i = 1, 12 do
- for j = 1, #mon do
- mon[j].setTextColor(2 ^ math.random(0, 14)) --# skipping 15 (black)
- mon[j].setCursorPos(math.floor((monX - #text) / 2) + 1, 1)
- mon[j].write(text)
- if colorReset then mon[j].setTextColor(colors.white) end
- end
- sleep(0.1)
- end
- end
- local function scrollText()
- local subValue, txtLen = 1, #text
- while true do
- if centerMessage then
- for i = monX, math.floor((monX - txtLen) / 2) + 1, -1 do
- for j = 1, #mon do
- mon[j].setCursorPos(i, 1)
- mon[j].write(text:sub(1, (subValue < txtLen) and subValue or txtLen))
- if subValue > txtLen then mon[j].write(" ") end
- end
- subValue = subValue + 1
- sleep(scrollSpeed)
- end
- sleep(1 - scrollSpeed)
- if flashMessage then flashText() sleep(0.5) end
- subValue = 1
- for i = 1, #mon do
- mon[i].clear()
- end
- else
- for i = monX, 0 - #text, -1 do
- for j = 1, #mon do
- mon[j].setCursorPos(i > 0 and i or 1, 1)
- if i > 0 then --# if i == 0 then do nothing
- mon[j].write(text:sub(1, (subValue < txtLen) and subValue or txtLen))
- elseif i < 0 then --# if i == 0 then do nothing
- mon[j].write(text:sub(-(i - 1)))
- end
- if subValue > txtLen and i ~= 0 then mon[j].write(" ") end
- end
- subValue = subValue + 1
- if i ~= 0 then sleep(scrollSpeed) end
- end
- subValue = 1
- end
- end
- end
- local function userInput()
- while true do
- local _, char = os.pullEvent("char")
- if char == "q" then
- for i = 1, #mon do
- mon[i].setTextColor(colors.white)
- mon[i].clear()
- end
- return
- end
- end
- end
- local function inputMessage()
- while true do
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Message to be displayed...")
- term.setCursorPos(1, 3)
- local message = read()
- if message:lower() == "quit" or message:lower() == "exit" then error("\n", 0) end
- if message ~= "" then
- term.setCursorPos(1, 5)
- term.write("Text Scale? [0.5 to 5] ")
- while true do
- local tScale = tonumber(read())
- if tScale and tScale >= 0.5 and tScale <= 5 then
- for i = 1, #mon do
- mon[i].setTextScale(tScale)
- monX, monY = mon[i].getSize()
- end
- break
- end
- end
- if #message <= monX then
- term.setCursorPos(1, 7)
- term.write("Center message? [Y/N] ")
- centerMessage = read():sub(1, 1):lower() == "y"
- if centerMessage then
- term.setCursorPos(1, 9)
- term.write("Flash message? [Y/N] ")
- flashMessage = read():sub(1, 1):lower() == "y"
- if flashMessage then
- term.setCursorPos(1, 11)
- term.write("Reset color? [Y/N] ")
- colorReset = read():sub(1, 1):lower() == "y"
- end
- end
- end
- return message
- end
- end
- end
- local function termDisplay()
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Message being displayed...")
- term.setCursorPos(1, 3)
- write(text)
- end
- if tArgs[1] then
- text = tArgs[1]
- if not tArgs[2] then error("Text Scale must be specified as the second argument", 0) end
- local tScale = tonumber(tArgs[2])
- if tScale and tScale >= 0.5 and tScale <= 5 then
- for i = 1, #mon do
- mon[i].setTextScale(tScale)
- monX, monY = mon[i].getSize()
- end
- else
- error("Text Scale must be between 0.5 and 5.0", 0)
- end
- if tArgs[3] then
- centerMessage = tArgs[3]:sub(1, 1):lower() == "y"
- if tArgs[4] then
- flashMessage = tArgs[4]:sub(1, 1):lower() == "y"
- if tArgs[5] then
- colorReset = tArgs[5]:sub(1, 1):lower() == "y"
- end
- end
- end
- while true do
- termDisplay()
- parallel.waitForAny(scrollText, userInput)
- text = inputMessage()
- end
- else
- while true do
- text = inputMessage()
- termDisplay()
- parallel.waitForAny(scrollText, userInput)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement