class SshMenu::Selector

Public Class Methods

new(connections) click to toggle source
# File lib/sshmenu/selector.rb, line 4
def initialize(connections)
  @connections = connections
end

Public Instance Methods

connection_list() click to toggle source
# 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_invalid() click to toggle source
# File lib/sshmenu/selector.rb, line 25
def raise_invalid
  raise ArgumentError, 'selection must be an index from the list'
end
request_selection() click to toggle source
# 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
select() click to toggle source
# 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