Class: Twterm::Notifier

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

Instance Method Summary collapse

Constructor Details

#initializeNotifier

Returns a new instance of Notifier.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/twterm/notifier.rb', line 6

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

  Thread.new do
    while notification = @queue.pop
      show(notification)
      sleep 3
      show
    end
  end
end

Instance Method Details

#show(notification = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/twterm/notifier.rb', line 31

def show(notification = nil)
  loop do
    break unless closed?
    sleep 0.5
  end

  @window.clear

  if notification.is_a? Notification::Base
    @window.with_color(notification.fg_color, notification.bg_color) do
      @window.setpos(0, 0)
      @window.addstr(' ' * @window.maxx)
      @window.setpos(0, 1)
      time = notification.time.strftime('[%H:%M:%S]')
      message = notification.show_with_width(@window.maxx)
      @window.addstr("#{time} #{message}")
    end
  end

  @window.refresh
end

#show_error(message) ⇒ Object



19
20
21
22
23
# File 'lib/twterm/notifier.rb', line 19

def show_error(message)
  notification = Notification::Error.new(message)
  @queue.push(notification)
  self
end

#show_message(message) ⇒ Object



25
26
27
28
29
# File 'lib/twterm/notifier.rb', line 25

def show_message(message)
  notification = Notification::Message.new(message)
  @queue.push(notification)
  self
end