Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/widgets/chat/history.rb

75 lines
1.5 KiB
Ruby
Raw Normal View History

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
window.clear
return if props[:messages].empty?
offset = 0
2017-07-26 11:05:03 -04:00
props[:messages].reverse_each do |msg|
2017-07-24 07:40:49 -04:00
offset += draw_message offset, msg[:out], msg[:time].strftime('%H:%M:%S'), msg[:name], msg[:text]
2017-07-25 10:36:28 -04:00
break if offset >= props[:height]
end
2017-07-21 14:04:59 -04:00
end
2017-07-26 03:19:33 -04:00
def draw_message(offset, out, time, name, text)
draw_header offset, out, time, name
width = props[:width] / 3 * 2
left = out ? props[:width] - width : 0
lines = (text.length / width.to_f).ceil
1.upto lines do |line|
s = text[(width * (line - 1))...(width * line)]
if out && s.length != width
setpos left + width - s.length, offset + line
else
setpos left, offset + line
end
addstr s
end
1 + lines
end
def draw_header(offset, out, name, time)
2017-07-26 03:19:33 -04:00
if out
setpos props[:width] - name.length - time.length - 1, offset
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
setpos 0, offset
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-22 05:46:15 -04:00
end
end
2017-07-21 14:04:59 -04:00
end
end
end