Ask something to the user and receives a response.
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
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
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/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 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
Check if base is muted
# File lib/thor/shell/basic.rb, line 25 25: def mute? 26: @mute 27: end
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
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
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/thor/shell/basic.rb, line 106 106: def print_table(table, options={}) 107: return if table.empty? 108: 109: formats, ident, colwidth = [], options[:ident].to_i, options[:colwidth] 110: options[:truncate] = terminal_width if options[:truncate] == true 111: 112: formats << "%-#{colwidth + 2}s" if colwidth 113: start = colwidth ? 1 : 0 114: 115: start.upto(table.first.length - 2) do |i| 116: maxima ||= table.max{|a,b| a[i].size <=> b[i].size }[i].size 117: formats << "%-#{maxima + 2}s" 118: end 119: 120: formats[0] = formats[0].insert(0, " " * ident) 121: formats << "%s" 122: 123: table.each do |row| 124: sentence = "" 125: 126: row.each_with_index do |column, i| 127: sentence << formats[i] % column.to_s 128: end 129: 130: sentence = truncate(sentence, options[:truncate]) if options[:truncate] 131: $stdout.puts sentence 132: end 133: 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/thor/shell/basic.rb, line 144 144: def print_wrapped(message, options={}) 145: ident = options[:ident] || 0 146: width = terminal_width - ident 147: paras = message.split("\n\n") 148: 149: paras.map! do |unwrapped| 150: unwrapped.strip.gsub(/\n/, " ").squeeze(" "). 151: gsub(/.{1,#{width}}(?:\s|\Z)/){($& + 5.chr). 152: gsub(/\n\0005/,"\n").gsub(/\0005/,"\n")} 153: end 154: 155: paras.each do |para| 156: para.split("\n").each do |line| 157: $stdout.puts line.insert(0, " " * ident) 158: end 159: $stdout.puts unless para == paras.last 160: end 161: 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/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 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
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
# 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
# File lib/thor/shell/basic.rb, line 272 272: def dynamic_width_tput 273: %{tput cols 2>/dev/null}.to_i 274: end
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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.