summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-03-02 17:55:23 -0800
committerGitHub <noreply@github.com>2020-03-02 17:55:23 -0800
commit173b413690d93e7bfb439b0a586b9563384e3072 (patch)
tree660e3dcb2fa2c0a115dc386f53549be848cd5ac4
parent5139c1a3fbf3535d763c31b3071022dc3a4ab3d5 (diff)
parentc0368ea911daff28e0759c91e08c53a2e986fa45 (diff)
downloadchef-173b413690d93e7bfb439b0a586b9563384e3072.tar.gz
Merge pull request #9434 from chef/btm/colors
Replace highline.color with pastel.decorate
-rw-r--r--Gemfile.lock2
-rw-r--r--chef.gemspec1
-rw-r--r--lib/chef/formatters/indentable_output_stream.rb23
-rw-r--r--lib/chef/knife/core/ui.rb10
-rw-r--r--spec/unit/knife/core/ui_spec.rb16
5 files changed, 35 insertions, 17 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index d988519022..89544debd6 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -53,6 +53,7 @@ PATH
net-ssh (>= 4.2, < 6)
net-ssh-multi (~> 1.2, >= 1.2.1)
ohai (~> 16.0)
+ pastel
plist (~> 3.2)
proxifier (~> 1.0)
syslog-logger (~> 1.6)
@@ -87,6 +88,7 @@ PATH
net-ssh (>= 4.2, < 6)
net-ssh-multi (~> 1.2, >= 1.2.1)
ohai (~> 16.0)
+ pastel
plist (~> 3.2)
proxifier (~> 1.0)
syslog-logger (~> 1.6)
diff --git a/chef.gemspec b/chef.gemspec
index 9046365b9b..427a2cfece 100644
--- a/chef.gemspec
+++ b/chef.gemspec
@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
s.add_dependency "bcrypt_pbkdf", "~> 1.0" # ed25519 ssh key support
s.add_dependency "highline", ">= 1.6.9", "< 2"
s.add_dependency "tty-screen", "~> 0.6" # knife list
+ s.add_dependency "pastel" # knife ui.color
s.add_dependency "erubis", "~> 2.7"
s.add_dependency "diff-lcs", "~> 1.2", ">= 1.2.4"
s.add_dependency "ffi-libarchive"
diff --git a/lib/chef/formatters/indentable_output_stream.rb b/lib/chef/formatters/indentable_output_stream.rb
index 5d58df6f11..d508a32eb0 100644
--- a/lib/chef/formatters/indentable_output_stream.rb
+++ b/lib/chef/formatters/indentable_output_stream.rb
@@ -17,23 +17,14 @@ class Chef
@semaphore = Mutex.new
end
- def highline
- @highline ||= begin
- require "highline"
- HighLine.new
+ # pastel.decorate is a lightweight replacement for highline.color
+ def pastel
+ @pastel ||= begin
+ require "pastel"
+ Pastel.new
end
end
- # Print text. This will start a new line and indent if necessary
- # but will not terminate the line (future print and puts statements
- # will start off where this print left off).
- #
- # @param string [String]
- # @param args [Array<Hash,Symbol>]
- def color(string, *args)
- print(string, from_args(args))
- end
-
# Print the start of a new line. This will terminate any existing lines and
# cause indentation but will not move to the next line yet (future 'print'
# and 'puts' statements will stay on this line).
@@ -83,7 +74,7 @@ class Chef
#
# == Alternative
#
- # You may also call print('string', :red) (a list of colors a la Highline.color)
+ # You may also call print('string', :red) (https://github.com/piotrmurach/pastel#3-supported-colors)
def print(string, *args)
options = from_args(args)
@@ -140,7 +131,7 @@ class Chef
end
if Chef::Config[:color] && options[:colors]
- @out.print highline.color(line, *options[:colors])
+ @out.print pastel.decorate(line, *options[:colors])
else
@out.print line
end
diff --git a/lib/chef/knife/core/ui.rb b/lib/chef/knife/core/ui.rb
index 41fb37c220..0dfa1db79c 100644
--- a/lib/chef/knife/core/ui.rb
+++ b/lib/chef/knife/core/ui.rb
@@ -61,6 +61,14 @@ class Chef
end
end
+ # pastel.decorate is a lightweight replacement for highline.color
+ def pastel
+ @pastel ||= begin
+ require "pastel"
+ Pastel.new
+ end
+ end
+
# Prints a message to stdout. Aliased as +info+ for compatibility with
# the logger API.
#
@@ -134,7 +142,7 @@ class Chef
def color(string, *colors)
if color?
- highline.color(string, *colors)
+ pastel.decorate(string, *colors)
else
string
end
diff --git a/spec/unit/knife/core/ui_spec.rb b/spec/unit/knife/core/ui_spec.rb
index dfe906fdc1..e870926b3f 100644
--- a/spec/unit/knife/core/ui_spec.rb
+++ b/spec/unit/knife/core/ui_spec.rb
@@ -507,6 +507,22 @@ describe Chef::Knife::UI do
end
end
+ describe "color" do
+ context "when ui.color? => true" do
+ it "returns colored output" do
+ expect(@ui).to receive(:color?).and_return(true)
+ expect(@ui.color("a_bus_is", :yellow)).to eql("\e[33ma_bus_is\e[0m")
+ end
+ end
+
+ context "when ui.color? => false" do
+ it "returns plain output" do
+ expect(@ui).to receive(:color?).and_return(false)
+ expect(@ui.color("a_bus_is", :yellow)).to eql("a_bus_is")
+ end
+ end
+ end
+
describe "confirm" do
let(:stdout) { StringIO.new }
let(:output) { stdout.string }