summaryrefslogtreecommitdiff
path: root/lib/bundler/ui
diff options
context:
space:
mode:
Diffstat (limited to 'lib/bundler/ui')
-rw-r--r--lib/bundler/ui/rg_proxy.rb2
-rw-r--r--lib/bundler/ui/shell.rb77
-rw-r--r--lib/bundler/ui/silent.rb39
3 files changed, 95 insertions, 23 deletions
diff --git a/lib/bundler/ui/rg_proxy.rb b/lib/bundler/ui/rg_proxy.rb
index ef6def225b..b17ca65f53 100644
--- a/lib/bundler/ui/rg_proxy.rb
+++ b/lib/bundler/ui/rg_proxy.rb
@@ -12,7 +12,7 @@ module Bundler
end
def say(message)
- @ui && @ui.debug(message)
+ @ui&.debug(message)
end
end
end
diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb
index 17777af4ac..6df1512a5b 100644
--- a/lib/bundler/ui/shell.rb
+++ b/lib/bundler/ui/shell.rb
@@ -6,43 +6,69 @@ 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
- def info(msg, newline = nil)
- tell_me(msg, nil, newline) if level("info")
+ def info(msg = nil, newline = nil)
+ return unless info?
+
+ tell_me(msg || yield, nil, newline)
end
- def confirm(msg, newline = nil)
- tell_me(msg, :green, newline) if level("confirm")
+ def confirm(msg = nil, newline = nil)
+ return unless confirm?
+
+ tell_me(msg || yield, :green, newline)
end
- def warn(msg, newline = nil, color = :yellow)
- return unless level("warn")
+ def warn(msg = nil, newline = nil, color = :yellow)
+ return unless warn?
return if @warning_history.include? msg
@warning_history << msg
- tell_err(msg, color, newline)
+ tell_err(msg || yield, color, newline)
+ end
+
+ def error(msg = nil, newline = nil, color = :red)
+ return unless error?
+
+ tell_err(msg || yield, color, newline)
end
- def error(msg, newline = nil, color = :red)
- return unless level("error")
- tell_err(msg, color, newline)
+ def debug(msg = nil, newline = nil)
+ return unless debug?
+
+ tell_me(msg || yield, nil, newline)
end
- def debug(msg, newline = nil)
- tell_me(msg, nil, newline) if debug?
+ def info?
+ level("info")
+ end
+
+ def confirm?
+ level("confirm")
+ end
+
+ def warn?
+ level("warn")
+ end
+
+ def error?
+ level("error")
end
def debug?
@@ -61,7 +87,7 @@ module Bundler
@shell.yes?(msg)
end
- def no?
+ def no?(msg)
@shell.no?(msg)
end
@@ -78,16 +104,25 @@ module Bundler
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_me(msg, nil, newline)
+ 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
@@ -96,6 +131,8 @@ module Bundler
# 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)
@@ -107,7 +144,7 @@ module Bundler
def tell_err(message, color = nil, newline = nil)
return if @shell.send(:stderr).closed?
- newline ||= message.to_s !~ /( |\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?
@@ -124,7 +161,7 @@ module Bundler
spaces ? text.gsub(/#{spaces}/, "") : text
end
- def word_wrap(text, line_width = @shell.terminal_width)
+ def word_wrap(text, line_width = Thor::Terminal.terminal_width)
strip_leading_spaces(text).split("\n").collect do |line|
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
@@ -137,6 +174,14 @@ module Bundler
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
diff --git a/lib/bundler/ui/silent.rb b/lib/bundler/ui/silent.rb
index dca1b2ac86..83d31d4b55 100644
--- a/lib/bundler/ui/silent.rb
+++ b/lib/bundler/ui/silent.rb
@@ -13,30 +13,53 @@ module Bundler
string
end
- def info(message, newline = nil)
+ def info(message = nil, newline = nil)
end
- def confirm(message, newline = nil)
+ def confirm(message = nil, newline = nil)
end
- def warn(message, newline = nil)
+ def warn(message = nil, newline = nil)
@warnings |= [message]
end
- def error(message, newline = nil)
+ def error(message = nil, newline = nil)
end
- def debug(message, newline = nil)
+ def debug(message = nil, newline = nil)
+ end
+
+ def confirm?
+ false
+ end
+
+ def error?
+ false
end
def debug?
false
end
+ def info?
+ false
+ end
+
def quiet?
false
end
+ def warn?
+ false
+ end
+
+ def output_stream=(_symbol)
+ end
+
+ def output_stream
+ nil
+ end
+
def ask(message)
end
@@ -44,7 +67,7 @@ module Bundler
raise "Cannot ask yes? with a silent shell"
end
- def no?
+ def no?(msg)
raise "Cannot ask no? with a silent shell"
end
@@ -61,6 +84,10 @@ module Bundler
yield
end
+ def progress
+ yield
+ end
+
def unprinted_warnings
@warnings
end