Class: Hobostove::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/hobostove/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



3
4
5
# File 'lib/hobostove/window.rb', line 3

def messages
  @messages
end

Instance Method Details

#connectObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hobostove/window.rb', line 5

def connect
  @current_message = ""
  @running = true
  @messages = []

  begin
    start_curses
    load_users
    stream
    main
  rescue => e
    Hobostove.logger.error(e.inspect)
  ensure
    Hobostove.logger.debug("terminated")
    stop_curses
  end
end

#handle_message(message) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hobostove/window.rb', line 84

def handle_message(message)
  Hobostove.logger.debug(message.inspect)

  case message.type
  when "TextMessage"
    Notify.notify message.username, message.body
  when "EnterMessage"
    @users_panel.add_user(message.user)
  when "LeaveMessage"
    @users_panel.remove_user(message.user)
  when "UploadMessage"
    Notify.notify message.username, message.body
  end

  message_renderer.render_lines(message).each do |line|
    @messages_panel.<<(line, false)
  end
rescue => e
  Hobostove.logger.fatal(e.inspect)
end

#mainObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hobostove/window.rb', line 23

def main
  campfire.join

  while @running && (ch = Curses.getch)
    Hobostove.logger.debug("#{ch} - #{ch.chr.inspect} pressed")

    case ch
    when 14 # C+n
      @messages_panel.scroll_down
    when 16 # C+p
      @messages_panel.scroll_up
    when 10 # enter
      speak
    when 21 # C-u
      @current_message = ""
    when 127 # backspace
      @current_message = @current_message.first(@current_message.size - 1)
    when 9 # tab
      @current_message = "#{@users_panel.user_names.find { |user| user =~ /^#@current_message/ }}: "
    else
      @current_message << ch.chr
    end

    @message_panel << @current_message
    @message_panel.update_cursor
  end

  campfire.leave
end

#speakObject



53
54
55
56
57
58
59
60
61
# File 'lib/hobostove/window.rb', line 53

def speak
  if @current_message == "/quit"
    @running = false
    return
  end

  campfire.send_message @current_message
  @current_message = ""
end

#streamObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hobostove/window.rb', line 63

def stream
  Thread.new do
    loop do
      begin
        recent = campfire.recent_messages
        recent.each do |message|
          next if messages.include?(message.id)
          messages << message.id
          handle_message(message)
        end

        @messages_panel.refresh

        sleep 1
      rescue => e
        Hobostove.logger.error(e.inspect)
      end
    end
  end
end