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
18
# File 'lib/twterm/notifier.rb', line 6

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

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

Instance Method Details

#show(notification = nil) ⇒ Object



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/notifier.rb', line 39

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(1, 0)
      @window.addstr(' ' * @window.maxx)
      @window.setpos(1, 1)
      time = notification.time.strftime('[%H:%M:%S]')
      message = notification.show_with_width(@window.maxx)
      @window.addstr("#{time} #{message}")
    end
  end

  @window.with_color(:black, :green) do
    @window.setpos(0, 0)
    @window.addstr(' ' * @window.maxx)
    @window.setpos(0, 1)
    @window.addstr(@help)
  end

  @window.refresh
end

#show_error(message) ⇒ Object



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

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

#show_help(message) ⇒ Object



32
33
34
35
36
37
# File 'lib/twterm/notifier.rb', line 32

def show_help(message)
  return if @help == message

  @help = message
  show
end

#show_message(message) ⇒ Object



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

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