Advertisement
Shaka01

Messenger Client viewAll Items - dependancy

Mar 6th, 2023 (edited)
3,511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.41 KB | None | 0 0
  1. shaka = require("API")
  2. local maxMessagesDisplayed = 15
  3. local messageIndex = 1
  4. local scrollListStart = 3
  5. local quit = false
  6. local messages = shaka.readFile(".myMessages")
  7.   -- local file = fs.open(".userData", "r")
  8.   -- local userName = file.readAll()
  9.   -- file.close()
  10. local userName = shaka.readFile(".userData", true)
  11. rednet.broadcast(userName, "delNotifications")
  12.  
  13.  
  14.  
  15.  
  16. function inverseTable(t)
  17.   local inversedTable = {}
  18.   for i = #t, 1, -1 do
  19.     table.insert(inversedTable, t[i])
  20.   end
  21.   return inversedTable
  22. end
  23.  
  24. messages = inverseTable(messages)
  25.  
  26. function writeHeader()
  27.     shaka.clearScreen()
  28.     shaka.changeColors(colors.gray, colors.black)
  29.     term.clearLine()
  30.     print("All messages:")
  31.     local a, b = term.getSize()
  32.     term.setCursorPos(a - 1, 1)
  33.     term.setBackgroundColor(colors.red)
  34.     term.write(" X")
  35. end
  36.  
  37. function approximate_time(time_str)
  38.   local hour = tonumber(time_str:sub(1, 2))
  39.   local minute = tonumber(time_str:sub(4, 5))
  40.  
  41.   if minute < 15 then
  42.     return string.format('%02d:00', hour)
  43.   elseif minute < 45 then
  44.     return string.format('%02d:30', hour)
  45.   else
  46.     hour = (hour + 1) % 24
  47.     return string.format('%02d:00', hour)
  48.   end
  49. end
  50.  
  51.  
  52. function showMessage(text, dateVar, timeVar, sender, receiver)
  53.   -- Define the maximum number of lines that can fit on the screen
  54.   local maxLines = 15
  55.  
  56.   -- Define the current line number and the total number of lines
  57.   local currentLine = 0
  58.   local totalLines = 0
  59.  
  60.   -- Split the message text into lines
  61.   local lines = {}
  62.   for line in string.gmatch(text, "[^\r\n]+") do
  63.     table.insert(lines, line)
  64.     totalLines = totalLines + 1
  65.   end
  66.  
  67.   -- Check if scrolling makes sense
  68.   local scrollAble = totalLines > maxLines
  69.  
  70.   -- Clear the screen and display the message text
  71.   shaka.clearScreen()
  72.   term.setTextColor(colors.gray)
  73.   print(dateVar .. " - " .. timeVar)
  74.   shaka.changeColors(colors.gray, colors.lightBlue)
  75.   term.clearLine()
  76.   if sender == receiver then
  77.     print("Note to Self")
  78.   else
  79.     print(sender .. " -> " .. receiver)
  80.   end
  81.   shaka.resetColors()
  82.   shaka.nextLine()
  83.   if scrollAble then
  84.     local ab, cd = term.getCursorPos()
  85.     local ba, dc = term.getSize()
  86.     term.setCursorPos(1, dc-1)
  87.     term.setTextColor(colors.gray)
  88.     print("Scrolling available..")
  89.     term.setTextColor(colors.white)
  90.     term.setCursorPos(ab, cd)
  91.   end
  92.  
  93.   -- Print the first set of lines that can fit on the screen
  94.   for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  95.     if i ~= 0 then
  96.       print(lines[i])
  97.     end
  98.   end
  99.  
  100.   -- Listen for mouse scroll to scroll up or down or click to end
  101.   sleep(0.1)
  102.   repeat
  103.     local eventData = {os.pullEvent()}
  104.     local event = eventData[1]
  105.     button = tonumber(eventData[2])
  106.     if button == -1 and currentLine > 1 then
  107.       -- Scroll up
  108.       currentLine = currentLine - 1
  109.       shaka.clearScreen()
  110.       term.setTextColor(colors.gray)
  111.       print(dateVar .. " - " .. timeVar)
  112.       shaka.changeColors(colors.black, colors.lightBlue)
  113.       if sender == receiver then
  114.         print("Note to Self")
  115.       else
  116.         print(sender .. " -> " .. receiver)
  117.       end
  118.       shaka.resetColors()
  119.       print()
  120.       for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  121.         print(lines[i])
  122.       end
  123.  
  124.     elseif button == 1 and currentLine < totalLines - maxLines + 1 then
  125.       -- Scroll down
  126.       currentLine = currentLine + 1
  127.       shaka.clearScreen()
  128.       term.setTextColor(colors.gray)
  129.       print(dateVar .. " - " .. timeVar)
  130.       shaka.changeColors(colors.black, colors.lightBlue)
  131.       if sender == receiver then
  132.         print("Note to Self")
  133.       else
  134.         print(sender .. " -> " .. receiver)
  135.       end
  136.       shaka.resetColors()
  137.       print()
  138.       for i = currentLine, math.min(totalLines, currentLine + maxLines - 1) do
  139.         print(lines[i])
  140.       end
  141.     end
  142.   until event == "mouse_click"
  143. end
  144.  
  145.  
  146. function convertDate(dateString)
  147.   local day, month, year = dateString:match("(%d+).(%d+).(%d+)")
  148.   return tonumber(day) .. "/" .. tonumber(month)
  149. end
  150.  
  151.  
  152. function drawMessages()
  153.   writeHeader()
  154.   if messageIndex == 1 then
  155.     local a, b = term.getSize()
  156.     term.setCursorPos(1, b - 1)
  157.     shaka.changeColors(colors.black, colors.gray)
  158.     if #messages > 15 then
  159.         print("Scroll and click..")
  160.     else
  161.         print("Click..")
  162.     end
  163. end
  164.  
  165.   term.setCursorPos(1, scrollListStart)
  166.   for i = messageIndex, messageIndex + maxMessagesDisplayed - 1 do
  167.     if messages[i] then
  168.       local message = messages[i]
  169.      
  170.       term.setBackgroundColor(colors.black)
  171.       term.setTextColor(colors.white)
  172.      
  173.       if message.sender == userName and message.receiver == userName then
  174.           term.setTextColor(colors.yellow)
  175.           term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
  176.           term.write("Note to Self")
  177.       elseif message.sender == userName then
  178.           term.setTextColor(colors.white)
  179.           term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
  180.           term.write(shaka.shortenString(message.receiver, 13))
  181.       else
  182.           term.setTextColor(colors.green)
  183.           term.write(convertDate(message.date) .. ", " ..message.time.. ": ")
  184.           term.write(shaka.shortenString(message.sender, 13))
  185.       end
  186.       local x, y = term.getCursorPos()
  187.       term.setCursorPos(1, y + 1)
  188.     end
  189.   end
  190. end
  191.  
  192. function handleScroll()
  193.   while true do
  194.     local event = {os.pullEvent()}
  195.     if event[1] == "mouse_scroll" then
  196.       if event[2] == -1 and messageIndex > 1 then
  197.         messageIndex = messageIndex - 1
  198.         term.scroll(1)
  199.       elseif event[2] == 1 and messageIndex + maxMessagesDisplayed - 1 < #messages then
  200.         messageIndex = messageIndex + 1
  201.         term.scroll(-1)
  202.       end
  203.     elseif event[1] == "mouse_click" and event[2] == 1 then
  204.       local x, y = term.getSize()
  205.       local clickX = event[3]
  206.       local clickY = event[4]
  207.       if clickY == 1 then
  208.         shell.run("messenger")
  209.         quit = true
  210.         return
  211.     end
  212.  
  213.       local selectedLine = clickY - 1 + messageIndex - scrollListStart + 1
  214.       if selectedLine >= messageIndex and selectedLine <= messageIndex + maxMessagesDisplayed - 1 and messages[selectedLine] then
  215.         local message = messages[selectedLine]
  216.         showMessage(message.text, message.date, message.time, shaka.shortenString(message.sender, 10), shaka.shortenString(message.receiver, 10))
  217.       end
  218.     end
  219.     drawMessages()
  220.   end
  221. end
  222.  
  223. while quit == false do
  224.   drawMessages()
  225.   handleScroll()
  226. end
  227.  
  228.  
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement