Module: Amp::UI
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
-
#ask(question = '', type = String) ⇒ String
Ask
question
and return the answer, chomped. -
#choose {|menu| ... } ⇒ Object
Produces a menu for the user.
-
#debug(message = '') ⇒ Object
Prints this message, only if the debug flag is set.
-
#edit(text = "", username = "") ⇒ String
Opens the editor for editing.
-
#edit_file(path) ⇒ Boolean
Opens the user’s editor for the file at the given path, with no extra processing afterward.
-
#err(message = '') ⇒ NilClass
Prints
message
to standard error. -
#get_editor ⇒ String
Gets the editor for the current system using environment variables or the configuration files.
-
#get_pass ⇒ String
Gets the user’s password, while hiding it from prying eyes.
-
#note(note = '') ⇒ NilClass
Notes the message - ignored unless in debug mode.
- #prompt(message = '', type = String, default = nil) ⇒ String deprecated Deprecated.
-
#say(message = '') ⇒ NilClass
Prints
message
to standard out with a trailing newline. -
#status(update = '') ⇒ NilClass
Prints a status update.
-
#tell(message = '') ⇒ NilClass
Prints
message
to standard out without a trailing newline. -
#warn(warning) ⇒ Object
Prints a warning.
-
#yes_or_no(question = '') ⇒ Boolean
(also: #agree)
Ask a yes or no question (accepts a variety of inputs).
Methods included from Merges::MergeUI
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
66 67 68 |
# File 'lib/amp/support/amp_ui.rb', line 66 def config @config end |
Instance Method Details
#ask(question = '', type = String) ⇒ String
Ask question
and return the answer, chomped
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/amp/support/amp_ui.rb', line 165 def ask(question='', type=String) type = type.to_s.downcase.to_sym print question.to_s result = gets.chomp unless type == :password # type conversion case type when :string result when :integer, :fixnum, :bignum, :numeric result.to_i when :array result.split(',').map {|e| e.strip } when :password get_pass else raise abort("Don't know how to convert to type #{type}") end end |
#choose {|menu| ... } ⇒ Object
Produces a menu for the user. Home-rolled to avoid rubygems dependencies!
86 87 88 89 90 |
# File 'lib/amp/support/amp_ui.rb', line 86 def choose = UIMenu.new yield .run end |
#debug(message = '') ⇒ Object
Prints this message, only if the debug flag is set.
227 228 229 230 231 |
# File 'lib/amp/support/amp_ui.rb', line 227 def debug(='') if @config && @config["debug","messages", Boolean, false] say end end |
#edit(text = "", username = "") ⇒ String
Opens the editor for editing. Uses a temporary file to capture the user’s input. The user must have either HGEDITOR, AMPEDITOR, VISUAL, EDITOR, or the configuration “ui”->“editor” set. Or, it’ll just use vi.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/amp/support/amp_ui.rb', line 242 def edit(text="", username="") tempfile = Tempfile.new("amp-editor-") path = tempfile.path tempfile.write text tempfile.close ENV["AMPUSER"] = username edit_file path text = File.open(path) {|tf| tf.read } || '' FileUtils.safe_unlink path text.gsub!(/^AMP:.*\n/,"") text end |
#edit_file(path) ⇒ Boolean
Opens the user’s editor for the file at the given path, with no extra processing afterward.
218 219 220 221 |
# File 'lib/amp/support/amp_ui.rb', line 218 def edit_file(path) editor = get_editor system "#{editor} #{path.inspect}" end |
#err(message = '') ⇒ NilClass
Prints message
to standard error.
155 156 157 |
# File 'lib/amp/support/amp_ui.rb', line 155 def err(='') $stderr.puts .to_s end |
#get_editor ⇒ String
Gets the editor for the current system using environment variables or the configuration files.
282 283 284 285 |
# File 'lib/amp/support/amp_ui.rb', line 282 def get_editor return ENV["AMPEDITOR"] || ENV["HGEDITOR"] || (@config && @config["ui","editor"]) || ENV["VISUAL"] || ENV["EDITOR"] || "vi" end |
#get_pass ⇒ String
Gets the user’s password, while hiding it from prying eyes.
REQUIRES ‘stty`
98 99 100 101 102 103 104 |
# File 'lib/amp/support/amp_ui.rb', line 98 def get_pass system "stty -echo" pass = gets.chomp tell "\n" system "stty echo" pass end |
#note(note = '') ⇒ NilClass
Notes the message - ignored unless in debug mode
125 126 127 128 129 130 |
# File 'lib/amp/support/amp_ui.rb', line 125 def note(note='') return unless $display if !@config || @config["ui", "amp-show-notes", Boolean, false] say "note: #{note}" end end |
#prompt(message = '', type = String, default = nil) ⇒ String
Asks the user something.
270 271 272 273 274 275 |
# File 'lib/amp/support/amp_ui.rb', line 270 def prompt(='', type=String, default=nil) say .to_s response = STDIN.gets.strip response = default if response == "" return response end |
#say(message = '') ⇒ NilClass
Prints message
to standard out with a trailing newline
137 138 139 |
# File 'lib/amp/support/amp_ui.rb', line 137 def say(='') tell "#{.to_s}\n" end |
#status(update = '') ⇒ NilClass
Prints a status update. Can be disabled using configuration files. These are casual updates to let the user know the progress in an operation.
113 114 115 116 117 118 |
# File 'lib/amp/support/amp_ui.rb', line 113 def status(update='') return unless $display if !@config || @config["ui", "amp-show-status", Boolean, true] say "status: #{update}" end end |
#tell(message = '') ⇒ NilClass
Prints message
to standard out without a trailing newline.
146 147 148 |
# File 'lib/amp/support/amp_ui.rb', line 146 def tell(='') $stdout.print .to_s end |
#warn(warning) ⇒ Object
Prints a warning. Can be disabled using configuration files. These are informational in nature but imply the user did something wrong, or that something is impossible.
74 75 76 77 78 |
# File 'lib/amp/support/amp_ui.rb', line 74 def warn(warning) if !@config || @config["ui","amp-show-warnings",Boolean,true] err "warning: #{warning}" end end |
#yes_or_no(question = '') ⇒ Boolean Also known as: agree
Ask a yes or no question (accepts a variety of inputs)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/amp/support/amp_ui.rb', line 191 def yes_or_no(question='') result = ask(question.to_s + ' [y/n/r/h] ') case result.downcase[0, 1] when 'r' yes_or_no question # recurse when 'h' tell <<-EOS [y/n/r/h] means you can type anything starting with a 'y', 'n', 'r', or an 'h' to select those options. 'y'\tyes 'n'\tno 'r'\trepeat 'h'\thelp EOS yes_or_no question else result.downcase.start_with? 'y' end end |