def choose_server(list)
highline = HighLine.new
proxy = false
choice = nil
nm_col_len = list.values.map(&:length).sort[-1] + 5
ur_col_len = list.keys.map(&:length).sort[-1] + 5
header = sprintf("| %-3s | %-#{nm_col_len}s | %-#{ur_col_len}s |", "", "name", "url")
border = ("-" * header.length)
table = [border, header, border]
list = list.to_a.map{|url, name| [URI.parse(url), name]}
list = filter_server_list(list)
list = sort_server_list(list)
list.each_with_index do |(url, name), idx|
table << sprintf("| %-2d | %-#{nm_col_len}s | %-#{ur_col_len}s |", idx + 1, name, url)
end
table << border
table = table.join("\n")
Kernel.puts table
while choice.nil?
if proxy
question = "(q) to quit; (r) to refresh (c) to connect\nproxy to: "
else
question = "(q) to quit; (r) to refresh (p) to proxy\nconnect to: "
end
if (choice = opts.delete(:proxy))
proxy = true
else
choice = opts.delete(:connect) || highline.ask(question)
proxy = false
end
return close_connection if ['q', 'quit', 'exit'].include?(choice.downcase)
if ['r', 'reload', 'refresh'].include?(choice.downcase)
send_server_list
return nil
end
if ['c', 'connect'].include?(choice.downcase)
proxy = false
choice = nil
next
end
if ['p', 'proxy'].include?(choice.downcase)
proxy = true
choice = nil
next
end
choice = choice.to_i > 0 ?
list[choice.to_i - 1] :
list.find{|(url, name)| url.to_s == choice || name == choice }
log.error("\033[31mserver not found\033[0m") unless choice
end
[choice, proxy]
end