Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local TEXT_SCALE = 2 -- Text size (0.5, 1, 2, ...)
- local TEXT_COLOR = colors.orange -- Text color (use colors.<color>)
- -- Setup monitors
- local monitors = {}
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "monitor" and peripheral.isPresent(name) then
- local monitor = peripheral.wrap(name)
- monitor.setTextScale(TEXT_SCALE)
- table.insert(monitors, monitor)
- end
- end
- -- Wireless modem setup
- local modem = peripheral.wrap("left")
- assert(modem, "Wireless modem not connected.")
- modem.open(1)
- local function splitIntoLines(text, maxWidth)
- local lines = {}
- while #text > maxWidth do
- local splitPoint = maxWidth
- for i = maxWidth, 1, -1 do
- if text:sub(i, i):match("%s") then
- splitPoint = i
- break
- end
- end
- local line = text:sub(1, splitPoint):gsub("^%s+", ""):gsub("%s+$", "")
- table.insert(lines, line)
- text = text:sub(splitPoint + 1):gsub("^%s+", "")
- end
- if #text > 0 then
- table.insert(lines, text)
- end
- return lines
- end
- local function sanitizeText(text)
- -- Replace UTF-8 artifacts with proper characters
- text = text:gsub("é", "é"):gsub("è", "è"):gsub("ê", "ê"):gsub("ë", "ë")
- text = text:gsub("Ã ", "à"):gsub("Ã ", "â"):gsub("Ã ", "ä")
- local replacements = {
- ["é"] = "e", ["è"] = "e", ["ê"] = "e", ["ë"] = "e",
- ["à"] = "a", ["â"] = "a", ["ä"] = "a",
- ["ç"] = "c",
- ["ù"] = "u", ["û"] = "u", ["ü"] = "u",
- ["î"] = "i", ["ï"] = "i",
- ["ô"] = "o", ["ö"] = "o",
- ["â"] = "a", ["ã"] = "a",
- ["œ"] = "oe", ["æ"] = "ae",
- ["ÿ"] = "y",
- ["É"] = "E", ["È"] = "E", ["Ê"] = "E", ["Ë"] = "E",
- ["À"] = "A", ["Â"] = "A", ["Ä"] = "A",
- ["Ç"] = "C",
- ["Ù"] = "U", ["Û"] = "U", ["Ü"] = "U",
- ["Î"] = "I", ["Ï"] = "I",
- ["Ô"] = "O", ["Ö"] = "O",
- ["Â"] = "A", ["Ã"] = "A",
- ["Œ"] = "OE", ["Æ"] = "AE",
- ["Ÿ"] = "Y"
- }
- -- Replace each character in the text based on the replacements table
- for original, replacement in pairs(replacements) do
- text = text:gsub(original, replacement)
- end
- return text
- end
- local function displayMessageCentered(message)
- message = sanitizeText(message)
- for _, monitor in ipairs(monitors) do
- monitor.clear()
- monitor.setTextColor(TEXT_COLOR)
- local width, height = monitor.getSize()
- local lines = splitIntoLines(message, width)
- local yStart = math.floor((height - #lines) / 2) + 1
- for i, line in ipairs(lines) do
- local xStart = math.floor((width - #line) / 2) + 1
- monitor.setCursorPos(xStart, yStart + i - 1)
- monitor.write(line)
- end
- end
- end
- -- Listen for incoming messages
- while true do
- local event, side, senderChannel, replyChannel, message, distance = os.pullEvent("modem_message")
- if senderChannel == 1 then
- displayMessageCentered(message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement