-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ansicolor.rb
139 lines (124 loc) · 4.43 KB
/
ansicolor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
require 'cucumber/platform'
require 'cucumber/term/ansicolor'
Cucumber::Term::ANSIColor.coloring = false unless $stdout.tty?
module Cucumber
module Formatter
# This module allows to format cucumber related outputs using ANSI escape sequences.
#
# For example, it provides a `passed` method which returns the string with
# the ANSI escape sequence to format it green per default.
#
# To use this, include or extend it in your class.
#
# Example:
#
# require 'cucumber/formatter/ansicolor'
#
# class MyFormatter
# extend Cucumber::Term::ANSIColor
#
# def on_test_step_finished(event)
# $stdout.puts undefined(event.test_step) if event.result.undefined?
# $stdout.puts passed(event.test_step) if event.result.passed?
# end
# end
#
# This module also allows the user to customize the format of cucumber outputs
# using environment variables.
#
# For instance, if your shell has a black background and a green font (like the
# "Homebrew" settings for OS X' Terminal.app), you may want to override passed
# steps to be white instead of green.
#
# Example:
#
# export CUCUMBER_COLORS="passed=white,bold:passed_param=white,bold,underline"
#
# The colours that you can change are:
#
# * <tt>undefined</tt> - defaults to <tt>yellow</tt>
# * <tt>pending</tt> - defaults to <tt>yellow</tt>
# * <tt>pending_param</tt> - defaults to <tt>yellow,bold</tt>
# * <tt>flaky</tt> - defaults to <tt>yellow</tt>
# * <tt>flaky_param</tt> - defaults to <tt>yellow,bold</tt>
# * <tt>failed</tt> - defaults to <tt>red</tt>
# * <tt>failed_param</tt> - defaults to <tt>red,bold</tt>
# * <tt>passed</tt> - defaults to <tt>green</tt>
# * <tt>passed_param</tt> - defaults to <tt>green,bold</tt>
# * <tt>outline</tt> - defaults to <tt>cyan</tt>
# * <tt>outline_param</tt> - defaults to <tt>cyan,bold</tt>
# * <tt>skipped</tt> - defaults to <tt>cyan</tt>
# * <tt>skipped_param</tt> - defaults to <tt>cyan,bold</tt>
# * <tt>comment</tt> - defaults to <tt>grey</tt>
# * <tt>tag</tt> - defaults to <tt>cyan</tt>
#
# To see what colours and effects are available, just run this in your shell:
#
# ruby -e "require 'rubygems'; require 'cucumber/term/ansicolor'; puts Cucumber::Term::ANSIColor.attributes"
#
module ANSIColor
include Cucumber::Term::ANSIColor
ALIASES = Hash.new do |h, k|
next unless k.to_s =~ /(.*)_param/
"#{h[Regexp.last_match(1)]},bold"
end.merge(
'undefined' => 'yellow',
'pending' => 'yellow',
'flaky' => 'yellow',
'failed' => 'red',
'passed' => 'green',
'outline' => 'cyan',
'skipped' => 'cyan',
'comment' => 'grey',
'tag' => 'cyan'
)
# Apply the custom color scheme -> i.e. apply_custom_colors('passed=white')
def self.apply_custom_colors(colors)
colors.split(':').each do |pair|
a = pair.split('=')
ALIASES[a[0]] = a[1]
end
end
apply_custom_colors(ENV['CUCUMBER_COLORS']) if ENV['CUCUMBER_COLORS']
# Define the color-named methods required by Term::ANSIColor.
#
# Examples:
#
# def failed(string=nil, &proc)
# red(string, &proc)
# end
#
# def failed_param(string=nil, &proc)
# red(bold(string, &proc)) + red
# end
ALIASES.each_key do |method_name|
next if method_name.end_with?('_param')
define_method(method_name) do |text = nil, &proc|
apply_styles(ALIASES[method_name], text, &proc)
end
define_method("#{method_name}_param") do |text = nil, &proc|
apply_styles(ALIASES["#{method_name}_param"], text, &proc) + apply_styles(ALIASES[method_name])
end
end
def cukes(amount)
('(::) ' * amount).strip
end
def green_cukes(amount)
blink(green(cukes(amount)))
end
def red_cukes(amount)
blink(red(cukes(amount)))
end
def yellow_cukes(amount)
blink(yellow(cukes(amount)))
end
private
def apply_styles(styles, text = nil, &proc)
styles.split(',').reverse.reduce(text) do |result, method_name|
send(method_name, result, &proc)
end
end
end
end
end