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



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sshmenu/selector.rb', line 35

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

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

#raise_invalidObject

Raises:

  • (ArgumentError)


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

def raise_invalid
  raise ArgumentError, 'selection must be an index from the list'
end

#request_selectionObject



29
30
31
32
33
# File 'lib/sshmenu/selector.rb', line 29

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
22
23
# 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
    raise_invalid unless index.between?(0, @connections.length - 1)
    {:index => index}
  when /\A\z/
    {}
  else
    raise_invalid
  end
end