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

Class Method Details

.announce_task(task) { ... } ⇒ Object

Announces the start and end of a task, printing the task name and duration.

Parameters:

  • task (String, Object)

    the task to announce

Yields:

  • the block representing the task to be executed



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.

Parameters:

  • str (String)

    the string to bold

Returns:

  • (String)

    the bolded string



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.

Parameters:

  • str (String)

    the string to enhance

Returns:

  • (String)

    the enhanced string



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.

Parameters:

  • str (String)

    the string to color

  • color (Symbol)

    the color to use

  • map (Hash) (defaults to: COLOR_MAP)

    the color map to use (default: COLOR_MAP)

Returns:

  • (String)

    the colored string

Raises:



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