Class: SshMenu::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/sshmenu/selector.rb

Instance Method Summary collapse

Constructor Details

#initialize(connections) ⇒ Selector

Returns a new instance of Selector.



4
5
6
# File 'lib/sshmenu/selector.rb', line 4

def initialize(connections)
  @connections = connections
end

Instance Method Details

#connection_listObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sshmenu/selector.rb', line 29

def connection_list
  uh_length = @connections.map {|conn| conn['user_host'].length}.max
  index_length = @connections.size.to_s.length

  s = StringIO.new
  @connections.each.with_index do |connection, index|
    conn = connection['user_host']
    desc = connection['description']
    s << "%#{index_length}d) %-#{uh_length+3}s %s\n" % [ index+1, conn, desc]
  end
  s.string
end

#request_selectionObject



23
24
25
26
27
# File 'lib/sshmenu/selector.rb', line 23

def request_selection
  puts connection_list
  print 'Pick a server (or e to edit connections): '
  STDIN.gets.chomp
end

#selectObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/sshmenu/selector.rb', line 8

def select
  selection = request_selection
  
  case selection
  when /\A[eE]\z/
    {:action => :edit}
  when /\A[1-9][0-9]*\z/
    {:index => Integer(selection) - 1 }
  when /\A\z/
    {}
  else
    raise ArgumentError, "invalid selection '#{selection}'"
  end
end