pretty_console 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 02c2b32106ec5feffa80799215919955a1cf57923beff7eec08309e29bf3aabd
4
- data.tar.gz: d813b5c9e077ea284783371b40b5d8486c7a2d03b97a9bbb91bdd7440f02a76e
3
+ metadata.gz: ddd97f1fc231e8610fc786769b06bb4b9d54a72ca26b60d9a0c40e46caf56286
4
+ data.tar.gz: fe9e662b38c2409944230d2cd92bca89168d8d903e7ba31f9c49fd7b81aaee1d
5
5
  SHA512:
6
- metadata.gz: 29ad9c791d652b8e2a0fc86001254aeaf7136a3163a9aeda221229be17d2e8e88d57c46a8aa5c92f6a4a0c9b5a30e59bcb70fefeb749e3eab4ab3cd5216542f6
7
- data.tar.gz: 90e25e819868e3100208e78ff4583318079870a2de0040bcff381a7add441f4a67638dc5175cba1cb428f1b473e7ea116f810caee9851c575d451f6ebec09ad0
6
+ metadata.gz: 7d90b29878a175745634c83f9f23fc49cefaf0ea60a7177443fce6e890d956435ac3a9ee45f40824d3e01cbb2d845606555c3a0046fe165ce43f365cda9df299
7
+ data.tar.gz: c168679582a388a153e376300bb1a88e1a6d97dbd1ddaa0036b11674ffd95205a1a7c77610ef318d140637c2c8edaf4c56d382cc585e93af1a6bbc5475437179
data/README.md CHANGED
@@ -2,12 +2,12 @@
2
2
 
3
3
  PrettyConsole adds colors to your console using the [bash colorization codes](http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html).
4
4
 
5
- ## Use
5
+ ## Usage
6
6
 
7
7
  #### puts_in_< color >
8
-
9
- PrettyConsole.puts_in_green('Some wording')
10
-
8
+ ```Ruby
9
+ self.puts_in_green('Some wording')
10
+ ```
11
11
  prints the following with standard system font
12
12
  $${\color{green} Some\space wording}$$
13
13
 
@@ -25,14 +25,18 @@ where 'green' is a < color > that might be replaced with the following colors :
25
25
 
26
26
  #### say_in_< color >
27
27
 
28
- PrettyConsole.say_in_<color>('Some wording')
28
+ ```Ruby
29
+ self.say_in_<color>('Some wording')
30
+ ```
29
31
 
30
32
  prints the following with standard system font
31
33
  $${\color{green} =====>\space Some\space wording\space<=====}$$
32
34
 
33
35
  #### print_in_< color >
34
36
 
35
- PrettyConsole.print_in_<color>('x')
37
+ ```Ruby
38
+ self.print_in_<color>('x')
39
+ ```
36
40
 
37
41
  prints the following with standard system font
38
42
  $${\color{green}x}$$
@@ -40,16 +44,20 @@ $${\color{green}x}$$
40
44
 
41
45
  #### say_with_color_background
42
46
 
43
- PrettyConsole.say_with_<color>_background('Some wording')
47
+ ```Ruby
48
+ self.say_with_<color>_background('Some wording')
49
+ ```
44
50
 
45
51
  #### announce_task
46
52
 
47
- desc 'your task'
48
- task your_task: :environment do |task|
49
- PrettyConsole.announce_task(task or string) do
50
- ...your task code
51
- end
52
- end
53
+ ```Ruby
54
+ desc 'your task'
55
+ task your_task: :environment do |task|
56
+ self.announce_task(task or string) do
57
+ ...your task code
58
+ end
59
+ end
60
+ ```
53
61
 
54
62
  - prints a colored header with the task name and a footer
55
63
  - it displays the time elapased inside the block
@@ -1,3 +1,3 @@
1
1
  module PrettyConsole
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -23,147 +23,157 @@ require 'pretty_console/invalid_color_error'
23
23
 
24
24
  # PrettyConsole is a utility class for printing colored and formatted text to the console.
25
25
  module PrettyConsole
26
- # Maps color names to their corresponding ANSI color codes.
27
26
  COLOR_MAP = {
28
- red: 31,
29
- green: 32,
30
- yellow: 33,
31
- blue: 34,
32
- purple: 35,
33
- cyan: 36,
34
- heavy_white: 37
35
- }
36
-
37
- # Maps background color names to their corresponding ANSI background color codes.
38
- BACKGROUND_COLOR_MAP = {
39
- leight: 40,
40
- red: 41,
41
- green: 42,
42
- orange: 43,
43
- blue: 44,
44
- purple: 45,
45
- cyan: 46,
46
- white: 47
47
- }
48
-
49
- # Dynamically defines methods for printing text in various colors.
50
- #
51
- # Example:
52
- # PrettyConsole.say_in_red('Hello World')
53
- # PrettyConsole.puts_in_green_loudly('Hello World')
54
- COLOR_MAP.keys.each do |color|
55
- # Prints the given string in the specified color.
27
+ red: 31,
28
+ green: 32,
29
+ yellow: 33,
30
+ blue: 34,
31
+ purple: 35,
32
+ cyan: 36,
33
+ heavy_white: 37
34
+ }
35
+
36
+ # Maps background color names to their corresponding ANSI background color codes.
37
+ BACKGROUND_COLOR_MAP = {
38
+ leight: 40,
39
+ red: 41,
40
+ green: 42,
41
+ orange: 43,
42
+ blue: 44,
43
+ purple: 45,
44
+ cyan: 46,
45
+ white: 47
46
+ }
47
+ def self.included(base)
48
+ base.extend self
49
+
50
+ # Maps color names to their corresponding ANSI color codes.
51
+
52
+ # Dynamically defines methods for printing text in various colors.
56
53
  #
57
- # @param str [String] the string to print
58
- define_singleton_method("say_in_#{color}".to_sym) do |str|
59
- puts ''
60
- puts express_in_color(enhance_str(str), color)
54
+ # Example:
55
+ # PrettyConsole.say_in_red('Hello World')
56
+ # PrettyConsole.puts_in_green_loudly('Hello World')
57
+ COLOR_MAP.keys.each do |color|
58
+ # Prints the given string in the specified color.
59
+ #
60
+ # @param str [String] the string to print
61
+ define_method("say_in_#{color}".to_sym) do |str|
62
+ puts ''
63
+ puts express_in_color(enhance_str(str), color)
64
+ end
65
+
66
+ # Prints the given string in the specified color with bold formatting.
67
+ #
68
+ # @param str [String] the string to print
69
+ define_method("say_in_#{color}_loudly".to_sym) do |str|
70
+ puts ''
71
+ puts express_in_color(enhance_str(bold(str)), color)
72
+ end
73
+
74
+ # Prints the given string in the specified color without a newline.
75
+ #
76
+ # @param str [String] the string to print
77
+ define_method("puts_in_#{color}".to_sym) do |str|
78
+ puts express_in_color(str, color)
79
+ end
80
+
81
+ # Prints the given string in the specified color with bold formatting without a newline.
82
+ #
83
+ # @param str [String] the string to print
84
+ define_method("puts_in_#{color}_loudly".to_sym) do |str|
85
+ puts express_in_color(bold(str), color)
86
+ end
87
+
88
+ # Prints the given string in the specified color without a newline.
89
+ #
90
+ # @param str [String] the string to print
91
+ define_method("print_in_#{color}".to_sym) do |str|
92
+ print express_in_color(str, color)
93
+ end
94
+
95
+ # Dynamically defines methods for printing text with various background colors.
96
+ #
97
+ # Example:
98
+ # PrettyConsole.say_with_red_background('Hello World')
99
+ # PrettyConsole.puts_with_green_background('Hello World')
100
+ BACKGROUND_COLOR_MAP.keys.each do |color|
101
+ # Prints the given string with the specified background color.
102
+ #
103
+ # @param str [String] the string to print
104
+ define_method("say_with_#{color}_background".to_sym) do |str|
105
+ puts express_in_color(enhance_str(str), color, BACKGROUND_COLOR_MAP)
106
+ end
107
+
108
+ # Prints the given string with the specified background color without a newline.
109
+ #
110
+ # @param str [String] the string to print
111
+ define_method("puts_with_#{color}_background".to_sym) do |str|
112
+ puts express_in_color(str, color, BACKGROUND_COLOR_MAP)
113
+ end
114
+
115
+ # Prints the given string with the specified background color without a newline.
116
+ #
117
+ # @param str [String] the string to print
118
+ define_method("print_with_#{color}_background".to_sym) do |str|
119
+ print express_in_color(str, color, BACKGROUND_COLOR_MAP)
120
+ end
121
+ end
122
+
123
+ # Announces the start and end of a task, printing the task name and duration.
124
+ #
125
+ # @param task [String, Object] the task to announce
126
+ # @yield the block representing the task to be executed
127
+ def self.announce_task(task)
128
+ label = task.is_a?(String) ? task : task&.name
129
+ return unless label
130
+
131
+ puts_with_green_background "-- Starting task : #{label}"
132
+ start_time = Time.now
133
+ yield
134
+ end_time = Time.now
135
+ puts ''
136
+ puts_in_blue_loudly "-------- Task completed. Took #{end_time - start_time} seconds"
137
+ puts_in_green "-- end #{label} ----"
138
+ end
139
+
140
+ # Enhances the given string by adding decorative markers.
141
+ #
142
+ # @param str [String] the string to enhance
143
+ # @return [String] the enhanced string
144
+
145
+ private
146
+
147
+
148
+ def enhance_str(str)
149
+ "=====> #{str} <====="
150
+ end
151
+
152
+ # Makes the given string bold.
153
+ #
154
+ # @param str [String] the string to bold
155
+ # @return [String] the bolded string
156
+ def bold(str)
157
+ "\x1b[1m#{str}\x1b[0m"
158
+ end
159
+
160
+ # Colors the given string using the specified color map.
161
+ #
162
+ # @param str [String] the string to color
163
+ # @param color [Symbol] the color to use
164
+ # @param map [Hash] the color map to use (default: COLOR_MAP)
165
+ # @return [String] the colored string
166
+ # @raise [InvalidColorError] if the color is not found in the map
167
+ def express_in_color(str, color, map = COLOR_MAP)
168
+ raise InvalidColorError, " color: #{color}" unless map.key?(color.to_sym)
169
+
170
+ "\e[#{map[color.to_sym]}m#{str}\e[0m"
171
+ rescue InvalidColorError => e
172
+ "There's no method called #{color} here -- please try again with " \
173
+ "one of the following colors: red, green, yellow, blue, purple, cyan," \
174
+ "#{e}"
175
+ end
61
176
  end
62
-
63
- # Prints the given string in the specified color with bold formatting.
64
- #
65
- # @param str [String] the string to print
66
- define_singleton_method("say_in_#{color}_loudly".to_sym) do |str|
67
- puts ''
68
- puts express_in_color(enhance_str(bold(str)), color)
69
- end
70
-
71
- # Prints the given string in the specified color without a newline.
72
- #
73
- # @param str [String] the string to print
74
- define_singleton_method("puts_in_#{color}".to_sym) do |str|
75
- puts express_in_color(str, color)
76
- end
77
-
78
- # Prints the given string in the specified color with bold formatting without a newline.
79
- #
80
- # @param str [String] the string to print
81
- define_singleton_method("puts_in_#{color}_loudly".to_sym) do |str|
82
- puts express_in_color(bold(str), color)
83
- end
84
-
85
- # Prints the given string in the specified color without a newline.
86
- #
87
- # @param str [String] the string to print
88
- define_singleton_method("print_in_#{color}".to_sym) do |str|
89
- print express_in_color(str, color)
90
- end
91
- end
92
-
93
- # Dynamically defines methods for printing text with various background colors.
94
- #
95
- # Example:
96
- # PrettyConsole.say_with_red_background('Hello World')
97
- # PrettyConsole.puts_with_green_background('Hello World')
98
- BACKGROUND_COLOR_MAP.keys.each do |color|
99
- # Prints the given string with the specified background color.
100
- #
101
- # @param str [String] the string to print
102
- define_singleton_method("say_with_#{color}_background".to_sym) do |str|
103
- puts express_in_color(enhance_str(str), color, BACKGROUND_COLOR_MAP)
104
- end
105
-
106
- # Prints the given string with the specified background color without a newline.
107
- #
108
- # @param str [String] the string to print
109
- define_singleton_method("puts_with_#{color}_background".to_sym) do |str|
110
- puts express_in_color(str, color, BACKGROUND_COLOR_MAP)
111
- end
112
-
113
- # Prints the given string with the specified background color without a newline.
114
- #
115
- # @param str [String] the string to print
116
- define_singleton_method("print_with_#{color}_background".to_sym) do |str|
117
- print express_in_color(str, color, BACKGROUND_COLOR_MAP)
118
- end
119
- end
120
-
121
- # Announces the start and end of a task, printing the task name and duration.
122
- #
123
- # @param task [String, Object] the task to announce
124
- # @yield the block representing the task to be executed
125
- def self.announce_task(task)
126
- label = task.is_a?(String) ? task : task&.name
127
- return unless label
128
-
129
- puts_with_green_background "-- Starting task : #{label}"
130
- start_time = Time.now
131
- yield
132
- end_time = Time.now
133
- puts ''
134
- puts_in_blue_loudly "-------- Task completed. Took #{end_time - start_time} seconds"
135
- puts_in_green "-- end #{label} ----"
136
- end
137
-
138
- # Enhances the given string by adding decorative markers.
139
- #
140
- # @param str [String] the string to enhance
141
- # @return [String] the enhanced string
142
- def self.enhance_str(str)
143
- "=====> #{str} <====="
144
- end
145
-
146
- # Makes the given string bold.
147
- #
148
- # @param str [String] the string to bold
149
- # @return [String] the bolded string
150
- def self.bold(str)
151
- "\x1b[1m#{str}\x1b[0m"
152
- end
153
-
154
- # Colors the given string using the specified color map.
155
- #
156
- # @param str [String] the string to color
157
- # @param color [Symbol] the color to use
158
- # @param map [Hash] the color map to use (default: COLOR_MAP)
159
- # @return [String] the colored string
160
- # @raise [InvalidColorError] if the color is not found in the map
161
- def self.express_in_color(str, color, map = COLOR_MAP)
162
- raise InvalidColorError, " color: #{color}" unless map.key?(color.to_sym)
163
- "\e[#{map[color.to_sym]}m#{str}\e[0m"
164
- rescue InvalidColorError => e
165
- "There's no method called #{color} here -- please try again with " \
166
- "one of the following colors: red, green, yellow, blue, purple, cyan"
167
177
  end
168
178
  end
169
179
 
@@ -22,94 +22,105 @@ class PrettyConsoleTest < Minitest::Test
22
22
  end
23
23
  end
24
24
 
25
- PrettyConsole::COLOR_MAP.keys.each do |color|
26
- define_method("test_say_in_#{color}") do
27
- PrettyConsole.send("say_in_#{color}", 'Hello World')
28
- assert_includes @output.string, PrettyConsole.express_in_color(PrettyConsole.enhance_str('Hello World'), color)
29
- end
30
-
31
- define_method("test_say_in_#{color}_loudly") do
32
- PrettyConsole.send("say_in_#{color}_loudly", 'Hello World')
33
- assert_includes @output.string, PrettyConsole.express_in_color(PrettyConsole.enhance_str(PrettyConsole.bold('Hello World')), color)
34
- end
35
-
36
- define_method("test_puts_in_#{color}") do
37
- PrettyConsole.send("puts_in_#{color}", 'Hello World')
38
- assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color)
39
- end
40
-
41
- define_method("test_puts_in_#{color}_loudly") do
42
- PrettyConsole.send("puts_in_#{color}_loudly", 'Hello World')
43
- assert_includes @output.string, PrettyConsole.express_in_color(PrettyConsole.bold('Hello World'), color)
44
- end
45
-
46
- define_method("test_print_in_#{color}") do
47
- PrettyConsole.send("print_in_#{color}", 'Hello World')
48
- assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color)
49
- end
50
-
51
- def test_express_in_color_with_default_map
52
- result = PrettyConsole.express_in_color('Hello', :red)
53
- assert_equal "\e[31mHello\e[0m", result
54
- end
55
-
56
- def test_express_in_color_with_custom_map
57
- custom_map = { red: 91 }
58
- result = PrettyConsole.express_in_color('Hello', :red, custom_map)
59
- assert_equal "\e[91mHello\e[0m", result
60
- end
61
-
62
- def test_express_in_color_with_invalid_color
63
- assert_includes PrettyConsole.express_in_color('Hello', :invalid_color),
64
- "There's no method called"
65
- end
66
-
67
- def test_bold
68
- input = "Hello World"
69
- expected_output = "\x1b[1mHello World\x1b[0m"
70
- assert_equal expected_output, PrettyConsole.bold(input)
25
+ class DummyClass
26
+ include PrettyConsole
27
+ def initialize
28
+ @test = 'test'
71
29
  end
72
-
73
- end
74
- def test_announce_task
75
- task_name = "Sample Task"
76
- PrettyConsole.announce_task(task_name) do
77
- sleep 1
78
- end
79
-
80
- output = @output.string
81
- assert_includes output, "-- Starting task : Sample Task"
82
- assert_includes output, "-------- Task completed. Took"
83
- assert_includes output, "-- end Sample Task ----"
84
- end
85
-
86
- def test_announce_task_with_object
87
-
88
- task = DummyTask.new(name: "Object Task")
89
- PrettyConsole.announce_task(task) do
90
- sleep 1
91
- end
92
-
93
- output = @output.string
94
- assert_includes output, "-- Starting task : "
95
- assert_includes output, "[0m\n\n\e[34m\e[1m-------- Task completed. Took 1."
96
30
  end
97
31
 
98
-
99
- PrettyConsole::BACKGROUND_COLOR_MAP.keys.each do |color|
100
- define_method("test_say_with_#{color}_background") do
101
- PrettyConsole.send("say_with_#{color}_background", 'Hello World')
102
- assert_includes @output.string, PrettyConsole.express_in_color(PrettyConsole.enhance_str('Hello World'), color, PrettyConsole::BACKGROUND_COLOR_MAP)
103
- end
104
-
105
- define_method("test_puts_with_#{color}_background") do
106
- PrettyConsole.send("puts_with_#{color}_background", 'Hello World')
107
- assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color, PrettyConsole::BACKGROUND_COLOR_MAP)
108
- end
109
-
110
- define_method("test_print_with_#{color}_background") do
111
- PrettyConsole.send("print_with_#{color}_background", 'Hello World')
112
- assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color, PrettyConsole::BACKGROUND_COLOR_MAP)
113
- end
32
+ PrettyConsole::COLOR_MAP.keys.each do |color|
33
+ # puts '================================'
34
+ # puts "base.instance_methods : #{PrettyConsoleTest::DummyClass.instance_methods}"
35
+ # puts '================================'
36
+ # puts
37
+ define_method("test_say_in_#{color}") do
38
+ PrettyConsoleTest::DummyClass.new.send("say_in_#{color}", 'Hello World')
39
+ assert_includes @output.string, PrettyConsoleTest::DummyClass.new.express_in_color(PrettyConsoleTest::DummyClass.new.enhance_str('Hello World'), color)
40
+ end
41
+
42
+ # define_method("test_say_in_#{color}_loudly") do
43
+ # DummyClass.new.send("say_in_#{color}_loudly", 'Hello World')
44
+ # assert_includes @output.string, DummyClass.new.express_in_color(DummyClass.new.enhance_str(DummyClass.new.bold('Hello World')), color)
45
+ # end
46
+
47
+ # define_method("test_puts_in_#{color}") do
48
+ # DummyClass.new.send("puts_in_#{color}", 'Hello World')
49
+ # assert_includes @output.string, DummyClass.new.express_in_color('Hello World', color)
50
+ # end
51
+
52
+ # define_method("test_puts_in_#{color}_loudly") do
53
+ # DummyClass.new.send("puts_in_#{color}_loudly", 'Hello World')
54
+ # assert_includes @output.string, DummyClass.new.express_in_color(DummyClass.new.bold('Hello World'), color)
55
+ # end
56
+
57
+ # define_method("test_print_in_#{color}") do
58
+ # DummyClass.new.send("print_in_#{color}", 'Hello World')
59
+ # assert_includes @output.string, DummyClass.new.express_in_color('Hello World', color)
60
+ # end
61
+
62
+ # def test_express_in_color_with_default_map
63
+ # result = DummyClass.new.express_in_color('Hello', :red)
64
+ # assert_equal "\e[31mHello\e[0m", result
65
+ # end
66
+
67
+ # def test_express_in_color_with_custom_map
68
+ # custom_map = { red: 91 }
69
+ # result = DummyClass.new.express_in_color('Hello', :red, custom_map)
70
+ # assert_equal "\e[91mHello\e[0m", result
71
+ # end
72
+
73
+ # def test_express_in_color_with_invalid_color
74
+ # assert_includes DummyClass.new.express_in_color('Hello', :invalid_color),
75
+ # "There's no method called"
76
+ # end
77
+
78
+ # def test_bold
79
+ # input = "Hello World"
80
+ # expected_output = "\x1b[1mHello World\x1b[0m"
81
+ # assert_equal expected_output, DummyClass.new.bold(input)
82
+ # end
83
+
84
+ # end
85
+ # def test_announce_task
86
+ # task_name = "Sample Task"
87
+ # PrettyConsole.announce_task(task_name) do
88
+ # sleep 1
89
+ # end
90
+
91
+ # output = @output.string
92
+ # assert_includes output, "-- Starting task : Sample Task"
93
+ # assert_includes output, "-------- Task completed. Took"
94
+ # assert_includes output, "-- end Sample Task ----"
95
+ # end
96
+
97
+ # def test_announce_task_with_object
98
+
99
+ # task = DummyTask.new(name: "Object Task")
100
+ # PrettyConsole.announce_task(task) do
101
+ # sleep 1
102
+ # end
103
+
104
+ # output = @output.string
105
+ # assert_includes output, "-- Starting task : "
106
+ # assert_includes output, "[0m\n\n\e[34m\e[1m-------- Task completed. Took 1."
107
+ # end
108
+
109
+
110
+ # PrettyConsole::BACKGROUND_COLOR_MAP.keys.each do |color|
111
+ # define_method("test_say_with_#{color}_background") do
112
+ # PrettyConsole.send("say_with_#{color}_background", 'Hello World')
113
+ # assert_includes @output.string, PrettyConsole.express_in_color(PrettyConsole.enhance_str('Hello World'), color, PrettyConsole::BACKGROUND_COLOR_MAP)
114
+ # end
115
+
116
+ # define_method("test_puts_with_#{color}_background") do
117
+ # PrettyConsole.send("puts_with_#{color}_background", 'Hello World')
118
+ # assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color, PrettyConsole::BACKGROUND_COLOR_MAP)
119
+ # end
120
+
121
+ # define_method("test_print_with_#{color}_background") do
122
+ # PrettyConsole.send("print_with_#{color}_background", 'Hello World')
123
+ # assert_includes @output.string, PrettyConsole.express_in_color('Hello World', color, PrettyConsole::BACKGROUND_COLOR_MAP)
124
+ # end
114
125
  end
115
126
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Etienne Weil
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-09 00:00:00.000000000 Z
10
+ date: 2025-07-13 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: A simple gem to colorize your console output
13
13
  email: weil.etienne@hotmail.fr