Parent

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/thor/shell/basic.rb, line 40
40:       def ask(statement, color=nil)
41:         say("#{statement} ", color)
42:         $stdin.gets.strip
43:       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/thor/shell/basic.rb, line 202
202:       def error(statement)
203:         $stderr.puts statement
204:       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/thor/shell/basic.rb, line 171
171:       def file_collision(destination)
172:         return true if @always_force
173:         options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
174: 
175:         while true
176:           answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
177: 
178:           case answer
179:             when is?(:yes), is?(:force), ""
180:               return true
181:             when is?(:no), is?(:skip)
182:               return false
183:             when is?(:always)
184:               return @always_force = true
185:             when is?(:quit)
186:               say 'Aborting...'
187:               raise SystemExit
188:             when is?(:diff)
189:               show_diff(destination, yield) if block_given?
190:               say 'Retrying...'
191:             else
192:               say file_collision_help
193:           end
194:         end
195:       end
mute() click to toggle source

Mute everything that’s inside given block

    # File lib/thor/shell/basic.rb, line 16
16:       def mute
17:         @mute = true
18:         yield
19:       ensure
20:         @mute = false
21:       end
mute?() click to toggle source

Check if base is muted

    # File lib/thor/shell/basic.rb, line 25
25:       def mute?
26:         @mute
27:       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/thor/shell/basic.rb, line 93
93:       def no?(statement, color=nil)
94:         !yes?(statement, color)
95:       end
padding=(value) click to toggle source

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

    # File lib/thor/shell/basic.rb, line 31
31:       def padding=(value)
32:         @padding = [0, value].max
33:       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/thor/shell/basic.rb, line 52
52:       def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/))
53:         message = message.to_s
54:         message = set_color(message, color) if color
55: 
56:         spaces = "  " * padding
57: 
58:         if force_new_line
59:           $stdout.puts(spaces + message)
60:         else
61:           $stdout.print(spaces + message)
62:         end
63:         $stdout.flush
64:       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/thor/shell/basic.rb, line 71
71:       def say_status(status, message, log_status=true)
72:         return if quiet? || log_status == false
73:         spaces = "  " * (padding + 1)
74:         color  = log_status.is_a?(Symbol) ? log_status : :green
75: 
76:         status = status.to_s.rjust(12)
77:         status = set_color status, color, true if color
78: 
79:         $stdout.puts "#{status}#{spaces}#{message}"
80:         $stdout.flush
81:       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/thor/shell/basic.rb, line 86
86:       def yes?(statement, color=nil)
87:         !!(ask(statement, color) =~ is?(:yes))
88:       end

Protected Instance Methods

dynamic_width() click to toggle source

Calculate the dynamic width of the terminal

     # File lib/thor/shell/basic.rb, line 264
264:         def dynamic_width
265:           @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
266:         end
dynamic_width_stty() click to toggle source
     # File lib/thor/shell/basic.rb, line 268
268:         def dynamic_width_stty
269:           %{stty size 2>/dev/null}.split[1].to_i
270:         end
dynamic_width_tput() click to toggle source
     # File lib/thor/shell/basic.rb, line 272
272:         def dynamic_width_tput
273:           %{tput cols 2>/dev/null}.to_i
274:         end
terminal_width() click to toggle source

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

     # File lib/thor/shell/basic.rb, line 252
252:         def terminal_width
253:           if ENV['THOR_COLUMNS']
254:             result = ENV['THOR_COLUMNS'].to_i
255:           else
256:             result = unix? ? dynamic_width : 80
257:           end
258:           (result < 10) ? 80 : result
259:         rescue
260:           80
261:         end
truncate(string, width) click to toggle source
     # File lib/thor/shell/basic.rb, line 280
280:         def truncate(string, width)
281:           if string.length <= width
282:             string
283:           else
284:             ( string[0, width-3] || "" ) + "..."
285:           end
286:         end
unix?() click to toggle source
     # File lib/thor/shell/basic.rb, line 276
276:         def unix?
277:           RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/
278:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.