lib/travis/cli/command.rb in travis-1.0.3 vs lib/travis/cli/command.rb in travis-1.1.0

- old
+ new

@@ -1,6 +1,8 @@ require 'travis/cli' +require 'travis/tools/formatter' + require 'highline' require 'forwardable' require 'delegate' require 'yaml' @@ -9,25 +11,27 @@ class Command extend Parser extend Forwardable def_delegators :terminal, :agree, :ask, :choose - HighLine.use_color = !CLI.windows? + HighLine.use_color = !CLI.windows? && $stdin.tty? HighLine.color_scheme = HighLine::ColorScheme.new do |cs| cs[:command] = [ :bold ] cs[:error] = [ :red ] cs[:important] = [ :bold, :underline ] cs[:success] = [ :green ] cs[:info] = [ :yellow ] + cs[:debug] = [ :magenta ] end on('-h', '--help', 'Display help') do |c, _| c.say c.help exit end on('-i', '--[no-]interactive', "be interactive and colorful") do |c, v| + HighLine.use_color = v unless CLI.windows? c.force_interactive = v end on('-E', '--[no-]explode', "don't rescue exceptions") @@ -46,16 +50,17 @@ def self.skip(name) define_method(name) {} end - attr_accessor :arguments, :config, :terminal, :force_interactive + attr_accessor :arguments, :config, :terminal, :force_interactive, :formatter def initialize(options = {}) - @output = SimpleDelegator.new($stdout) - @input = SimpleDelegator.new($stdin) - @terminal = HighLine.new(@input, @output) + @formatter = Travis::Tools::Formatter.new + @output = SimpleDelegator.new($stdout) + @input = SimpleDelegator.new($stdin) + @terminal = HighLine.new(@input, @output) options.each do |key, value| public_send("#{key}=", value) if respond_to? "#{key}=" end @arguments ||= [] end @@ -107,43 +112,55 @@ def command_name self.class.command_name end def usage - usage = "#$0 #{command_name} [options]" + usage = "#$0 #{command_name}" method = method(:run) if method.respond_to? :parameters method.parameters.each do |type, name| name = "[#{name}]" if type == :opt name = "[#{name}..]" if type == :rest usage << " #{name}" end - else + elsif method.arity != 0 usage << " ..." end + usage << " [options]" "Usage: " << color(usage, :command) end def help parser.banner = usage parser.to_s end - def say(data, format = nil) - data = format % color(data, :important) if format and interactive? - terminal.say data.gsub(/<\[\[/, '<%=').gsub(/\]\]>/, '%>') + def say(data, format = nil, style = nil) + terminal.say format(data, format, style) end + def debug(line) + write_to($stderr) do + say color("** #{line}", :debug) + end + end + private + def format(data, format = nil, style = nil) + style ||= :important + data = format % color(data, style) if format and interactive? + data = data.gsub(/<\[\[/, '<%=').gsub(/\]\]>/, '%>') + end + def template(file) File.read(file).split('__END__', 2)[1].strip end - def color(line, *args) + def color(line, style) return line unless interactive? - terminal.color(line, *args) + terminal.color(line, Array(style).map(&:to_sym)) end def interactive?(io = output) return io.tty? if force_interactive.nil? force_interactive @@ -151,18 +168,22 @@ def empty_line say "\n" end - def error(message) + def warn(message) write_to($stderr) do say color(message, :error) yield if block_given? - exit 1 end end + def error(message, &block) + warn(message, &block) + exit 1 + end + def command(name) color("#$0 #{name}", :command) end def success(line) @@ -195,10 +216,10 @@ def check_arity(method, *args) return unless method.respond_to? :parameters method.parameters.each do |type, name| return if type == :rest - wrong_args("few") unless args.shift or type == :opt + wrong_args("few") unless args.shift or type == :opt or type == :block end wrong_args("many") if args.any? end def wrong_args(quantity)