Class: Twterm::Notifier

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

Instance Method Summary collapse

Methods included from Subscriber

included, #subscribe, #unsubscribe

Constructor Details

#initializeNotifier



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twterm/notifier.rb', line 12

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

  subscribe(Event::Favorite) do |e|
    break if e.source.id == e.authenticating_user.user_id

    msg = '@%s has favorited your tweet: %s' % [
      e.source.screen_name, e.target.text
    ]
    show_message(msg)
  end

  subscribe(Event::Notification) do |e|
    queue(e)
  end

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

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

Instance Method Details

#show(notification = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twterm/notifier.rb', line 46

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

  @window.clear

  if notification.is_a?(Event::Notification)
    fg_color, bg_color = notification.color

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

  @window.refresh
end

#show_message(message) ⇒ Object



40
41
42
43
44
# File 'lib/twterm/notifier.rb', line 40

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