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-21 14:08:16 -04:00
|
|
|
attr_reader :messages
|
|
|
|
|
2017-07-21 14:04:59 -04:00
|
|
|
def initialize(x, y, width, height)
|
2017-07-22 05:00:28 -04:00
|
|
|
super
|
2017-07-21 14:08:16 -04:00
|
|
|
|
|
|
|
@messages = 1.upto(100).map do
|
|
|
|
{
|
2017-07-24 07:39:24 -04:00
|
|
|
out: rand <= 0.2,
|
2017-07-21 14:08:16 -04:00
|
|
|
time: Faker::Time.forward,
|
|
|
|
name: Faker::Name.name,
|
|
|
|
text: Faker::Lorem.sentence.freeze,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-22 09:03:10 -04:00
|
|
|
def draw
|
2017-07-21 14:08:16 -04:00
|
|
|
offset = 0
|
|
|
|
|
|
|
|
messages.each do |msg|
|
2017-07-24 07:39:24 -04:00
|
|
|
offset = draw_message offset, msg[:out], msg[:time].strftime('%H:%M:%S'), msg[:name], msg[:text]
|
2017-07-21 14:08:16 -04:00
|
|
|
|
|
|
|
break if offset >= height
|
|
|
|
end
|
2017-07-21 14:04:59 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 07:39:24 -04:00
|
|
|
def draw_message(offset, out, time, name, text)
|
2017-07-22 10:31:38 -04:00
|
|
|
setpos 0, offset
|
2017-07-21 14:08:16 -04:00
|
|
|
|
2017-07-24 05:52:42 -04:00
|
|
|
Style.default.message_time window do
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr time
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
|
|
|
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr ' '
|
2017-07-22 05:46:15 -04:00
|
|
|
|
2017-07-24 05:52:42 -04:00
|
|
|
Style.default.message_author window do
|
2017-07-22 14:46:50 -04:00
|
|
|
addstr name
|
2017-07-22 05:46:15 -04:00
|
|
|
end
|
2017-07-21 14:08:16 -04:00
|
|
|
|
2017-07-24 07:36:10 -04:00
|
|
|
lines = (text.length / width.to_f).ceil
|
2017-07-21 14:08:16 -04:00
|
|
|
|
|
|
|
1.upto lines do |line|
|
2017-07-22 10:31:38 -04:00
|
|
|
setpos 0, offset + line
|
2017-07-24 07:36:10 -04:00
|
|
|
addstr text[(width * (line - 1))...(width * line)]
|
2017-07-21 14:08:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
offset + 1 + lines
|
|
|
|
end
|
2017-07-21 14:04:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|