Parent

Class Index [+]

Quicksearch

Thor::Shell::Basic

Attributes

base[RW]
padding[RW]

Public Instance Methods

ask(statement, color=nil) click to toggle source

Ask something to the user and receives a response.

Example

ask(“What is your name?”)

    # File lib/bundler/vendor/thor/shell/basic.rb, line 25
25:       def ask(statement, color=nil)
26:         say("#{statement} ", color)
27:         $stdin.gets.strip
28:       end
error(statement) click to toggle source

Called if something goes wrong during the execution. This is used by Thor internally and should not be used inside your scripts. If someone went wrong, you can always raise an exception. If you raise a Thor::Error, it will be rescued and wrapped in the method below.

     # File lib/bundler/vendor/thor/shell/basic.rb, line 187
187:       def error(statement)
188:         $stderr.puts statement
189:       end
file_collision(destination) click to toggle source

Deals with file collision and returns true if the file should be overwriten and false otherwise. If a block is given, it uses the block response as the content for the diff.

Parameters

destination

the destination file to solve conflicts

block

an optional block that returns the value to be used in diff

     # File lib/bundler/vendor/thor/shell/basic.rb, line 156
156:       def file_collision(destination)
157:         return true if @always_force
158:         options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
159: 
160:         while true
161:           answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
162: 
163:           case answer
164:             when is?(:yes), is?(:force), ""
165:               return true
166:             when is?(:no), is?(:skip)
167:               return false
168:             when is?(:always)
169:               return @always_force = true
170:             when is?(:quit)
171:               say 'Aborting...'
172:               raise SystemExit
173:             when is?(:diff)
174:               show_diff(destination, yield) if block_given?
175:               say 'Retrying...'
176:             else
177:               say file_collision_help
178:           end
179:         end
180:       end
no?(statement, color=nil) click to toggle source

Make a question the to user and returns true if the user replies “n” or “no”.

    # File lib/bundler/vendor/thor/shell/basic.rb, line 78
78:       def no?(statement, color=nil)
79:         !yes?(statement, color)
80:       end
padding=(value) click to toggle source

Sets the output padding, not allowing less than zero values.

    # File lib/bundler/vendor/thor/shell/basic.rb, line 16
16:       def padding=(value)
17:         @padding = [0, value].max
18:       end
say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/)) click to toggle source

Say (print) something to the user. If the sentence ends with a whitespace or tab character, a new line is not appended (print + flush). Otherwise are passed straight to puts (behavior got from Highline).

Example

say(“I know you knew that.”)

    # File lib/bundler/vendor/thor/shell/basic.rb, line 37
37:       def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/))
38:         message = message.to_s
39:         message = set_color(message, color) if color
40: 
41:         spaces = "  " * padding
42: 
43:         if force_new_line
44:           $stdout.puts(spaces + message)
45:         else
46:       $stdout.print(spaces + message)
47:         end
48:         $stdout.flush
49:       end
say_status(status, message, log_status=true) click to toggle source

Say a status with the given color and appends the message. Since this method is used frequently by actions, it allows nil or false to be given in log_status, avoiding the message from being shown. If a Symbol is given in log_status, it’s used as the color.

    # File lib/bundler/vendor/thor/shell/basic.rb, line 56
56:       def say_status(status, message, log_status=true)
57:         return if quiet? || log_status == false
58:         spaces = "  " * (padding + 1)
59:         color  = log_status.is_a?(Symbol) ? log_status : :green
60: 
61:         status = status.to_s.rjust(12)
62:         status = set_color status, color, true if color
63: 
64:         $stdout.puts "#{status}#{spaces}#{message}"
65:         $stdout.flush
66:       end
yes?(statement, color=nil) click to toggle source

Make a question the to user and returns true if the user replies “y” or “yes”.

    # File lib/bundler/vendor/thor/shell/basic.rb, line 71
71:       def yes?(statement, color=nil)
72:         ask(statement, color) =~ is?(:yes)
73:       end

Protected Instance Methods

dynamic_width() click to toggle source

Calculate the dynamic width of the terminal

     # File lib/bundler/vendor/thor/shell/basic.rb, line 249
249:         def dynamic_width
250:           @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
251:         end
dynamic_width_stty() click to toggle source
     # File lib/bundler/vendor/thor/shell/basic.rb, line 253
253:         def dynamic_width_stty
254:           %{stty size 2>/dev/null}.split[1].to_i
255:         end
dynamic_width_tput() click to toggle source
     # File lib/bundler/vendor/thor/shell/basic.rb, line 257
257:         def dynamic_width_tput
258:           %{tput cols 2>/dev/null}.to_i
259:         end
terminal_width() click to toggle source

This code was copied from Rake, available under MIT-LICENSE Copyright © 2003, 2004 Jim Weirich

     # File lib/bundler/vendor/thor/shell/basic.rb, line 237
237:         def terminal_width
238:           if ENV['THOR_COLUMNS']
239:             result = ENV['THOR_COLUMNS'].to_i
240:           else
241:             result = unix? ? dynamic_width : 80
242:           end
243:           (result < 10) ? 80 : result
244:         rescue
245:           80
246:         end
truncate(string, width) click to toggle source
     # File lib/bundler/vendor/thor/shell/basic.rb, line 265
265:         def truncate(string, width)
266:           if string.length <= width
267:             string
268:           else
269:             ( string[0, width-3] || "" ) + "..."
270:           end
271:         end
unix?() click to toggle source
     # File lib/bundler/vendor/thor/shell/basic.rb, line 261
261:         def unix?
262:           RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/
263:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.