Skip to content
Snippets Groups Projects
Verified Commit 703c02f8 authored by Alexander Olofsson's avatar Alexander Olofsson
Browse files

Improve example bot formatting

parent bfa7b123
Branches
Tags
No related merge requests found
...@@ -61,14 +61,14 @@ class MatrixBot ...@@ -61,14 +61,14 @@ class MatrixBot
puts "[#{Time.now.strftime '%H:%M'}] <#{sender.id} in #{room.id} @ #{diff}ms> \"#{message.content[:body]}\"" puts "[#{Time.now.strftime '%H:%M'}] <#{sender.id} in #{room.id} @ #{diff}ms> \"#{message.content[:body]}\""
plaintext = '%<sender>s: Pong! (ping%<msg>s took %<time>u ms to arrive)' plaintext = '%<sender>s: Pong! (ping%<msg>s took %<time>s to arrive)'
html = '<a href="https://matrix.to/#/%<sender>s">%<sender>s</a>: Pong! (<a href="https://matrix.to/#/%<room>s/%<event>s">ping</a>%<msg>s took %<time>u ms to arrive)' html = '<a href="https://matrix.to/#/%<sender>s">%<sender>s</a>: Pong! (<a href="https://matrix.to/#/%<room>s/%<event>s">ping</a>%<msg>s took %<time>s to arrive)'
formatdata = { formatdata = {
sender: sender.id, sender: sender.id,
room: room.id, room: room.id,
event: message.event_id, event: message.event_id,
time: (diff * 1000).to_i, time: duration_format((diff * 1000).to_i),
msg: msgstr msg: msgstr
} }
...@@ -98,6 +98,59 @@ class MatrixBot ...@@ -98,6 +98,59 @@ class MatrixBot
def deep_copy(hash) def deep_copy(hash)
Marshal.load(Marshal.dump(hash)) Marshal.load(Marshal.dump(hash))
end end
MS_PER_DAY = 86_400_000.0
MS_PER_HOUR = 3_600_000.0
MS_PER_MINUTE = 60_000.0
MS_PER_SECOND = 1_000.0
def duration_format(duration_ms)
return "#{duration_ms} ms" if duration_ms <= 9000
timestr = ''
if duration_ms > MS_PER_DAY * 1.1
days = (duration_ms / MS_PER_DAY).floor
puts days
duration_ms -= days * MS_PER_DAY
puts duration_ms
if days.positive?
timestr += "#{days} days#{days > 1 ? 's' : ''} "
end
end
if duration_ms > MS_PER_HOUR * 1.1
hours = (duration_ms / MS_PER_HOUR).floor
puts hours
duration_ms -= hours * MS_PER_HOUR
puts duration_ms
if hours.positive?
timestr += 'and ' unless timestr.empty?
timestr += "#{hours} hour#{hours > 1 ? 's' : ''} "
end
end
if duration_ms > MS_PER_MINUTE * 1.1
minutes = (duration_ms / MS_PER_MINUTE).floor
puts minutes
duration_ms -= minutes * MS_PER_MINUTE
puts duration_ms
if minutes.positive?
timestr += 'and ' unless timestr.empty?
timestr += "#{minutes} minute#{minutes > 1 ? 's' : ''} "
end
end
seconds = (duration_ms / MS_PER_SECOND).round(timestr.empty? ? 1 : 0)
puts seconds
seconds = seconds.round if seconds.round == seconds
if seconds.positive?
timestr += 'and ' unless timestr.empty?
timestr += "#{seconds} second#{seconds > 1 ? 's' : ''} "
end
timestr.rstrip
end
end end
if $PROGRAM_NAME == __FILE__ if $PROGRAM_NAME == __FILE__
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment