Created
February 18, 2011 22:49
-
-
Save paul-appsinyourpants/834555 to your computer and use it in GitHub Desktop.
Add color to console logging easily in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COLOR_ESCAPES = { | |
:none => 0, | |
:bright => 1, | |
:black => 30, | |
:red => 31, | |
:green => 32, | |
:yellow => 33, | |
:blue => 34, | |
:magenta => 35, | |
:cyan => 36, | |
:white => 37, | |
:default => 39, | |
} | |
def c( clr, text = nil ) | |
"\x1B[" + ( COLOR_ESCAPES[ clr ] || 0 ).to_s + 'm' + ( text ? text + "\x1B[0m" : "" ) | |
end | |
def bc( clr, text = nil ) | |
"\x1B[" + ( ( COLOR_ESCAPES[ clr ] || 0 ) + 10 ).to_s + 'm' + ( text ? text + "\x1B[0m" : "" ) | |
end | |
# Can be used to color a string | |
# logger.error c :red, "Core failure!" | |
# | |
# Or background | |
# logger.warn bc :cyan, "My eyes!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you 👏 ❤️