crayon 1.0.0 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/README.md +22 -13
- data/VERSION +1 -1
- data/lib/crayon.rb +20 -83
- data/lib/crayon/crayon_string.rb +10 -0
- data/lib/crayon/method_parser.rb +32 -0
- data/lib/crayon/string_builder.rb +39 -0
- data/spec/crayon_spec.rb +6 -47
- metadata +6 -3
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
# crayon
|
2
2
|
|
3
|
-
### current version : 1.0.
|
3
|
+
### current version : 1.0.3
|
4
4
|
|
5
5
|
[http://github.com/mikowitz/crayon][github]
|
6
6
|
|
7
|
+
### version warnings
|
8
|
+
|
9
|
+
* `puts` and `print` will be deprecated. `Crayon` will no longer contain an IO object, but will only return strings
|
10
|
+
|
7
11
|
## Description
|
8
12
|
|
9
13
|
A simple, flexible gem that provides an open-ended API to print colored and styled output to the terminal.
|
@@ -35,29 +39,34 @@ The following are all methods that `Crayon` understands, and should give you an
|
|
35
39
|
Crayon.bold("this will be bold")
|
36
40
|
Crayon.underline_blue_on_yellow("this will be underlined blue text on a yellow background")
|
37
41
|
|
38
|
-
|
39
|
-
|
42
|
+
### `puts` and `print` deprecation warning
|
43
|
+
|
44
|
+
As of version 1.0.2, `puts` and `print` no longer control adding a newline to the end of Crayon-ed text. The methods themselves will remain in the code base until version 1.1.0, but will issue deprecation warnings, and have no real effect on the code. Instead of a line of code looking like this:
|
45
|
+
|
46
|
+
Crayon.print.red("red, ").blue("blue, ").puts.green("and green")
|
40
47
|
|
41
|
-
|
42
|
-
Crayon.print.blue("the next line will be printed right next to this")
|
43
|
-
Crayon.puts.green("this will on the second line of output, but will create a newline.")
|
48
|
+
you have a line of code like this:
|
44
49
|
|
45
|
-
|
50
|
+
puts Crayon.red("red, ").blue("blue, ").green("and green")
|
46
51
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
Crayon.puts.cyan("Also on the first line, but with a trailing newline")
|
51
|
-
Crayon.blue("On the second line by itself.")
|
52
|
+
This change is mostly due to the fact that defining Crayon's IO object would make using Crayon inside of another project more complicated than it would need to be. Crayon now simply returns a formatted string.
|
53
|
+
|
54
|
+
### chaining
|
52
55
|
|
53
56
|
You can also chain color calls without having to call `Crayon` multiple times
|
54
57
|
|
55
|
-
Crayon.
|
58
|
+
Crayon.red("red").blue("blue").green("green")
|
59
|
+
|
60
|
+
### case flexibility
|
56
61
|
|
57
62
|
`Crayon` will also handle mixed-case method calls
|
58
63
|
|
59
64
|
Crayon.ReD_ON_gREEN("It's Christmas!")
|
60
65
|
|
66
|
+
Of course, this is no different from calling
|
67
|
+
|
68
|
+
Crayon.red_on_green("It's Christmas!")
|
69
|
+
|
61
70
|
## Flexibility
|
62
71
|
|
63
72
|
The order of elements in the method name is unimportant. For example
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.3
|
data/lib/crayon.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + "/crayon"
|
2
|
+
|
3
|
+
%w{ method_parser string_builder}.each {|file| require file }
|
4
|
+
|
1
5
|
module Crayon
|
6
|
+
include Crayon::MethodParser
|
7
|
+
include Crayon::StringBuilder
|
2
8
|
extend self
|
3
9
|
|
4
10
|
class << self
|
5
11
|
# @private
|
6
|
-
attr_accessor :foreground, :background, :formatting, :method_name, :color
|
12
|
+
attr_accessor :foreground, :background, :formatting, :method_name, :color
|
7
13
|
end
|
8
14
|
|
9
15
|
# @private
|
@@ -13,98 +19,29 @@ module Crayon
|
|
13
19
|
# @private
|
14
20
|
TERMINATION_STRING = "\e[0m"
|
15
21
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def self.print; @newline = false; return Crayon; end
|
22
|
+
def self.print
|
23
|
+
Kernel::puts "Color.print is deprecated and will be removed in version 1.1.0."
|
24
|
+
return Crayon
|
25
|
+
end
|
22
26
|
|
23
|
-
def self.puts
|
27
|
+
def self.puts
|
28
|
+
Kernel::puts "Color.puts is deprecated and will be removed in version 1.1.0."
|
29
|
+
return Crayon
|
30
|
+
end
|
24
31
|
|
25
32
|
def method_missing(method_name, string)
|
33
|
+
nullify_variables
|
26
34
|
@method_name = method_name
|
27
35
|
parse_method_name
|
28
|
-
|
29
|
-
nullify_variables
|
30
|
-
Crayon
|
31
|
-
end
|
32
|
-
|
33
|
-
##
|
34
|
-
# Converts a method name into color and formatting parameters
|
35
|
-
# @example
|
36
|
-
# Crayon.parse_method_name(:bold_red_on_green) #=> "['red', 'green', ['bold']]"
|
37
|
-
# @private
|
38
|
-
def parse_method_name
|
39
|
-
@method_name = @method_name.to_s.downcase.split("_")
|
40
|
-
@background = parse_background
|
41
|
-
@foreground = parse_foreground
|
42
|
-
@formatting = parse_formatting
|
43
|
-
end
|
44
|
-
|
45
|
-
# @private
|
46
|
-
def parse_background
|
47
|
-
_idx = @method_name.index("on")
|
48
|
-
if _idx
|
49
|
-
@method_name.delete("on")
|
50
|
-
_background = @method_name.delete_at(_idx)
|
51
|
-
_background if COLORS.keys.include?(_background)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# @private
|
56
|
-
def parse_foreground
|
57
|
-
@method_name.find {|color| COLORS.keys.include?(color) }
|
58
|
-
end
|
59
|
-
|
60
|
-
# @private
|
61
|
-
def parse_formatting
|
62
|
-
@method_name.select {|format| FORMATS.keys.include?(format) }
|
63
|
-
end
|
64
|
-
|
65
|
-
##
|
66
|
-
# Builds output string with color escape characters.
|
67
|
-
# @example
|
68
|
-
# Crayon.prepare_string('hello', 'red', 'blue', ['underline']) #=> "\e[31m\e[44m\e[4mhello\e[0m"
|
69
|
-
# @private
|
70
|
-
def prepare_string(string) #, foreground=nil, background=nil, formatting=[])
|
71
|
-
[ prepare_foreground_color,
|
72
|
-
prepare_background_color,
|
73
|
-
prepare_formatting,
|
74
|
-
string,
|
75
|
-
(TERMINATION_STRING if @foreground || @background || !@formatting.empty?),
|
76
|
-
(newline? ? "\n" : "")
|
77
|
-
].join("")
|
78
|
-
end
|
79
|
-
|
80
|
-
# @private
|
81
|
-
def prepare_foreground_color
|
82
|
-
@color = @foreground
|
83
|
-
handle_color(3)
|
84
|
-
end
|
85
|
-
|
86
|
-
# @private
|
87
|
-
def prepare_background_color
|
88
|
-
@color = @background
|
89
|
-
handle_color(4)
|
90
|
-
end
|
91
|
-
|
92
|
-
# @private
|
93
|
-
def prepare_formatting
|
94
|
-
return "" if @formatting.empty?
|
95
|
-
@formatting.map{|format| "\e[#{FORMATS[format]}m"}.join("")
|
96
|
-
end
|
97
|
-
|
98
|
-
# @private
|
99
|
-
def handle_color(lead)
|
100
|
-
return "" unless @color
|
101
|
-
"\e[#{lead}#{COLORS[@color]}m"
|
36
|
+
CrayonString.new(prepare_string(string) || "")
|
102
37
|
end
|
103
38
|
|
104
39
|
# @private
|
105
40
|
def nullify_variables
|
106
41
|
@foreground, @background, @formatting = nil, nil, []
|
107
42
|
@method_name, @color = nil, nil
|
108
|
-
io.flush
|
109
43
|
end
|
110
44
|
end
|
45
|
+
|
46
|
+
require 'crayon_string'
|
47
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Crayon
|
2
|
+
module MethodParser
|
3
|
+
##
|
4
|
+
# Converts a method name into color and formatting parameters
|
5
|
+
# @private
|
6
|
+
def parse_method_name
|
7
|
+
@method_name = @method_name.to_s.downcase.split("_")
|
8
|
+
@background = parse_background
|
9
|
+
@foreground = parse_foreground
|
10
|
+
@formatting = parse_formatting
|
11
|
+
end
|
12
|
+
|
13
|
+
# @private
|
14
|
+
def parse_background
|
15
|
+
_idx = @method_name.index("on")
|
16
|
+
return nil unless _idx
|
17
|
+
@method_name.delete("on")
|
18
|
+
_background = @method_name.delete_at(_idx)
|
19
|
+
_background if COLORS.keys.include?(_background)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @private
|
23
|
+
def parse_foreground
|
24
|
+
@method_name.find {|color| COLORS.keys.include?(color) }
|
25
|
+
end
|
26
|
+
|
27
|
+
# @private
|
28
|
+
def parse_formatting
|
29
|
+
@method_name.select {|format| FORMATS.keys.include?(format) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Crayon
|
2
|
+
module StringBuilder
|
3
|
+
##
|
4
|
+
# Builds output string with color escape characters.
|
5
|
+
# @private
|
6
|
+
def prepare_string(string)
|
7
|
+
[ prepare_foreground_color,
|
8
|
+
prepare_background_color,
|
9
|
+
prepare_formatting,
|
10
|
+
string,
|
11
|
+
(TERMINATION_STRING if @foreground || @background || !@formatting.empty?)
|
12
|
+
].compact.join("")
|
13
|
+
end
|
14
|
+
|
15
|
+
# @private
|
16
|
+
def prepare_foreground_color
|
17
|
+
@color = @foreground
|
18
|
+
handle_color(3)
|
19
|
+
end
|
20
|
+
|
21
|
+
# @private
|
22
|
+
def prepare_background_color
|
23
|
+
@color = @background
|
24
|
+
handle_color(4)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @private
|
28
|
+
def prepare_formatting
|
29
|
+
return "" if @formatting.empty?
|
30
|
+
@formatting.map{|format| "\e[#{FORMATS[format]}m"}.join("")
|
31
|
+
end
|
32
|
+
|
33
|
+
# @private
|
34
|
+
def handle_color(lead)
|
35
|
+
return "" unless @color
|
36
|
+
"\e[#{lead}#{COLORS[@color]}m"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/crayon_spec.rb
CHANGED
@@ -30,23 +30,8 @@ def test_prepare_string(expected_output, *args)
|
|
30
30
|
end
|
31
31
|
|
32
32
|
describe "Crayon" do
|
33
|
-
it "should
|
34
|
-
Crayon.
|
35
|
-
end
|
36
|
-
describe "after a print command" do
|
37
|
-
before { Crayon.print }
|
38
|
-
it "should not add line breaks" do
|
39
|
-
Crayon.newline?.should_not be
|
40
|
-
end
|
41
|
-
end
|
42
|
-
describe "after a puts command" do
|
43
|
-
before { Crayon.puts }
|
44
|
-
it "should add line breaks" do
|
45
|
-
Crayon.newline?.should be
|
46
|
-
end
|
47
|
-
end
|
48
|
-
it "chaining color calls should be allowed by return Crayon from method_missing" do
|
49
|
-
Crayon.red("OK").should == Crayon
|
33
|
+
it "chaining color calls should be allowed by returning a CrayonString from method_missing" do
|
34
|
+
Crayon.red("OK").should be_a CrayonString
|
50
35
|
end
|
51
36
|
describe "method_missing" do
|
52
37
|
describe "should call :prepare string" do
|
@@ -55,21 +40,6 @@ describe "Crayon" do
|
|
55
40
|
Crayon.red("hello")
|
56
41
|
end
|
57
42
|
end
|
58
|
-
describe "should unset instance variables after being called" do
|
59
|
-
before { Crayon.bold_red_on_green("hello") }
|
60
|
-
it "should unset foreground" do
|
61
|
-
Crayon.foreground.should be_nil
|
62
|
-
end
|
63
|
-
it "should unset background" do
|
64
|
-
Crayon.background.should be_nil
|
65
|
-
end
|
66
|
-
it "should unset formatting" do
|
67
|
-
Crayon.formatting.should be_empty
|
68
|
-
end
|
69
|
-
it "should unset method_name" do
|
70
|
-
Crayon.method_name.should be_nil
|
71
|
-
end
|
72
|
-
end
|
73
43
|
end
|
74
44
|
describe "parse_method_name" do
|
75
45
|
test_parse_method_name(:red, "red", nil, [])
|
@@ -100,20 +70,9 @@ describe "Crayon" do
|
|
100
70
|
end
|
101
71
|
|
102
72
|
describe "prepare_string" do
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
test_prepare_string("\e[33m\e[47mhello\e[0m", "hello", "yellow", "white")
|
108
|
-
test_prepare_string("\e[4m\e[1mhello\e[0m", "hello", nil, nil, ["underline", "bold"])
|
109
|
-
end
|
110
|
-
|
111
|
-
describe "with a line break" do
|
112
|
-
before { Crayon.puts }
|
113
|
-
test_prepare_string("hello\n", "hello")
|
114
|
-
test_prepare_string("\e[31mhello\e[0m\n", "hello", "red", nil)
|
115
|
-
test_prepare_string("\e[33m\e[47mhello\e[0m\n", "hello", "yellow", "white")
|
116
|
-
test_prepare_string("\e[4m\e[1mhello\e[0m\n", "hello", nil, nil, ["underline", "bold"])
|
117
|
-
end
|
73
|
+
test_prepare_string("hello", "hello")
|
74
|
+
test_prepare_string("\e[31mhello\e[0m", "hello", "red", nil)
|
75
|
+
test_prepare_string("\e[33m\e[47mhello\e[0m", "hello", "yellow", "white")
|
76
|
+
test_prepare_string("\e[4m\e[1mhello\e[0m", "hello", nil, nil, ["underline", "bold"])
|
118
77
|
end
|
119
78
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 1.0.
|
8
|
+
- 3
|
9
|
+
version: 1.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Berkowitz
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-22 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -38,6 +38,9 @@ files:
|
|
38
38
|
- Rakefile
|
39
39
|
- VERSION
|
40
40
|
- lib/crayon.rb
|
41
|
+
- lib/crayon/crayon_string.rb
|
42
|
+
- lib/crayon/method_parser.rb
|
43
|
+
- lib/crayon/string_builder.rb
|
41
44
|
- spec/crayon_spec.rb
|
42
45
|
- spec/helper.rb
|
43
46
|
- spec/spec.opts
|