pretty_console 0.0.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 +7 -0
- data/lib/pretty_console/invalid_color_error.rb +10 -0
- data/lib/pretty_console.rb +169 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4c8f384cc0171d47d47c79ffbee696731b64c45754e57acb24a17516a5b73e5d
|
4
|
+
data.tar.gz: 42143c7c49e749ee581d64a23378751ced816b16defb3ede8bf55486996d8cee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7216a6d220124b6cf62a4e4c56a46e2dcbf8ff81b86f0f8e63e604299de8603eef19db9bbc83515d39e2883a67c2ba6db1795463874d9d1b9a0f4fc649e7dd31
|
7
|
+
data.tar.gz: b6f3bd0d68721f3a725f6a081e33c7203a60f5b769b662f05bb7e3c43344ad5dc62ae555ebe272cbc6e7ae4c2e1cf9495d377bdcda5a851d58085f821f4c5266
|
@@ -0,0 +1,169 @@
|
|
1
|
+
# The main PrettyConsole driver
|
2
|
+
# The PrettyConsole class provides methods to print and display strings in various colors and styles
|
3
|
+
# in the console. It supports both foreground and background colors, as well as bold text.
|
4
|
+
#
|
5
|
+
# Constants:
|
6
|
+
# - COLOR_MAP: A hash mapping color names to their corresponding ANSI color codes for text.
|
7
|
+
# - BACKGROUND_COLOR_MAP: A hash mapping color names to their corresponding ANSI color codes for background.
|
8
|
+
#
|
9
|
+
# Methods:
|
10
|
+
# - say_in_<color>(str): Prints the string with the specified color and enhanced formatting.
|
11
|
+
# - say_in_<color>_loudly(str): Prints the string with the specified color, enhanced formatting, and bold text.
|
12
|
+
# - puts_in_<color>(str): Prints the string with the specified color.
|
13
|
+
# - puts_in_<color>_loudly(str): Prints the string with the specified color and bold text.
|
14
|
+
# - print_in_<color>(str): Prints the string with the specified color without a newline.
|
15
|
+
# - say_with_<color>_background(str): Prints the string with the specified background color and enhanced formatting.
|
16
|
+
# - puts_with_<color>_background(str): Prints the string with the specified background color.
|
17
|
+
# - print_with_<color>_background(str): Prints the string with the specified background color without a newline.
|
18
|
+
# - announce_task(task): Prints a message indicating the start and end of a task, with timing information.
|
19
|
+
# - enhance_str(str): Enhances the string with additional formatting.
|
20
|
+
# - bold(str): Returns the string in bold text.
|
21
|
+
# - express_in_color(str, color, map = COLOR_MAP): Returns the string formatted with the specified color.
|
22
|
+
require 'pretty_console/invalid_color_error'
|
23
|
+
|
24
|
+
# PrettyConsole is a utility class for printing colored and formatted text to the console.
|
25
|
+
module PrettyConsole
|
26
|
+
# Maps color names to their corresponding ANSI color codes.
|
27
|
+
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.
|
56
|
+
#
|
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)
|
61
|
+
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
|
+
end
|
168
|
+
end
|
169
|
+
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pretty_console
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Etienne Weil
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem to colorize your console output
|
14
|
+
email: weil.etienne@hotmail.fr
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/pretty_console.rb
|
20
|
+
- lib/pretty_console/invalid_color_error.rb
|
21
|
+
homepage: https://rubygems.org/gems/pretty_console
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubygems_version: 3.5.22
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: pretty_console summary
|
44
|
+
test_files: []
|