Advertisement
wolfe_br

Rednet Monitor

Jun 23rd, 2023 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | Gaming | 0 0
  1. --[[
  2.   Wolfe's Rednet Monitor
  3.   Command: pastebin run a67ru7BV [protocol]
  4. ]]
  5.  
  6. --- Prints in multiple colors
  7. function print_colored (text_pairs)
  8.   for _, pair in pairs(text_pairs) do
  9.     term.setTextColor(pair[2] or colors.white)
  10.     term.write(pair[1])
  11.   end
  12.  
  13.   print('')
  14. end
  15.  
  16. --- Prints data with timestamp
  17. function log (data, header)
  18.   local time = os.date('%H:%M:%S')
  19.  
  20.   if header then
  21.     -- Renders header when needed
  22.     print_colored({
  23.       { ('[%s] '):format(time), colors.orange },
  24.       { header or '', colors.lime },
  25.     })
  26.    
  27.     term.setTextColor(colors.white)
  28.     print(data)
  29.   else
  30.     -- Writes on same line
  31.     print_colored({
  32.       { ('[%s] '):format(time), colors.orange },
  33.       { data },
  34.     })
  35.   end
  36. end
  37.  
  38. -- If any monitors are connected, redirects feed into them
  39. local monitor = peripheral.find('monitor')
  40. if monitor then
  41.   monitor.setTextScale(0.5)
  42.   term.redirect(monitor)
  43. end
  44.  
  45. -- Clears everything
  46. term.setCursorPos(1, 1)
  47. term.clear()
  48.  
  49. -- Opens our modem
  50. if peripheral.find('modem') then
  51.   peripheral.find('modem', rednet.open)
  52.   log('Connected to rednet!')
  53. else
  54.   error('No modem connected!')
  55. end
  56.  
  57. -- Gets our program arguments
  58. local args = {...}
  59. local protocol_filter = args[1] or nil
  60.  
  61. -- Lets user know which protocol is being listened
  62. if protocol_filter then
  63.   log(('Listening to protocol "%s"...'):format(protocol_filter))
  64. else
  65.   log('Listening to all messages...')
  66. end
  67.  
  68. print('')
  69.  
  70. -- Starts listening
  71. while true do
  72.   -- Pulls next message
  73.   local id, message = rednet.receive(protocol_filter)
  74.  
  75.   -- Suffix (to indicate what channel was sent to)
  76.   local suffix = ''
  77.   if protocol_filter then
  78.     suffix = (' @ %s'):format(protocol_filter)
  79.   end
  80.  
  81.   -- Prints message
  82.   log(message, ('from #%d%s:'):format(id, suffix))
  83.   print('')
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement