Permalink
Browse files

Expose terminal_width as a public API

  • Loading branch information...
1 parent 248cbcc commit da247a2f4e4fe308d3887a564e3b733c98c854d3 @sferik sferik committed May 7, 2012
Showing with 92 additions and 96 deletions.
  1. +1 −1 lib/thor/shell.rb
  2. +91 −95 lib/thor/shell/basic.rb
View
@@ -23,7 +23,7 @@ def self.shell=(klass)
end
module Shell
- SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision]
+ SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision, :terminal_width]
autoload :Basic, 'thor/shell/basic'
autoload :Color, 'thor/shell/color'
View
@@ -244,6 +244,19 @@ def file_collision(destination)
end
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
+ else
+ result = unix? ? dynamic_width : 80
+ end
+ (result < 10) ? 80 : result
+ rescue
+ 80
+ end
+
# Called if something goes wrong during the execution. This is used by Thor
# internally and should not be used inside your scripts. If something went
# wrong, you can always raise an exception. If you raise a Thor::Error, it
@@ -260,36 +273,36 @@ def set_color(string, *args) #:nodoc:
string
end
- protected
+ protected
- def lookup_color(color)
- return color unless color.is_a?(Symbol)
- self.class.const_get(color.to_s.upcase)
- end
+ def lookup_color(color)
+ return color unless color.is_a?(Symbol)
+ self.class.const_get(color.to_s.upcase)
+ end
- def stdout
- $stdout
- end
+ def stdout
+ $stdout
+ end
- def stdin
- $stdin
- end
+ def stdin
+ $stdin
+ end
- def stderr
- $stderr
- end
+ def stderr
+ $stderr
+ end
- def is?(value) #:nodoc:
- value = value.to_s
+ def is?(value) #:nodoc:
+ value = value.to_s
- if value.size == 1
- /\A#{value}\z/i
- else
- /\A(#{value}|#{value[0,1]})\z/i
- end
+ if value.size == 1
+ /\A#{value}\z/i
+ else
+ /\A(#{value}|#{value[0,1]})\z/i
end
+ end
- def file_collision_help #:nodoc:
+ def file_collision_help #:nodoc:
<<HELP
Y - yes, overwrite
n - no, do not overwrite
@@ -298,96 +311,79 @@ def file_collision_help #:nodoc:
d - diff, show the differences between the old and the new
h - help, show this help
HELP
- end
-
- def show_diff(destination, content) #:nodoc:
- diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
-
- Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
- temp.write content
- temp.rewind
- system %(#{diff_cmd} "#{destination}" "#{temp.path}")
- end
- end
+ end
- def quiet? #:nodoc:
- mute? || (base && base.options[:quiet])
- end
+ def show_diff(destination, content) #:nodoc:
+ diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
- # 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
- else
- result = unix? ? dynamic_width : 80
- end
- (result < 10) ? 80 : result
- rescue
- 80
+ Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
+ temp.write content
+ temp.rewind
+ system %(#{diff_cmd} "#{destination}" "#{temp.path}")
end
+ end
- # Calculate the dynamic width of the terminal
- def dynamic_width
- @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
- end
+ def quiet? #:nodoc:
+ mute? || (base && base.options[:quiet])
+ end
- def dynamic_width_stty
- %x{stty size 2>/dev/null}.split[1].to_i
- end
+ # Calculate the dynamic width of the terminal
+ def dynamic_width
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
+ end
- def dynamic_width_tput
- %x{tput cols 2>/dev/null}.to_i
- end
+ def dynamic_width_stty
+ %x{stty size 2>/dev/null}.split[1].to_i
+ end
- def unix?
- RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
- end
+ def dynamic_width_tput
+ %x{tput cols 2>/dev/null}.to_i
+ end
- def truncate(string, width)
- as_unicode do
- chars = string.chars.to_a
+ def unix?
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
+ end
- if chars.length <= width
- chars.join
- else
- ( chars[0, width-3].join ) + "..."
- end
+ def truncate(string, width)
+ as_unicode do
+ chars = string.chars.to_a
+ if chars.length <= width
+ chars.join
+ else
+ ( 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"
- yield
- ensure
- $KCODE = old
- end
+ if "".respond_to?(:encode)
+ def as_unicode
+ yield
end
-
- def ask_simply(statement, color = nil)
- say("#{statement} ", color)
- stdin.gets.strip
+ else
+ def as_unicode
+ old, $KCODE = $KCODE, "U"
+ yield
+ ensure
+ $KCODE = old
end
+ end
- def ask_filtered(statement, answer_set, *args)
- correct_answer = nil
-
- until correct_answer
- answer = ask_simply("#{statement} #{answer_set.inspect}", *args)
-
- correct_answer = answer_set.include?(answer) ? answer : nil
-
- answers = answer_set.map(&:inspect).join(", ")
- say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
- end
+ def ask_simply(statement, color=nil)
+ say("#{statement} ", color)
+ stdin.gets.strip
+ end
- correct_answer
+ def ask_filtered(statement, answer_set, *args)
+ correct_answer = nil
+ until correct_answer
+ answer = ask_simply("#{statement} #{answer_set.inspect}", *args)
+ correct_answer = answer_set.include?(answer) ? answer : nil
+ answers = answer_set.map(&:inspect).join(", ")
+ say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
end
+ correct_answer
+ end
+
end
end
end

0 comments on commit da247a2

Please sign in to comment.