Merge pull request #8624 from nandahkrishna/tty-fix

utils/tty: handling a TTY stderr
This commit is contained in:
Nanda H Krishna 2020-09-17 19:48:47 +05:30 committed by GitHub
commit 13d14e64cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 14 deletions

View File

@ -256,7 +256,7 @@ class Tap
return if !full_clone || !shallow?
end
ohai "Unshallowing #{name}" unless quiet
$stderr.ohai "Unshallowing #{name}" unless quiet
args = %w[fetch --unshallow]
args << "-q" if quiet
path.cd { safe_system "git", *args }
@ -265,7 +265,7 @@ class Tap
clear_cache
ohai "Tapping #{name}" unless quiet
$stderr.ohai "Tapping #{name}" unless quiet
args = %W[clone #{requested_remote} #{path}]
args << "--depth=1" unless full_clone
args << "-q" if quiet
@ -291,7 +291,7 @@ class Tap
link_completions_and_manpages
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet
$stderr.puts "Tapped#{formatted_contents} (#{path.abv})." unless quiet
CacheStoreDatabase.use(:descriptions) do |db|
DescriptionCacheStore.new(db)
.update_from_formula_names!(formula_names)
@ -301,7 +301,7 @@ class Tap
return unless private?
return if quiet
puts <<~EOS
$stderr.puts <<~EOS
It looks like you tapped a private repository. To avoid entering your
credentials each time you update, you can use git HTTP credential
caching or issue the following command:
@ -321,7 +321,7 @@ class Tap
require "descriptions"
raise TapUnavailableError, name unless installed?
puts "Untapping #{name}..."
$stderr.puts "Untapping #{name}..."
abv = path.abv
formatted_contents = contents.presence&.to_sentence&.dup&.prepend(" ")
@ -335,7 +335,7 @@ class Tap
Utils::Link.unlink_completions(path)
path.rmtree
path.parent.rmdir_if_possible
puts "Untapped#{formatted_contents} (#{abv})."
$stderr.puts "Untapped#{formatted_contents} (#{abv})."
Commands.rebuild_commands_completion_list
clear_cache
@ -625,7 +625,9 @@ class CoreTap < Tap
def install(full_clone: true, quiet: false, clone_target: nil, force_auto_update: nil)
remote = Homebrew::EnvConfig.core_git_remote
puts "HOMEBREW_CORE_GIT_REMOTE set: using #{remote} for Homebrew/core Git remote URL." if remote != default_remote
if remote != default_remote
$stderr.puts "HOMEBREW_CORE_GIT_REMOTE set: using #{remote} for Homebrew/core Git remote URL."
end
super(full_clone: full_clone, quiet: quiet, clone_target: remote, force_auto_update: force_auto_update)
end

View File

@ -11,8 +11,7 @@ describe "brew tap", :integration_test do
path = setup_test_tap
expect { brew "tap", "--force-auto-update", "--full", "homebrew/bar", path/".git" }
.to output(/Tapped/).to_stdout
.and output(/Cloning/).to_stderr
.to output(/Tapped/).to_stderr
.and be_a_success
end
end

View File

@ -11,8 +11,8 @@ describe "brew untap", :integration_test do
setup_test_tap
expect { brew "untap", "homebrew/foo" }
.to output(/Untapped/).to_stdout
.and not_to_output.to_stderr
.to output(/Untapped/).to_stderr
.and not_to_output.to_stdout
.and be_a_success
end
end

View File

@ -131,11 +131,15 @@ module Kernel
# Print a warning (do this rarely)
def opoo(message)
$stderr.puts Formatter.warning(message, label: "Warning")
Tty.with($stderr) do |stderr|
stderr.puts Formatter.warning(message, label: "Warning")
end
end
def onoe(message)
$stderr.puts Formatter.error(message, label: "Error")
Tty.with($stderr) do |stderr|
stderr.puts Formatter.error(message, label: "Error")
end
end
def ofail(error)

View File

@ -4,8 +4,19 @@
#
# @api private
module Tty
@stream = $stdout
module_function
def with(stream)
previous_stream = @stream
@stream = stream
yield stream
ensure
@stream = previous_stream
end
def strip_ansi(string)
string.gsub(/\033\[\d+(;\d+)*m/, "")
end
@ -78,6 +89,6 @@ module Tty
return false if Homebrew::EnvConfig.no_color?
return true if Homebrew::EnvConfig.color?
$stdout.tty?
@stream.tty?
end
end