Object
Ask something to the user and receives a response.
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
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
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.
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
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
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
Prints a table.
Array[Array[String, String, …]]
ident | Indent the first column by ident value. |
colwidth | Force the first column to colwidth spaces wide. |
# File lib/bundler/vendor/thor/shell/basic.rb, line 91 91: def print_table(table, options={}) 92: return if table.empty? 93: 94: formats, ident, colwidth = [], options[:ident].to_i, options[:colwidth] 95: options[:truncate] = terminal_width if options[:truncate] == true 96: 97: formats << "%-#{colwidth + 2}s" if colwidth 98: start = colwidth ? 1 : 0 99: 100: start.upto(table.first.length - 2) do |i| 101: maxima ||= table.max{|a,b| a[i].size <=> b[i].size }[i].size 102: formats << "%-#{maxima + 2}s" 103: end 104: 105: formats[0] = formats[0].insert(0, " " * ident) 106: formats << "%s" 107: 108: table.each do |row| 109: sentence = "" 110: 111: row.each_with_index do |column, i| 112: sentence << formats[i] % column.to_s 113: end 114: 115: sentence = truncate(sentence, options[:truncate]) if options[:truncate] 116: $stdout.puts sentence 117: end 118: end
Prints a long string, word-wrapping the text to the current width of the terminal display. Ideal for printing heredocs.
String
ident | Indent each line of the printed paragraph by ident value. |
# File lib/bundler/vendor/thor/shell/basic.rb, line 129 129: def print_wrapped(message, options={}) 130: ident = options[:ident] || 0 131: width = terminal_width - ident 132: paras = message.split("\n\n") 133: 134: paras.map! do |unwrapped| 135: unwrapped.strip.gsub(/\n/, " ").squeeze(" "). 136: gsub(/.{1,#{width}}(?:\s|\Z)/){($& + 5.chr). 137: gsub(/\n\0005/,"\n").gsub(/\0005/,"\n")} 138: end 139: 140: paras.each do |para| 141: para.split("\n").each do |line| 142: $stdout.puts line.insert(0, " " * ident) 143: end 144: $stdout.puts unless para == paras.last 145: end 146: end
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).
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 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
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
# 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
# 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
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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.