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

#resizeObject



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

def resize
  @window.resize(1, stdscr.maxx)
  @window.move(stdscr.maxy - 2, 0)
end

#show(notification = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twterm/notifier.rb', line 36

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



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

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

#show_message(message) ⇒ Object



30
31
32
33
34
# File 'lib/twterm/notifier.rb', line 30

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