Module: InteractionsHelper

Defined in:
lib/sapis/interactions_helper.rb

Overview

<interactions_helper.rb> - Part of Sav’s APIs. Copyright © 2011 Saverio Miroddi

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Class Method Summary collapse

Class Method Details

.ask_entries_in_line(header, entries, default = nil) ⇒ Object

Displays a list of entries, in the format:

header: entry_a,entry_b [default]?


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sapis/interactions_helper.rb', line 126

def self.ask_entries_in_line(header, entries, default=nil)
  while true
    print "#{header}: "

    print entries.join(',')

    print " [#{default}]" if default

    print "? "

    answer = STDIN.gets.chomp

    if answer == '' && default
      break default
    elsif entries.include?(answer)
      break answer
    end
  end
end

.ask_entries_with_points(header, entries, options = {}) ⇒ Object

Displays a numbered (or user-choosen) list of entries, in the format:

entry_a) value_a
entry_b) value_b

or

0) value_a
1) value_b

depending on <entries> being respectively a Hash or an Array.

options:

:default:            default entry, if the user doesn't choose
:autochoose_if_one:  [false] automatically choose the entry if it's only one
:filter_by:          [nil] String (for simplicity), which is matched case-insensitively.
                     If there are more matches and the pattern matches exactly one of
                     them, it's automatically chosen.

Raises:

  • (ArgumentError)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sapis/interactions_helper.rb', line 69

def self.ask_entries_with_points(header, entries, options={})
  raise ArgumentError.new("No entries passed! [#{header}]") if entries.empty?

  default           = options[:default]
  autochoose_if_one = options[:autochoose_if_one]
  filtering_pattern = options[:filter_by]

  raise "Pattern must be a String, Regexp is not supported" if filtering_pattern.is_a?(Regexp)

  # Convert to Hash if it's an array
  #
  if entries.is_a?(Array)
    entries = (0 ... entries.size).zip(entries)

    entries = entries.inject({}) do |current_entries, (i, entry)|
      current_entries[i.to_s] = entry
      current_entries
    end
  end

  if filtering_pattern
    exact_matches = entries.select { |_, entry_value| entry_value.downcase == filtering_pattern.downcase }

    return exact_matches.values.first if exact_matches.size == 1

    entries = entries.select { |_, entry_value| entry_value.downcase.include?(filtering_pattern.downcase) }

    raise ArgumentError.new("No entries after filtering! [#{header}, #{filtering_pattern}]") if entries.empty?
  end

  if entries.size == 1 && (autochoose_if_one || filtering_pattern)
    return entries.values.first
  end

  while true
    puts "#{header}:"

    entries.each do |point, entry|
      print " #{point}"
      print default.to_s == entry ? '*' : ')'
      puts " #{entry}"
    end

    answer = STDIN.gets.chomp

    if answer == '' && default
      break default
    elsif entries.has_key?(answer)
      break entries[answer]
    end
  end
end

.ask_entry(header, default = nil) ⇒ Object

Asks a question, optionally using a default.

Format:

<header> [default]?


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sapis/interactions_helper.rb', line 34

def self.ask_entry(header, default=nil)
  while true
    print "#{header}"
    print " [#{default}]" if default
    print "? "

    answer = STDIN.gets.chomp

    if answer = '' && default
      return default
    elsif answer != ''
      return answer
    end
  end
end

.secure_ask(question = 'Insert password: ') ⇒ Object



24
25
26
# File 'lib/sapis/interactions_helper.rb', line 24

def self.secure_ask(question='Insert password: ')
  HighLine.new.ask(question) { |q| q.echo = '*' }
end