Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Wolfe's Rednet Monitor
- Command: pastebin run a67ru7BV [protocol]
- ]]
- --- Prints in multiple colors
- function print_colored (text_pairs)
- for _, pair in pairs(text_pairs) do
- term.setTextColor(pair[2] or colors.white)
- term.write(pair[1])
- end
- print('')
- end
- --- Prints data with timestamp
- function log (data, header)
- local time = os.date('%H:%M:%S')
- if header then
- -- Renders header when needed
- print_colored({
- { ('[%s] '):format(time), colors.orange },
- { header or '', colors.lime },
- })
- term.setTextColor(colors.white)
- print(data)
- else
- -- Writes on same line
- print_colored({
- { ('[%s] '):format(time), colors.orange },
- { data },
- })
- end
- end
- -- If any monitors are connected, redirects feed into them
- local monitor = peripheral.find('monitor')
- if monitor then
- monitor.setTextScale(0.5)
- term.redirect(monitor)
- end
- -- Clears everything
- term.setCursorPos(1, 1)
- term.clear()
- -- Opens our modem
- if peripheral.find('modem') then
- peripheral.find('modem', rednet.open)
- log('Connected to rednet!')
- else
- error('No modem connected!')
- end
- -- Gets our program arguments
- local args = {...}
- local protocol_filter = args[1] or nil
- -- Lets user know which protocol is being listened
- if protocol_filter then
- log(('Listening to protocol "%s"...'):format(protocol_filter))
- else
- log('Listening to all messages...')
- end
- print('')
- -- Starts listening
- while true do
- -- Pulls next message
- local id, message = rednet.receive(protocol_filter)
- -- Suffix (to indicate what channel was sent to)
- local suffix = ''
- if protocol_filter then
- suffix = (' @ %s'):format(protocol_filter)
- end
- -- Prints message
- log(message, ('from #%d%s:'):format(id, suffix))
- print('')
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement