Module: Commander::UI

Included in:
Object
Defined in:
lib/commander/user_interaction.rb

Overview

User Interaction

Commander’s user interaction module mixes in common methods which extend HighLine’s functionality such as a password method rather than calling ask directly.

Defined Under Namespace

Modules: AskForClass Classes: ProgressBar

Instance Method Summary collapse

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'


64
65
66
67
68
69
70
# File 'lib/commander/user_interaction.rb', line 64

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_pagingObject

Enable paging of output after called.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/commander/user_interaction.rb', line 75

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


46
47
48
# File 'lib/commander/user_interaction.rb', line 46

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 ‘*’.



31
32
33
34
35
# File 'lib/commander/user_interaction.rb', line 31

def password message = 'Password: ', mask = '*'
  pass = ask(message) { |q| q.echo = mask }
  pass = password message, mask if pass.empty?
  pass
end