From 7388acb86e0b9811e6badd610255954b3b3a6453 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Mon, 12 Feb 2018 11:06:55 -0600 Subject: Tty: if HOMEBREW_NO_COLOR env var is present, disable color bin/brew will recognize NO_COLOR variable and copy it to HOMEBREW_NO_COLOR --- Library/Homebrew/manpages/brew.1.md.erb | 3 +++ Library/Homebrew/test/cask/dsl_spec.rb | 1 + Library/Homebrew/test/utils/tty_spec.rb | 14 +++++++++++++- Library/Homebrew/utils/tty.rb | 4 +++- 4 files changed, 20 insertions(+), 2 deletions(-) (limited to 'Library/Homebrew') diff --git a/Library/Homebrew/manpages/brew.1.md.erb b/Library/Homebrew/manpages/brew.1.md.erb index 508c90f2d..fbe6fca94 100644 --- a/Library/Homebrew/manpages/brew.1.md.erb +++ b/Library/Homebrew/manpages/brew.1.md.erb @@ -199,6 +199,9 @@ can take several different forms: If set, Homebrew will not auto-update before running `brew install`, `brew upgrade` or `brew tap`. + * `HOMEBREW_NO_COLOR`: + If set, Homebrew will not print text with color added. + * `HOMEBREW_NO_EMOJI`: If set, Homebrew will not print the `HOMEBREW_INSTALL_BADGE` on a successful build. diff --git a/Library/Homebrew/test/cask/dsl_spec.rb b/Library/Homebrew/test/cask/dsl_spec.rb index d3bb46264..993c03bac 100644 --- a/Library/Homebrew/test/cask/dsl_spec.rb +++ b/Library/Homebrew/test/cask/dsl_spec.rb @@ -75,6 +75,7 @@ describe Hbc::DSL, :cask do it "may use deprecated DSL version hash syntax" do allow(ENV).to receive(:[]).with("HOMEBREW_DEVELOPER").and_return(nil) + allow(ENV).to receive(:[]).with("HOMEBREW_NO_COLOR").and_return(nil) expect(cask.token).to eq("with-dsl-version") expect(cask.url.to_s).to eq("http://example.com/TestCask.dmg") diff --git a/Library/Homebrew/test/utils/tty_spec.rb b/Library/Homebrew/test/utils/tty_spec.rb index 3ba89b6fd..e6c9168f3 100644 --- a/Library/Homebrew/test/utils/tty_spec.rb +++ b/Library/Homebrew/test/utils/tty_spec.rb @@ -53,7 +53,7 @@ describe Tty do allow($stdout).to receive(:tty?).and_return(true) end - it "returns an empty string for all colors" do + it "returns ANSI escape codes for colors" do expect(subject.to_s).to eq("") expect(subject.red.to_s).to eq("\033[31m") expect(subject.green.to_s).to eq("\033[32m") @@ -63,5 +63,17 @@ describe Tty do expect(subject.cyan.to_s).to eq("\033[36m") expect(subject.default.to_s).to eq("\033[39m") end + + it "returns an empty string for all colors when HOMEBREW_NO_COLOR is set" do + ENV["HOMEBREW_NO_COLOR"] = "1" + expect(subject.to_s).to eq("") + expect(subject.red.to_s).to eq("") + expect(subject.green.to_s).to eq("") + expect(subject.yellow.to_s).to eq("") + expect(subject.blue.to_s).to eq("") + expect(subject.magenta.to_s).to eq("") + expect(subject.cyan.to_s).to eq("") + expect(subject.default.to_s).to eq("") + end end end diff --git a/Library/Homebrew/utils/tty.rb b/Library/Homebrew/utils/tty.rb index e872e6460..81d5f00d7 100644 --- a/Library/Homebrew/utils/tty.rb +++ b/Library/Homebrew/utils/tty.rb @@ -59,7 +59,9 @@ module Tty end def to_s - return "" unless $stdout.tty? + if ENV["HOMEBREW_NO_COLOR"] || !$stdout.tty? + return "" + end current_escape_sequence ensure reset_escape_sequence! -- cgit v1.2.3