Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Resolution 57x24
- local params = {...}
- if #params < 3 then
- error("Use: info monitor_id filename(.txt) filename(.col)")
- end
- local linkColor = colors.red
- local monitorId = params[1]
- local textFile = params[2] .. ".txt"
- local colorFile = params[3] .. ".col"
- local monitor
- local width, height
- local palette = {
- text = nil,
- background = nil,
- header = nil
- }
- local function fileExists(filename)
- local file = fs.open(filename, "r")
- if file then
- file.close()
- return true
- end
- return false
- end
- local function checkFile (filename)
- if not fileExists(filename) then
- error("File '" .. filename .. "' not found")
- end
- end
- local function getMonitor (sideOrId)
- if not peripheral.getType(sideOrId) == "monitor" then
- error("No monitor found")
- end
- print("Monitor found: " .. sideOrId)
- monitor = peripheral.wrap(sideOrId)
- monitor.setTextScale(0.5)
- width, height = monitor.getSize()
- monitor.clear()
- monitor.setCursorPos(1, 1)
- end
- local function loadColors ()
- local file = fs.open(colorFile, "r")
- for lineNumber = 1, 3 do
- local line = file.readLine()
- if not line then
- error(
- colorFile .. ": No line "
- .. lineNumber .. " found"
- )
- end
- if lineNumber == 1 then
- palette.text = tonumber(line)
- elseif lineNumber == 2 then
- palette.background = tonumber(line)
- else
- palette.header = tonumber(line)
- end
- end
- end
- local function newLine ()
- local x, y = monitor.getCursorPos()
- monitor.setCursorPos(1, y + 1)
- end
- local function wordWrap (text)
- local remainingSpace = width - 2
- monitor.write(" ")
- for word in text:gmatch("[a-zA-Z0-9()'\"@!:*/.,_-]+") do
- wordIsLink = false
- if word:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?")
- or word:sub(1, 7) == "http://" then
- wordIsLink = true
- monitor.setTextColor(linkColor)
- end
- if #word > remainingSpace then
- newLine()
- monitor.write(" " .. word .. " ")
- remainingSpace = width - #word - 3
- else
- monitor.write(word .. " ")
- remainingSpace = remainingSpace - #word - 1
- end
- if wordIsLink then
- monitor.setTextColor(palette.text)
- end
- end
- end
- local function drawBackground (color)
- monitor.setBackgroundColor(color)
- monitor.clear()
- end
- local function centerPrint (lineNumber, textColor, text)
- monitor.setCursorPos((width - #text) / 2, lineNumber)
- monitor.setTextColor(textColor)
- monitor.write(text)
- end
- local function getData ()
- local info = fs.open(textFile, "r")
- local infoData = {}
- repeat
- line = info.readLine()
- if line then
- table.insert(infoData, line)
- end
- until line == nil
- info.close()
- return infoData
- end
- --Draw a filled box for e.g. a button or shadow
- local function drawBox (x, y, width, height, color)
- monitor.setBackgroundColor(color)
- monitor.setTextColor(color)
- for i = 0, height - 1 do
- monitor.setCursorPos(x, y + i)
- for j = 1, width do
- monitor.write(" ")
- end
- end
- end
- local function drawHeader (text)
- --Draw shadow
- drawBox(4, 3, width - 5, 3, colors.lightGray)
- --Draw header box
- drawBox(3, 2, width - 5, 3, colors.white)
- --Write text
- centerPrint(3, palette.header, text)
- monitor.setCursorPos(1, 6)
- end
- local function displayInfo ()
- local text = getData()
- drawBackground(palette.background)
- for lineNumber, line in ipairs(text) do
- if lineNumber == 1 then
- drawHeader(line:upper())
- monitor.setTextColor(palette.text)
- monitor.setBackgroundColor(palette.background)
- else
- wordWrap(line)
- end
- newLine()
- end
- end
- --Main program
- checkFile(textFile)
- checkFile(colorFile)
- getMonitor(monitorId)
- loadColors()
- displayInfo()
Advertisement
Add Comment
Please, Sign In to add comment