Class: Salticid::Interface::HostView

Inherits:
View show all
Defined in:
lib/salticid/interface/host_view.rb

Instance Attribute Summary collapse

Attributes inherited from View

#height, #left, #top, #width

Instance Method Summary collapse

Methods inherited from View

#hide, #show

Methods included from Resizeable

#resize

Constructor Details

#initialize(interface, params = {}) ⇒ HostView

Returns a new instance of HostView.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/salticid/interface/host_view.rb', line 7

def initialize(interface, params = {})
  @messages = []

  @scroll_position = -1

  super

  @host = params[:host]
  @host.on_log do |message|
    self << message
  end

  @state = nil
  @on_state_change = proc { |state| }
end

Instance Attribute Details

#messagesObject

Returns the value of attribute messages.



5
6
7
# File 'lib/salticid/interface/host_view.rb', line 5

def messages
  @messages
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/salticid/interface/host_view.rb', line 5

def state
  @state
end

#windowObject

Returns the value of attribute window.



5
6
7
# File 'lib/salticid/interface/host_view.rb', line 5

def window
  @window
end

Instance Method Details

#<<(message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/salticid/interface/host_view.rb', line 23

def <<(message)
  # Scroll if at bottom
  @scroll_position += 1 if @scroll_position == @messages.size - 1

  @messages << message

  if @state != message.severity
    @state = message.severity
    @on_state_change.call(@state)
  end
  
  render
end

#on_state_change(&block) ⇒ Object



37
38
39
# File 'lib/salticid/interface/host_view.rb', line 37

def on_state_change(&block)
  @on_state_change = block
end

#renderObject



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/salticid/interface/host_view.rb', line 41

def render
  return if @hidden

  @window.clear

  lines_left = @height
  message_i = @scroll_position
  while message_i >= 0
    # Message
    message = @messages[message_i]
    message_i -= 1

    # Time
    time = message.time.strftime "%H:%M:%S"

    text = message.text
    color = Interface::COLOR_PAIRS[message.severity]
    
    offset = time.length + 1

    width = @width - offset
    lines = text.scan(/[^\n]{1,#{width}}/m)

    # Put lines in reverse
    i = lines.size
    while i > 0
      i -= 1
      line = lines[i]

      lines_left -= 1
      break if lines_left < 0

      if i.zero?
        # Put top line
        @window.setpos lines_left, 0
        @window.addstr time + ' '
        @window.attron Curses::A_BOLD
        @window.color_set color if color
        @window.addstr line
      else
        # Put hanging line
        @window.attron Curses::A_BOLD
        @window.color_set color if color
        @window.setpos lines_left, offset
        @window.addstr line
      end
      @window.color_set Interface::COLOR_PAIRS[:info] if color
      @window.attroff Curses::A_BOLD

      unless @window.cursor[1] == 0
        # Clear rest of line
        @window.clrtoeol
      end
    end
  end
  @window.refresh
end

#scroll(delta) ⇒ Object

Scrolls the window by delta messages



104
105
106
107
108
109
110
111
112
113
# File 'lib/salticid/interface/host_view.rb', line 104

def scroll(delta)
  @scroll_position += delta
  if @scroll_position < 0
    @scroll_position = 0
  elsif @scroll_position >= @messages.size
    @scroll_position = @messages.size - 1
  end

  render
end

#shutdownObject



115
116
117
118
119
120
# File 'lib/salticid/interface/host_view.rb', line 115

def shutdown
  @host.on_log do |message|
    puts message.text
  end
  @host = nil
end

#to_sObject



99
100
101
# File 'lib/salticid/interface/host_view.rb', line 99

def to_s
  @host.name
end