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.rb

74 lines
1.6 KiB
Ruby
Raw Normal View History

2017-07-21 11:19:59 -04:00
# frozen_string_literal: true
module Widgets
class Chat
attr_reader :x, :y, :width, :height
2017-07-21 13:36:20 -04:00
attr_accessor :focused
2017-07-21 12:16:49 -04:00
attr_reader :messages
2017-07-21 11:19:59 -04:00
def initialize(x, y, width, height)
@x = x
@y = y
@width = width
@height = height
2017-07-21 12:16:49 -04:00
2017-07-21 13:36:20 -04:00
@focused = false
2017-07-21 12:16:49 -04:00
@messages = 1.upto(100).map do
{
time: Faker::Time.forward,
name: Faker::Name.name,
text: Faker::Lorem.sentence.freeze,
}
end
2017-07-21 11:19:59 -04:00
end
def render
2017-07-21 12:16:49 -04:00
offset = 0
messages.each do |msg|
time = msg[:time].strftime '%H:%M:%S'
name = msg[:name]
text = msg[:text]
offset = render_message offset, time, name, text
break if offset >= height
end
end
def render_message(offset, time, name, text)
Curses.setpos y + offset, x
info_length = time.length + 1 + name.length + 2
head_length = width - info_length
head = text[0...head_length]
2017-07-21 12:23:15 -04:00
Curses.attron Curses.color_pair 6
2017-07-21 12:16:49 -04:00
Curses.addstr time
Curses.addstr ' '
2017-07-21 12:23:15 -04:00
Curses.attron Curses.color_pair 7
Curses.attron Curses::A_BOLD
2017-07-21 12:16:49 -04:00
Curses.addstr name
Curses.addstr ': '
2017-07-21 12:23:15 -04:00
Curses.attroff Curses::A_BOLD
Curses.attron Curses.color_pair 1
2017-07-21 12:16:49 -04:00
Curses.addstr head
tail_length = [0, text.length - head_length].max
tail = text[head_length..-1]
lines = (tail_length / width.to_f).ceil
1.upto lines do |line|
Curses.setpos y + offset + line, x
Curses.addstr tail[(width * (line - 1))...(width * line)]
end
offset + 1 + lines
2017-07-21 11:19:59 -04:00
end
2017-07-21 13:36:20 -04:00
def trigger(event); end
2017-07-21 11:19:59 -04:00
end
end