Module: Commander::UI
- Included in:
- Object
- Defined in:
- lib/commander/user_interaction.rb
Overview
User Interaction
Commander’s user interacton module mixes in common methods which extend HighLine’s functionality such as a unified password method rather than calling ask directly.
Defined Under Namespace
Modules: AskForClass Classes: ProgressBar
Instance Method Summary collapse
-
#ask_editor(input = nil, editor = ENV['EDITOR'] || 'mate') ⇒ Object
Prompt
editorfor input. -
#enable_paging ⇒ Object
Enable paging of output after called.
-
#log(action, *args) ⇒ Object
‘Log’ an action to the terminal.
-
#password(message = 'Password: ', mask = '*') ⇒ Object
Ask the user for a password.
Instance Method Details
#ask_editor(input = nil, editor = ENV['EDITOR'] || 'mate') ⇒ Object
Prompt editor for input. Optionally supply initial input which is written to the editor.
The editor defaults to the EDITOR environment variable when present, or ‘mate’ for TextMate.
Examples
ask_editor # => prompts EDITOR with no input
ask_editor('foo') # => prompts EDITOR with default text of 'foo'
ask_editor('foo', :mate) # => prompts TextMate with default text of 'foo'
52 53 54 55 56 57 58 |
# File 'lib/commander/user_interaction.rb', line 52 def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate' IO.popen(editor.to_s, 'w+') do |pipe| pipe.puts input.to_s unless input.nil? pipe.close_write pipe.read end end |
#enable_paging ⇒ Object
Enable paging of output after called.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/commander/user_interaction.rb', line 63 def enable_paging return unless $stdout.tty? read, write = IO.pipe if Kernel.fork $stdin.reopen read read.close; write.close Kernel.select [$stdin] ENV['LESS'] = 'FSRX' pager = ENV['PAGER'] || 'less' exec pager rescue exec '/bin/sh', '-c', pager else $stdout.reopen write $stderr.reopen write if $stderr.tty? read.close; write.close return end end |
#log(action, *args) ⇒ Object
‘Log’ an action to the terminal. This is typically used for verbose output regarding actions performed. For example:
create path/to/file.rb
remove path/to/old_file.rb
remove path/to/old_file2.rb
34 35 36 |
# File 'lib/commander/user_interaction.rb', line 34 def log action, *args say '%15s %s' % [action, args.join(' ')] end |
#password(message = 'Password: ', mask = '*') ⇒ Object
Ask the user for a password. Specify a custom message other than ‘Password: ’ or override the default mask of ‘*’.
19 20 21 22 23 |
# File 'lib/commander/user_interaction.rb', line 19 def password = 'Password: ', mask = '*' pass = ask() { |q| q.echo = mask } pass = password , mask if pass.empty? pass end |