Skip to content

Instantly share code, notes, and snippets.

@paul-appsinyourpants
Created February 18, 2011 22:49
Show Gist options
  • Save paul-appsinyourpants/834555 to your computer and use it in GitHub Desktop.
Save paul-appsinyourpants/834555 to your computer and use it in GitHub Desktop.
Add color to console logging easily in ruby
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!"
@shalvah
Copy link

shalvah commented Oct 14, 2022

Thank you 👏 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment