module Hanami::Utils::ShellColor
Shell helper for colorizing STDOUT
It doesn’t check if you’re writing to a file or anything, so you have to check that yourself before using this module.
@api public @since 1.2.0
Constants
- COLORS
Escapes codes for terminals to output strings in colors
@since 1.2.0 @api private
Public Class Methods
call(input, color:)
click to toggle source
Colorizes output 8 colors available: black, red, green, yellow, blue, magenta, cyan, and gray
@param input [#to_s] the string to colorize @param color [Symbol] the color
@raise [Hanami::Utils::ShellColor::UnknownColorError] if the color code is
unknown
@return [String] the colorized string
@since 1.2.0
# File lib/hanami/utils/shell_color.rb, line 52 def self.call(input, color:) "\e[#{color_code(color)}m#{input}\e[0m" end
color_code(code)
click to toggle source
Helper method to translate between color names and terminal escape codes
@api private @since 1.2.0
@raise [Hanami::Utils::ShellColor::UnknownColorError] if the color code is
unknown
# File lib/hanami/utils/shell_color.rb, line 63 def self.color_code(code) COLORS.fetch(code) { raise UnknownColorCodeError.new(code) } end