lib/thor/shell/basic.rb in thor-0.19.1 vs lib/thor/shell/basic.rb in thor-0.19.2

- old
+ new

@@ -1,18 +1,21 @@ require "tempfile" require "io/console" if RUBY_VERSION > "1.9.2" class Thor module Shell - class Basic # rubocop:disable ClassLength + class Basic attr_accessor :base attr_reader :padding # Initialize base, mute and padding to nil. # def initialize #:nodoc: - @base, @mute, @padding, @always_force = nil, false, 0, false + @base = nil + @mute = false + @padding = 0 + @always_force = false end # Mute everything that's inside given block # def mute @@ -22,20 +25,29 @@ @mute = false end # Check if base is muted # - def mute? # rubocop:disable TrivialAccessors + def mute? @mute end # Sets the output padding, not allowing less than zero values. # def padding=(value) @padding = [0, value].max end + # Sets the output padding while executing a block and resets it. + # + def indent(count = 1) + orig_padding = padding + self.padding = padding + count + yield + self.padding = orig_padding + end + # Asks something to the user and receives a response. # # If asked to limit the correct responses, you can pass in an # array of acceptable answers. If one of those is not supplied, # they will be shown a message stating that one of those answers @@ -146,11 +158,13 @@ # colwidth<Integer>:: Force the first column to colwidth spaces wide. # def print_table(array, options = {}) # rubocop:disable MethodLength return if array.empty? - formats, indent, colwidth = [], options[:indent].to_i, options[:colwidth] + formats = [] + indent = options[:indent].to_i + colwidth = options[:colwidth] options[:truncate] = terminal_width if options[:truncate] == true formats << "%-#{colwidth + 2}s" if colwidth start = colwidth ? 1 : 0 @@ -159,16 +173,16 @@ maximas = [] start.upto(colcount - 1) do |index| maxima = array.map { |row| row[index] ? row[index].to_s.size : 0 }.max maximas << maxima - if index == colcount - 1 - # Don't output 2 trailing spaces when printing the last column - formats << "%-s" - else - formats << "%-#{maxima + 2}s" - end + formats << if index == colcount - 1 + # Don't output 2 trailing spaces when printing the last column + "%-s" + else + "%-#{maxima + 2}s" + end end formats[0] = formats[0].insert(0, " " * indent) formats << "%s" @@ -176,19 +190,19 @@ sentence = "" row.each_with_index do |column, index| maxima = maximas[index] - if column.is_a?(Numeric) + f = if column.is_a?(Numeric) if index == row.size - 1 # Don't output 2 trailing spaces when printing the last column - f = "%#{maxima}s" + "%#{maxima}s" else - f = "%#{maxima}s " + "%#{maxima}s " end else - f = formats[index] + formats[index] end sentence << f % column.to_s end sentence = truncate(sentence, options[:truncate]) if options[:truncate] @@ -209,11 +223,11 @@ indent = options[:indent] || 0 width = terminal_width - indent paras = message.split("\n\n") paras.map! do |unwrapped| - unwrapped.strip.gsub(/\n/, " ").squeeze(" ").gsub(/.{1,#{width}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") } + unwrapped.strip.tr("\n", " ").squeeze(" ").gsub(/.{1,#{width}}(?:\s|\Z)/) { ($& + 5.chr).gsub(/\n\005/, "\n").gsub(/\005/, "\n") } end paras.each do |para| para.split("\n").each do |line| stdout.puts line.insert(0, " " * indent) @@ -228,11 +242,11 @@ # # ==== Parameters # destination<String>:: the destination file to solve conflicts # block<Proc>:: an optional block that returns the value to be used in diff # - def file_collision(destination) # rubocop:disable MethodLength + def file_collision(destination) return true if @always_force options = block_given? ? "[Ynaqdh]" : "[Ynaqh]" loop do answer = ask( @@ -247,11 +261,11 @@ return false when is?(:always) return @always_force = true when is?(:quit) say "Aborting..." - fail SystemExit + raise SystemExit when is?(:diff) show_diff(destination, yield) if block_given? say "Retrying..." else say file_collision_help @@ -260,14 +274,14 @@ end # This code was copied from Rake, available under MIT-LICENSE # Copyright (c) 2003, 2004 Jim Weirich def terminal_width - if ENV["THOR_COLUMNS"] - result = ENV["THOR_COLUMNS"].to_i + result = if ENV["THOR_COLUMNS"] + ENV["THOR_COLUMNS"].to_i else - result = unix? ? dynamic_width : 80 + unix? ? dynamic_width : 80 end result < 10 ? 80 : result rescue 80 end @@ -282,11 +296,11 @@ end # Apply color to the given string with optional bold. Disabled in the # Thor::Shell::Basic class. # - def set_color(string, *args) #:nodoc: + def set_color(string, *) #:nodoc: string end protected @@ -351,15 +365,15 @@ def dynamic_width @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput) end def dynamic_width_stty - %x(stty size 2>/dev/null).split[1].to_i + `stty size 2>/dev/null`.split[1].to_i end def dynamic_width_tput - %x(tput cols 2>/dev/null).to_i + `tput cols 2>/dev/null`.to_i end def unix? RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i end @@ -368,31 +382,32 @@ as_unicode do chars = string.chars.to_a if chars.length <= width chars.join else - ( chars[0, width - 3].join) + "..." + chars[0, width - 3].join + "..." end end end if "".respond_to?(:encode) def as_unicode yield end else def as_unicode - old, $KCODE = $KCODE, "U" + old = $KCODE + $KCODE = "U" yield ensure $KCODE = old end end def ask_simply(statement, color, options) default = options[:default] message = [statement, ("(#{default})" if default), nil].uniq.join(" ") - message = prepare_message(message, color) + message = prepare_message(message, *color) result = Thor::LineEditor.readline(message, options) return unless result result.strip!