Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shaka = require("API")
- local maxMessagesDisplayed = 15
- local messageIndex = 1
- local scrollListStart = 3
- local quit = false
- local messages = shaka.readFile(".myMessages")
- -- local file = fs.open(".userData", "r")
- -- local userName = file.readAll()
- -- file.close()
- local userName = shaka.readFile(".userData", true)
- rednet.broadcast(userName, "delNotifications")
- function inverseTable(t)
- local inversedTable = {}
- for i = #t, 1, -1 do
- table.insert(inversedTable, t[i])
- end
- return inversedTable
- end
- messages = inverseTable(messages)
- function writeHeader()
- shaka.clearScreen()
- shaka.changeColors(colors.gray, colors.black)
- term.clearLine()
- print("All messages:")
- local a, b = term.getSize()
- term.setCursorPos(a - 1, 1)
- term.setBackgroundColor(colors.red)
- term.write(" X")
- end
- function approximate_time(time_str)
- local hour = tonumber(time_str:sub(1, 2))
- local minute = tonumber(time_str:sub(4, 5))
- if minute < 15 then
- return string.format('%02d:00', hour)
- elseif minute < 45 then
- return string.format('%02d:30', hour)
- else
- hour = (hour + 1) % 24
- return string.format('%02d:00', hour)
- end
- end
- function showMessage(text, dateVar, timeVar, sender, receiver)
- -- Define the maximum number of lines that can fit on the screen
- local maxLines = 15
- -- Define the current line number and the total number of lines
- local currentLine = 0
- local totalLines = 0
- -- Split the message text into lines
- local lines = {}
- for line in string.gmatch(text, "[^\r\n]+") do
- table.insert(lines, line)
- totalLines = totalLines + 1
- end
- -- Check if scrolling makes sense
- local scrollAble = totalLines > maxLines
- -- Clear the screen and display the message text
- shaka.clearScreen()
- term.setTextColor(colors.gray)
- print(dateVar .. " - " .. timeVar)
- shaka.changeColors(colors.gray, colors.lightBlue)
- term.clearLine()
- if sender == receiver then
- print("Note to Self")
- else
- print(sender .. " -> " .. receiver)
- end
- shaka.resetColors()
- shaka.nextLine()
- if scrollAble then
- local ab, cd = term.getCursorPos()
- local ba, dc = term.getSize()
- term.setCursorPos(1, dc-1)
- term.setTextColor(colors.gray)
- print("Scrolling available..")
- term.setTextColor(colors.white)
- term.setCursorPos(ab, cd)
- end
- -- Print the first set of lines that can fit on the screen
- for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
- if i ~= 0 then
- print(lines[i])
- end
- end
- -- Listen for mouse scroll to scroll up or down or click to end
- sleep(0.1)
- repeat
- local eventData = {os.pullEvent()}
- local event = eventData[1]
- button = tonumber(eventData[2])
- if button == -1 and currentLine > 1 then
- -- Scroll up
- currentLine = currentLine - 1
- shaka.clearScreen()
- term.setTextColor(colors.gray)
- print(dateVar .. " - " .. timeVar)
- shaka.changeColors(colors.black, colors.lightBlue)
- if sender == receiver then
- print("Note to Self")
- else
- print(sender .. " -> " .. receiver)
- end
- shaka.resetColors()
- print()
- for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
- print(lines[i])
- end
- elseif button == 1 and currentLine < totalLines - maxLines + 1 then
- -- Scroll down
- currentLine = currentLine + 1
- shaka.clearScreen()
- term.setTextColor(colors.gray)
- print(dateVar .. " - " .. timeVar)
- shaka.changeColors(colors.black, colors.lightBlue)
- if sender == receiver then
- print("Note to Self")
- else
- print(sender .. " -> " .. receiver)
- end
- shaka.resetColors()
- print()
- for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
- print(lines[i])
- end
- end
- until event == "mouse_click"
- end
- function convertDate(dateString)
- local day, month, year = dateString:match("(%d+).(%d+).(%d+)")
- return tonumber(day) .. "/" .. tonumber(month)
- end
- function drawMessages()
- writeHeader()
- if messageIndex == 1 then
- local a, b = term.getSize()
- term.setCursorPos(1, b - 1)
- shaka.changeColors(colors.black, colors.gray)
- if #messages > 15 then
- print("Scroll and click..")
- else
- print("Click..")
- end
- end
- term.setCursorPos(1, scrollListStart)
- for i = messageIndex, messageIndex + maxMessagesDisplayed - 1 do
- if messages[i] then
- local message = messages[i]
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- if message.sender == userName and message.receiver == userName then
- term.setTextColor(colors.yellow)
- term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
- term.write("Note to Self")
- elseif message.sender == userName then
- term.setTextColor(colors.white)
- term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
- term.write(shaka.shortenString(message.receiver, 13))
- else
- term.setTextColor(colors.green)
- term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
- term.write(shaka.shortenString(message.sender, 13))
- end
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y + 1)
- end
- end
- end
- function handleScroll()
- while true do
- local event = {os.pullEvent()}
- if event[1] == "mouse_scroll" then
- if event[2] == -1 and messageIndex > 1 then
- messageIndex = messageIndex - 1
- term.scroll(1)
- elseif event[2] == 1 and messageIndex + maxMessagesDisplayed - 1 < #messages then
- messageIndex = messageIndex + 1
- term.scroll(-1)
- end
- elseif event[1] == "mouse_click" and event[2] == 1 then
- local x, y = term.getSize()
- local clickX = event[3]
- local clickY = event[4]
- if clickY == 1 then
- shell.run("messenger")
- quit = true
- return
- end
- local selectedLine = clickY - 1 + messageIndex - scrollListStart + 1
- if selectedLine >= messageIndex and selectedLine <= messageIndex + maxMessagesDisplayed - 1 and messages[selectedLine] then
- local message = messages[selectedLine]
- showMessage(message.text, message.date, message.time, shaka.shortenString(message.sender, 10), shaka.shortenString(message.receiver, 10))
- end
- end
- drawMessages()
- end
- end
- while quit == false do
- drawMessages()
- handleScroll()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement