Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit e1ce047

Browse files
committed
Merge pull request #2914 from rspec/bold-colours
Add support for bold colours
1 parent 60659b7 commit e1ce047

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

features/formatters/configurable_colors.feature

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ Feature: Configurable colors
1010
* `detail_color`: Color used for miscellaneous test details (default: `:cyan`)
1111

1212
Colors are specified as symbols. Options are `:black`, `:red`, `:green`,
13-
`:yellow`, `:blue`, `:magenta`, `:cyan`, and `:white`.
13+
`:yellow`, `:blue`, `:magenta`, `:cyan`, `:white`, `:bold_black`, `:bold_red`,
14+
`:bold_green`, `:bold_yellow`, `:bold_blue`, `:bold_magenta`, `:bold_cyan`,
15+
and `:bold_white`,
1416

1517
@keep-ansi-escape-sequences
1618
Scenario: Customizing the failure color
@@ -29,3 +31,21 @@ Feature: Configurable colors
2931
"""
3032
When I run `rspec custom_failure_color_spec.rb --format progress`
3133
Then the failing example is printed in magenta
34+
35+
@keep-ansi-escape-sequences
36+
Scenario: Customizing the failure color with a custom console code
37+
Given a file named "custom_failure_color_spec.rb" with:
38+
"""ruby
39+
RSpec.configure do |config|
40+
config.failure_color = "1;32"
41+
config.color_mode = :on
42+
end
43+
44+
RSpec.describe "failure" do
45+
it "fails and uses the custom color" do
46+
expect(2).to eq(4)
47+
end
48+
end
49+
"""
50+
When I run `rspec custom_failure_color_spec.rb --format progress`
51+
Then the failing example is printed wrapped in "1;32"

features/step_definitions/additional_cli_steps.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,16 @@
7676
end
7777

7878
# This step can be generalized if it's ever used to test other colors
79-
Then /^the failing example is printed in magenta$/ do
79+
Then /^the failing example is printed (?:wrapped )?in (.*)$/ do |color|
80+
code =
81+
case color
82+
when "magenta" then "\e[35m"
83+
when /"(.*)"/ then "\e[#{$1}m"
84+
end
85+
8086
# \e[35m = enable magenta
8187
# \e[0m = reset colors
82-
expect(all_output).to include("\e[35m" + "F" + "\e[0m")
88+
expect(all_output).to include(code + "F" + "\e[0m")
8389
end
8490

8591
Then /^the output from `([^`]+)` should contain "(.*?)"$/ do |cmd, expected_output|

lib/rspec/core/formatters/console_codes.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ module ConsoleCodes
77
# @private
88
VT100_CODES =
99
{
10-
:black => 30,
11-
:red => 31,
12-
:green => 32,
13-
:yellow => 33,
14-
:blue => 34,
15-
:magenta => 35,
16-
:cyan => 36,
17-
:white => 37,
18-
:bold => 1,
10+
:black => 30,
11+
:red => 31,
12+
:green => 32,
13+
:yellow => 33,
14+
:blue => 34,
15+
:magenta => 35,
16+
:cyan => 36,
17+
:white => 37,
18+
:bold_black => '1;30',
19+
:bold_red => '1;31',
20+
:bold_green => '1;32',
21+
:bold_yellow => '1;33',
22+
:bold_blue => '1;34',
23+
:bold_magenta => '1;35',
24+
:bold_cyan => '1;36',
25+
:bold_white => '1;37',
26+
:bold => 1,
1927
}
2028
# @private
2129
VT100_CODE_VALUES = VT100_CODES.invert

spec/rspec/core/formatters/console_codes_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
end
1111
end
1212

13+
context "when given a VT100 compound code" do
14+
it "returns the code" do
15+
expect(console_codes.console_code_for('1;32')).to eq '1;32'
16+
end
17+
end
18+
1319
context "when given a symbolic name" do
1420
it "returns the code" do
1521
expect(console_codes.console_code_for(:green)).to eq 32
@@ -41,19 +47,37 @@
4147
end
4248
end
4349

50+
context "when given a VT100 compound code" do
51+
it "formats the text with it" do
52+
expect(console_codes.wrap('abc', '1;32')).to eq "\e[1;32mabc\e[0m"
53+
end
54+
end
55+
4456
context "when given a symbolic color name" do
4557
it "translates it to the correct integer code and formats the text with it" do
4658
expect(console_codes.wrap('abc', :green)).to eq "\e[32mabc\e[0m"
4759
end
4860
end
4961

62+
context "when given a symbolic bold color name" do
63+
it "translates it to the correct integer code and formats the text with it" do
64+
expect(console_codes.wrap('abc', :bold_green)).to eq "\e[1;32mabc\e[0m"
65+
end
66+
end
67+
5068
context "when given an rspec code" do
5169
it "returns the console code" do
5270
RSpec.configuration.success_color = :blue # blue is 34
5371
expect(console_codes.wrap('abc', :success)).to eq "\e[34mabc\e[0m"
5472
end
5573
end
5674

75+
context "when given a compound rspec code" do
76+
it "returns the console code" do
77+
RSpec.configuration.success_color = :bold_blue # blue is 34
78+
expect(console_codes.wrap('abc', :success)).to eq "\e[1;34mabc\e[0m"
79+
end
80+
end
5781

5882
context "when given :bold" do
5983
it "formats the text as bold" do

0 commit comments

Comments
 (0)