Class: Twterm::MessageWindow

Inherits:
Object
  • Object
show all
Includes:
Singleton, Subscriber
Defined in:
lib/twterm/message_window.rb

Instance Method Summary collapse

Methods included from Subscriber

included, #subscribe, #unsubscribe

Constructor Details

#initializeMessageWindow

Returns a new instance of MessageWindow.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/twterm/message_window.rb', line 10

def initialize
  @window = Curses.stdscr.subwin(1, Curses.stdscr.maxx, Curses.stdscr.maxy - 1, 0)
  @queue = Queue.new

  subscribe(Event::Message::AbstractMessage) do |e|
    queue(e)
  end

  subscribe(Event::Screen::Resize, :resize)

  Thread.new do
    while message = @queue.pop # rubocop:disable Lint/AssignmentInCondition:
      show(message)
      sleep 3
      show
    end
  end
end

Instance Method Details

#show(message = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/twterm/message_window.rb', line 34

def show(message = nil)
  loop do
    break unless Curses.closed?
    sleep 0.5
  end

  @window.clear

  if message.is_a?(Event::Message::AbstractMessage)
    fg_color, bg_color =
      case message
      when Event::Message::Error
        [:white, :red]
      when Event::Message::Info
        [:black, :cyan]
      when Event::Message::Success
        [:black, :green]
      when Event::Message::Warning
        [:black, :yellow]
      end

    @window.with_color(fg_color, bg_color) do
      @window.setpos(0, 0)
      @window.addstr(' ' * @window.maxx)
      @window.setpos(0, 1)
      time = message.time.strftime('[%H:%M:%S]')
      body = message.body.gsub("\n", ' ')
      @window.addstr("#{time} #{body}")
    end
  end

  @window.refresh
end

#show_info(message) ⇒ Object



29
30
31
32
# File 'lib/twterm/message_window.rb', line 29

def show_info(message)
  @queue.push(Event::Message::Info.new(message))
  self
end