Module Commander::UI
In: lib/commander/user_interaction.rb
lib/commander/user_interaction.rb

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.

Methods

applescript   applescript   ask_editor   ask_editor   choose   choose   converse   converse   enable_paging   enable_paging   io   io   log   log   password   password   progress   progress   reset_io   reset_io   speak   speak  

Included Modules

Growl Growl

Classes and Modules

Module Commander::UI::AskForClass
Class Commander::UI::ProgressBar

Public Instance methods

Execute apple script.

[Source]

     # File lib/commander/user_interaction.rb, line 123
123:     def applescript script
124:       `osascript -e "#{ script.gsub('"', '\"') }"`
125:     end

Execute apple script.

[Source]

     # File lib/commander/user_interaction.rb, line 123
123:     def applescript script
124:       `osascript -e "#{ script.gsub('"', '\"') }"`
125:     end

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'

[Source]

     # File lib/commander/user_interaction.rb, line 182
182:     def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate'
183:       IO.popen(editor.to_s, 'w+') do |pipe|
184:         pipe.puts input.to_s unless input.nil?
185:         pipe.close_write
186:         pipe.read
187:       end
188:     end

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'

[Source]

     # File lib/commander/user_interaction.rb, line 182
182:     def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate'
183:       IO.popen(editor.to_s, 'w+') do |pipe|
184:         pipe.puts input.to_s unless input.nil?
185:         pipe.close_write
186:         pipe.read
187:       end
188:     end

Choose from a set array of choices.

[Source]

    # File lib/commander/user_interaction.rb, line 41
41:     def choose message, *choices
42:       say message
43:       super *choices
44:     end

Choose from a set array of choices.

[Source]

    # File lib/commander/user_interaction.rb, line 41
41:     def choose message, *choices
42:       say message
43:       super *choices
44:     end

Converse with speech recognition.

Currently a "poorman‘s" DSL to utilize applescript and the MacOS speech recognition server.

Examples

  case converse 'What is the best food?', :cookies => 'Cookies', :unknown => 'Nothing'
  when :cookies
    speak 'o.m.g. you are awesome!'
  else
    case converse 'That is lame, shall I convince you cookies are the best?', :yes => 'Ok', :no => 'No', :maybe => 'Maybe another time'
    when :yes
      speak 'Well you see, cookies are just fantastic.'
    else
      speak 'Ok then, bye.'
    end
  end

Notes

  • MacOS only

[Source]

     # File lib/commander/user_interaction.rb, line 103
103:     def converse prompt, responses = {}
104:       i, commands = 0, responses.map { |key, value| value.inspect }.join(',')
105:       statement = responses.inject '' do |statement, (key, value)|
106:         statement << (((i += 1) == 1 ? 
107:           %(if response is "#{value}" then\n):
108:             %(else if response is "#{value}" then\n))) <<
109:               %(do shell script "echo '#{key}'"\n)
110:       end
111:       applescript(%(
112:         tell application "SpeechRecognitionServer" 
113:           set response to listen for {#{commands}} with prompt "#{prompt}"
114:           #{statement}
115:           end if
116:         end tell
117:       )).strip.to_sym
118:     end

Converse with speech recognition.

Currently a "poorman‘s" DSL to utilize applescript and the MacOS speech recognition server.

Examples

  case converse 'What is the best food?', :cookies => 'Cookies', :unknown => 'Nothing'
  when :cookies
    speak 'o.m.g. you are awesome!'
  else
    case converse 'That is lame, shall I convince you cookies are the best?', :yes => 'Ok', :no => 'No', :maybe => 'Maybe another time'
    when :yes
      speak 'Well you see, cookies are just fantastic.'
    else
      speak 'Ok then, bye.'
    end
  end

Notes

  • MacOS only

[Source]

     # File lib/commander/user_interaction.rb, line 103
103:     def converse prompt, responses = {}
104:       i, commands = 0, responses.map { |key, value| value.inspect }.join(',')
105:       statement = responses.inject '' do |statement, (key, value)|
106:         statement << (((i += 1) == 1 ? 
107:           %(if response is "#{value}" then\n):
108:             %(else if response is "#{value}" then\n))) <<
109:               %(do shell script "echo '#{key}'"\n)
110:       end
111:       applescript(%(
112:         tell application "SpeechRecognitionServer" 
113:           set response to listen for {#{commands}} with prompt "#{prompt}"
114:           #{statement}
115:           end if
116:         end tell
117:       )).strip.to_sym
118:     end

Enable paging of output after called.

[Source]

     # File lib/commander/user_interaction.rb, line 193
193:     def enable_paging
194:       return unless $stdout.tty?
195:       return if Platform::jruby? # Fork is not supported by JRuby
196:       read, write = IO.pipe
197: 
198:       if Kernel.fork
199:         $stdin.reopen read
200:         read.close; write.close
201:         Kernel.select [$stdin]
202:         ENV['LESS'] = 'FSRX'
203:         pager = ENV['PAGER'] || 'less'
204:         exec pager rescue exec '/bin/sh', '-c', pager
205:       else
206:         $stdout.reopen write
207:         $stderr.reopen write if $stderr.tty?
208:         read.close; write.close
209:         return
210:       end
211:     end

Enable paging of output after called.

[Source]

     # File lib/commander/user_interaction.rb, line 193
193:     def enable_paging
194:       return unless $stdout.tty?
195:       return if Platform::jruby? # Fork is not supported by JRuby
196:       read, write = IO.pipe
197: 
198:       if Kernel.fork
199:         $stdin.reopen read
200:         read.close; write.close
201:         Kernel.select [$stdin]
202:         ENV['LESS'] = 'FSRX'
203:         pager = ENV['PAGER'] || 'less'
204:         exec pager rescue exec '/bin/sh', '-c', pager
205:       else
206:         $stdout.reopen write
207:         $stderr.reopen write if $stderr.tty?
208:         read.close; write.close
209:         return
210:       end
211:     end

Normalize IO streams, allowing for redirection of input and/or output, for example:

  $ foo              # => read from terminal I/O
  $ foo in           # => read from 'in' file, output to terminal output stream
  $ foo in out       # => read from 'in' file, output to 'out' file
  $ foo < in > out   # => equivalent to above (essentially)

Optionally a block may be supplied, in which case IO will be reset once the block has executed.

Examples

  command :foo do |c|
    c.syntax = 'foo [input] [output]'
    c.when_called do |args, options|
      # or io(args.shift, args.shift)
      io *args
      str = $stdin.gets
      puts 'input was: ' + str.inspect
    end
  end

[Source]

     # File lib/commander/user_interaction.rb, line 152
152:     def io input = nil, output = nil, &block
153:       $stdin = File.new(input) if input
154:       $stdout = File.new(output, 'r+') if output
155:       if block
156:         yield
157:         reset_io
158:       end
159:     end

Normalize IO streams, allowing for redirection of input and/or output, for example:

  $ foo              # => read from terminal I/O
  $ foo in           # => read from 'in' file, output to terminal output stream
  $ foo in out       # => read from 'in' file, output to 'out' file
  $ foo < in > out   # => equivalent to above (essentially)

Optionally a block may be supplied, in which case IO will be reset once the block has executed.

Examples

  command :foo do |c|
    c.syntax = 'foo [input] [output]'
    c.when_called do |args, options|
      # or io(args.shift, args.shift)
      io *args
      str = $stdin.gets
      puts 'input was: ' + str.inspect
    end
  end

[Source]

     # File lib/commander/user_interaction.rb, line 152
152:     def io input = nil, output = nil, &block
153:       $stdin = File.new(input) if input
154:       $stdout = File.new(output, 'r+') if output
155:       if block
156:         yield
157:         reset_io
158:       end
159:     end

‘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

[Source]

    # File lib/commander/user_interaction.rb, line 55
55:     def log action, *args
56:       say '%15s  %s' % [action, args.join(' ')]
57:     end

‘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

[Source]

    # File lib/commander/user_interaction.rb, line 55
55:     def log action, *args
56:       say '%15s  %s' % [action, args.join(' ')]
57:     end

Ask the user for a password. Specify a custom message other than ‘Password: ’ or override the default mask of ’*’.

[Source]

    # File lib/commander/user_interaction.rb, line 32
32:     def password message = 'Password: ', mask = '*'
33:       pass = ask(message) { |q| q.echo = mask }
34:       pass = password message, mask if pass.nil? || pass.empty?
35:       pass
36:     end

Ask the user for a password. Specify a custom message other than ‘Password: ’ or override the default mask of ’*’.

[Source]

    # File lib/commander/user_interaction.rb, line 32
32:     def password message = 'Password: ', mask = '*'
33:       pass = ask(message) { |q| q.echo = mask }
34:       pass = password message, mask if pass.nil? || pass.empty?
35:       pass
36:     end

Output progress while iterating arr.

Examples

  uris = %w( http://vision-media.ca http://google.com )
  progress uris, :format => "Remaining: :time_remaining" do |uri|
    res = open uri
  end

[Source]

     # File lib/commander/user_interaction.rb, line 224
224:     def progress arr, options = {}, &block
225:       bar = ProgressBar.new arr.length, options
226:       bar.show
227:       arr.each { |v| bar.increment yield(v) }
228:     end

Output progress while iterating arr.

Examples

  uris = %w( http://vision-media.ca http://google.com )
  progress uris, :format => "Remaining: :time_remaining" do |uri|
    res = open uri
  end

[Source]

     # File lib/commander/user_interaction.rb, line 224
224:     def progress arr, options = {}, &block
225:       bar = ProgressBar.new arr.length, options
226:       bar.show
227:       arr.each { |v| bar.increment yield(v) }
228:     end

Reset IO to initial constant streams.

[Source]

     # File lib/commander/user_interaction.rb, line 164
164:     def reset_io
165:       $stdin, $stdout = STDIN, STDOUT
166:     end

Reset IO to initial constant streams.

[Source]

     # File lib/commander/user_interaction.rb, line 164
164:     def reset_io
165:       $stdin, $stdout = STDIN, STDOUT
166:     end

Speak message using voice which defaults to ‘Alex’, which is one of the better voices.

Examples

  speak 'What is your favorite food? '
  food = ask 'favorite food?: '
  speak "wow, I like #{food} too. We have so much alike."

Notes

  • MacOS only

[Source]

    # File lib/commander/user_interaction.rb, line 74
74:     def speak message, voice = :Alex
75:       Thread.new { applescript "say #{message.inspect} using #{voice.to_s.inspect}" }
76:     end

Speak message using voice which defaults to ‘Alex’, which is one of the better voices.

Examples

  speak 'What is your favorite food? '
  food = ask 'favorite food?: '
  speak "wow, I like #{food} too. We have so much alike."

Notes

  • MacOS only

[Source]

    # File lib/commander/user_interaction.rb, line 74
74:     def speak message, voice = :Alex
75:       Thread.new { applescript "say #{message.inspect} using #{voice.to_s.inspect}" }
76:     end

[Validate]