Module: PrettyConsole
- Defined in:
- lib/pretty_console.rb,
lib/pretty_console/version.rb,
lib/pretty_console/invalid_color_error.rb
Overview
PrettyConsole is a utility class for printing colored and formatted text to the console.
Defined Under Namespace
Classes: InvalidColorError, PrettyConsoleError
Constant Summary collapse
- COLOR_MAP =
Maps color names to their corresponding ANSI color codes.
{ red: 31, green: 32, yellow: 33, blue: 34, purple: 35, cyan: 36, heavy_white: 37 }
- BACKGROUND_COLOR_MAP =
Maps background color names to their corresponding ANSI background color codes.
{ leight: 40, red: 41, green: 42, orange: 43, blue: 44, purple: 45, cyan: 46, white: 47 }
- VERSION =
"0.1.1"
Class Method Summary collapse
-
.announce_task(task) { ... } ⇒ Object
Announces the start and end of a task, printing the task name and duration.
-
.bold(str) ⇒ String
Makes the given string bold.
-
.enhance_str(str) ⇒ String
Enhances the given string by adding decorative markers.
-
.express_in_color(str, color, map = COLOR_MAP) ⇒ String
Colors the given string using the specified color map.
Class Method Details
.announce_task(task) { ... } ⇒ Object
Announces the start and end of a task, printing the task name and duration.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/pretty_console.rb', line 125 def self.announce_task(task) label = task.is_a?(String) ? task : task&.name return unless label puts_with_green_background "-- Starting task : #{label}" start_time = Time.now yield end_time = Time.now puts '' puts_in_blue_loudly "-------- Task completed. Took #{end_time - start_time} seconds" puts_in_green "-- end #{label} ----" end |
.bold(str) ⇒ String
Makes the given string bold.
150 151 152 |
# File 'lib/pretty_console.rb', line 150 def self.bold(str) "\x1b[1m#{str}\x1b[0m" end |
.enhance_str(str) ⇒ String
Enhances the given string by adding decorative markers.
142 143 144 |
# File 'lib/pretty_console.rb', line 142 def self.enhance_str(str) "=====> #{str} <=====" end |
.express_in_color(str, color, map = COLOR_MAP) ⇒ String
Colors the given string using the specified color map.
161 162 163 164 165 166 167 |
# File 'lib/pretty_console.rb', line 161 def self.express_in_color(str, color, map = COLOR_MAP) raise InvalidColorError, " color: #{color}" unless map.key?(color.to_sym) "\e[#{map[color.to_sym]}m#{str}\e[0m" rescue InvalidColorError => e "There's no method called #{color} here -- please try again with " \ "one of the following colors: red, green, yellow, blue, purple, cyan" end |