lib/bundler/ui/shell.rb in bundler-2.5.18 vs lib/bundler/ui/shell.rb in bundler-2.5.19

- old
+ new

@@ -4,18 +4,21 @@ module Bundler module UI class Shell LEVELS = %w[silent error warn confirm info debug].freeze + OUTPUT_STREAMS = [:stdout, :stderr].freeze attr_writer :shell + attr_reader :output_stream def initialize(options = {}) Thor::Base.shell = options["no-color"] ? Thor::Shell::Basic : nil @shell = Thor::Base.shell.new @level = ENV["DEBUG"] ? "debug" : "info" @warning_history = [] + @output_stream = :stdout end def add_color(string, *color) @shell.set_color(string, *color) end @@ -82,11 +85,11 @@ def yes?(msg) @shell.yes?(msg) end - def no? + def no?(msg) @shell.no?(msg) end def level=(level) raise ArgumentError unless LEVELS.include?(level.to_s) @@ -99,28 +102,39 @@ raise "#{name.inspect} is not a valid level" end index <= LEVELS.index(@level) end + def output_stream=(symbol) + raise ArgumentError unless OUTPUT_STREAMS.include?(symbol) + @output_stream = symbol + end + def trace(e, newline = nil, force = false) return unless debug? || force msg = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n ")}" tell_err(msg, nil, newline) end def silence(&blk) with_level("silent", &blk) end + def progress(&blk) + with_output_stream(:stderr, &blk) + end + def unprinted_warnings [] end private # valimism def tell_me(msg, color = nil, newline = nil) + return tell_err(msg, color, newline) if output_stream == :stderr + msg = word_wrap(msg) if newline.is_a?(Hash) && newline[:wrap] if newline.nil? @shell.say(msg, color) else @shell.say(msg, color, newline) @@ -128,11 +142,11 @@ end def tell_err(message, color = nil, newline = nil) return if @shell.send(:stderr).closed? - newline ||= !message.to_s.match?(/( |\t)\Z/) + newline = !message.to_s.match?(/( |\t)\Z/) if newline.nil? message = word_wrap(message) if newline.is_a?(Hash) && newline[:wrap] color = nil if color && !$stderr.tty? buffer = @shell.send(:prepare_message, message, *color) @@ -157,9 +171,17 @@ original = @level @level = level yield ensure @level = original + end + + def with_output_stream(symbol) + original = output_stream + self.output_stream = symbol + yield + ensure + @output_stream = original end end end end