Class: Salticid::Interface

Inherits:
Object show all
Defined in:
lib/salticid/interface.rb,
lib/salticid/interface/view.rb,
lib/salticid/interface/tab_view.rb,
lib/salticid/interface/host_view.rb,
lib/salticid/interface/resizable.rb

Defined Under Namespace

Modules: Resizeable Classes: HostView, TabView, View

Constant Summary collapse

COLORS =
{
  :info => Curses::COLOR_WHITE,
  :error => Curses::COLOR_RED,
  :warn => Curses::COLOR_YELLOW,
  :debug => Curses::COLOR_CYAN,
  :stderr => Curses::COLOR_MAGENTA,
  :finished => Curses::COLOR_GREEN
}
COLOR_PAIRS =
{}
KEY_SPACE =

Keys

32
KEY_ENTER =
13
KEY_SCROLL_UP =
258
KEY_SCROLL_DOWN =
259

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(salticid) ⇒ Interface

Returns a new instance of Interface.



38
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
# File 'lib/salticid/interface.rb', line 38

def initialize(salticid)
  self.class.interfaces << self


  # Set up ncurses
  Curses.init_screen
  Curses.cbreak
  Curses.noecho
  Curses.nonl
  # Gone in Ruby Curses because ???
  # Curses.stdscr.intrflush false
  Curses.stdscr.keypad true
  Curses.start_color
  Curses.use_default_colors

  @salticid = salticid
  @hosts = []
  @tabs = TabView.new(
    self,
    :height => 1
  )

  @tabs.window.keypad true

  colorize
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



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

def hosts
  @hosts
end

#salticidObject (readonly)

Returns the value of attribute salticid.



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

def salticid
  @salticid
end

Class Method Details

.interfacesObject



26
27
28
# File 'lib/salticid/interface.rb', line 26

def self.interfaces
  @interfaces ||= []
end

.shutdown(*args) ⇒ Object



30
31
32
33
34
# File 'lib/salticid/interface.rb', line 30

def self.shutdown *args
  @interfaces.each do |i|
    i.shutdown *args
  end
end

Instance Method Details

#add_tab(host) ⇒ Object

Add a new tab interface backed by source.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/salticid/interface.rb', line 66

def add_tab(host)
  @tabs.active.hide if tab

  hv = HostView.new(
    self,
    :host => host,
    :top => 1,
    :height => Curses.lines - 1
  )
  hv.on_state_change do |state|
    @tabs.render
  end
  @tabs << hv
  tab.show
end

#colorizeObject

Set up colors



83
84
85
86
87
88
89
# File 'lib/salticid/interface.rb', line 83

def colorize
  COLORS.each_with_index do |c, i|
    pair_num = i + 1
    Curses.init_pair pair_num, c.last, -1
    COLOR_PAIRS[c.first] = pair_num
  end
end

#delete_tab(target) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/salticid/interface.rb', line 91

def delete_tab(target)
  begin
    target.hide
    @tabs.delete target
  ensure
    tab.show if tab
  end
end

#joinObject

Join the mainloop.



101
102
103
# File 'lib/salticid/interface.rb', line 101

def join
  @main.join
end

#mainObject

Mainloop



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/salticid/interface.rb', line 106

def main
  @main = Thread.new do
    Thread.current.priority = -1
    main_thread = Thread.current
    trap("WINCH") { resize if main_thread.alive? }
    trap("SIGINT") { exit }

    loop do
      # Get character
      if IO.select [$stdin], nil, nil, 1
        char = @tabs.window.getch
      else
        Thread.pass
        next
      end

      File.open("/tmp/curses.log", "a") { |f|
        f.puts char.inspect
      }

      # Do stuff
      case char
      when 9 # tab
        @tabs.scroll
      when "q" # q
        shutdown
      when Curses::KEY_LEFT
        @tabs.scroll -1
      when Curses::KEY_RIGHT
        @tabs.scroll 1
      when Curses::KEY_PPAGE
        tab.scroll -tab.height / 2
      when Curses::KEY_NPAGE
        tab.scroll tab.height / 2
      when Curses::KEY_UP
        tab.scroll -1 
      when Curses::KEY_DOWN
        tab.scroll 1
      end
    end
  end
end

#resizeObject

Resize to fit display



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/salticid/interface.rb', line 150

def resize
  # We need to nuke ncurses to pick up the new dimensions
  Curses.def_prog_mode
  Curses.close_screen
  Curses.reset_prog_mode
  height, width = Curses.dimensions
   
  # Resize tabs 
  @tabs.resize(
   :width => width,
   :height => height
  )
  @tabs.render
end

#shutdown(and_exit = true) ⇒ Object

Shut down interface



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/salticid/interface.rb', line 166

def shutdown(and_exit = true)
  # Shut down views
  @tabs.shutdown
 
  # Shut down ncurses
  Curses.echo
  Curses.nocbreak
  Curses.nl
  Curses.close_screen

  # Stop interface
  @main.exit rescue nil

  # Exit if okay
  exit if and_exit
end

#switch(target = nil) ⇒ Object

Switch to a different conversation



184
185
186
187
188
189
190
191
192
# File 'lib/salticid/interface.rb', line 184

def switch(target = nil)
  if target
    # Switch to a specific tab
    @tabs.switch_to_label target
  else
    # Switch to the next tab
    @tabs.scroll
  end
end

#tabObject



194
195
196
# File 'lib/salticid/interface.rb', line 194

def tab
  @tabs.active
end