Module: Lowline
- Defined in:
- lib/lowline.rb
Instance Method Summary collapse
- #ask(q, opts = {}) ⇒ Object
- #ask_for_many(plural_name, name = nil) ⇒ Object
- #ask_for_selection(stuff, name, to_string = :to_s) ⇒ Object
- #ask_multiline(q) ⇒ Object
- #ask_yon(q) ⇒ Object
Instance Method Details
#ask(q, opts = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lowline.rb', line 55 def ask q, opts={} default_s = case opts[:default] when nil; nil when ""; " (enter for none)" else; " (enter for #{opts[:default].inspect})" end tail = case q when /[:?]$/; " " when /[:?]\s+$/; "" else; ": " end while true print [q, default_s, tail].compact.join ans = gets.strip if opts[:default] ans = opts[:default] if ans.blank? else next if ans.blank? && !opts[:empty_ok] end break ans unless (opts[:restrict] && ans !~ opts[:restrict]) end end |
#ask_for_many(plural_name, name = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lowline.rb', line 100 def ask_for_many plural_name, name=nil name ||= plural_name.gsub(/s$/, "") stuff = [] while true puts puts "Current #{plural_name}:" if stuff.empty? puts "None!" else stuff.each_with_index { |c, i| puts " #{i + 1}) #{c}" } end puts ans = ask "(A)dd #{name}, (r)emove #{name}, or (d)one" case ans when "a", "A" ans = ask "#{name.ucfirst} name", "" stuff << ans unless ans =~ /^\s*$/ when "r", "R" ans = ask "Remove which component? (1--#{stuff.size})" stuff.delete_at(ans.to_i - 1) if ans when "d", "D" break end end stuff end |
#ask_for_selection(stuff, name, to_string = :to_s) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/lowline.rb', line 128 def ask_for_selection stuff, name, to_string=:to_s puts "Choose a #{name}:" stuff.each_with_index do |c, i| pretty = case to_string when Symbol c.send to_string when Proc to_string.call c else raise ArgumentError, "unknown to_string argument type; expecting Proc or Symbol" end puts " #{i + 1}) #{pretty}" end j = while true i = ask "#{name.ucfirst} (1--#{stuff.size})" break i.to_i if i && (1 .. stuff.size).member?(i.to_i) end stuff[j - 1] end |
#ask_multiline(q) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/lowline.rb', line 80 def ask_multiline q puts "#{q} (ctrl-d or . by itself to stop):" ans = "" while true print "> " line = gets break if line =~ /^\.$/ || line.nil? ans << line.strip + "\n" end ans.sub(/\n+$/, "") end |
#ask_yon(q) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/lowline.rb', line 92 def ask_yon q while true print "#{q} (y/n): " a = gets.strip break a if a =~ /^[yn]$/i end =~ /y/i end |