2017-07-21 14:04:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Widgets
|
2017-07-22 05:00:28 -04:00
|
|
|
class Chat < VPanel
|
|
|
|
class History < Base
|
2017-07-25 10:48:37 -04:00
|
|
|
private
|
|
|
|
|
2017-07-22 09:03:10 -04:00
|
|
|
def draw
|
2017-07-25 22:09:36 -04:00
|
|
|
window.clear
|
|
|
|
|
|
|
|
return if props[:messages].empty?
|
|
|
|
|
2017-07-21 14:08:16 -04:00
|
|
|
offset = 0
|
|
|
|
|
2017-07-26 11:05:03 -04:00
|
|
|
props[:messages].reverse_each do |msg|
|
2017-07-27 18:01:17 -04:00
|
|
|
offset += draw_message(
|
|
|
|
offset,
|
|
|
|
msg[:error] || msg[:out] && !msg[:received] && Time.now.utc - msg[:time] > 10,
|
|
|
|
msg[:out],
|
|
|
|
msg[:time].strftime('%H:%M:%S'),
|
|
|
|
msg[:name],
|
|
|
|
msg[:text],
|
|
|
|
)
|
2017-07-21 14:08:16 -04:00
|
|
|
|
2017-07-25 10:36:28 -04:00
|
|
|
break if offset >= props[:height]
|
2017-07-21 14:08:16 -04:00
|
|
|
end
|
2017-07-21 14:04:59 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 17:40:59 -04:00
|
|
|
def draw_message(offset, error, out, time, name, text)
|
2017-07-26 03:27:17 -04:00
|
|
|
width = props[:width] / 3 * 2
|
|
|
|
left = out ? props[:width] - width : 0
|
|
|
|
|
|
|
|
lines = (text.length / width.to_f).ceil
|
|
|
|
|
|
|
|
1.upto lines do |line|
|
2017-07-26 11:30:19 -04:00
|
|
|
s = text[(width * (line - 1))...(width * line)].strip
|
2017-07-26 03:27:17 -04:00
|
|
|
|
|
|
|
if out && s.length != width
|
2017-07-26 11:26:49 -04:00
|
|
|
setpos left + width - s.length, props[:height] - offset - lines + line - 1
|
2017-07-26 03:27:17 -04:00
|
|
|
else
|
2017-07-26 11:26:49 -04:00
|
|
|
setpos left, props[:height] - offset - lines + line - 1
|
2017-07-26 03:27:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
addstr s
|
|
|
|
end
|
|
|
|
|
2017-07-27 17:40:59 -04:00
|
|
|
draw_header props[:height] - offset - lines - 1, error, out, time, name
|
2017-07-26 11:26:49 -04:00
|
|
|
|
2017-07-26 03:27:17 -04:00
|
|
|
1 + lines
|
|
|
|
end
|
|
|
|
|
2017-07-27 17:40:59 -04:00
|
|
|
def draw_header(y, error, out, name, time)
|
2017-07-26 03:19:33 -04:00
|
|
|
if out
|
2017-07-27 17:40:59 -04:00
|
|
|
setpos props[:width] - name.length - time.length - (error ? 3 : 1), y
|
|
|
|
|
|
|
|
if error
|
|
|
|
Style.default.message_error window do
|
|
|
|
addstr 'x'
|
|
|
|
end
|
|
|
|
|
|
|
|
addstr ' '
|
|
|
|
end
|
2017-07-21 14:08:16 -04:00
|
|
|
|
2017-07-26 09:05:30 -04:00
|
|
|
Style.default.message_time window do
|
|
|
|
addstr time
|
2017-07-26 03:19:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
addstr ' '
|
|
|
|
|
2017-07-26 09:05:30 -04:00
|
|
|
Style.default.message_author window do
|
|
|
|
addstr name
|
2017-07-26 03:19:33 -04:00
|
|
|
end
|
|
|
|
else
|
2017-07-26 11:26:49 -04:00
|
|
|
setpos 0, y
|
2017-07-26 03:19:33 -04:00
|
|
|
|
2017-07-26 09:05:30 -04:00
|
|
|
Style.default.message_author window do
|
|
|
|
addstr name
|
2017-07-26 03:19:33 -04:00
|
|
|
end
|
2017-07-22 05:46:15 -04:00
|
|
|
|
2017-07-26 03:19:33 -04:00
|
|
|
addstr ' '
|
2017-07-22 05:46:15 -04:00
|
|
|
|
2017-07-26 09:05:30 -04:00
|
|
|
Style.default.message_time window do
|
|
|
|
addstr time
|
2017-07-26 03:19:33 -04:00
|
|
|
end
|
2017-07-27 17:40:59 -04:00
|
|
|
|
|
|
|
if error
|
|
|
|
addstr ' '
|
|
|
|
|
|
|
|
Style.default.message_error window do
|
|
|
|
addstr 'x'
|
|
|
|
end
|
|
|
|
end
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
2017-07-21 14:08:16 -04:00
|
|
|
end
|
2017-07-21 14:04:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|