Skip to content

Instantly share code, notes, and snippets.

@nas
Created December 17, 2009 08:06
Show Gist options
  • Save nas/258614 to your computer and use it in GitHub Desktop.
Save nas/258614 to your computer and use it in GitHub Desktop.
Colour strings
# taken and modified from http://www.andrewmcdonough.com/2009/10/20/colorized-logs-in-ruby
def colorify(string, color, options = {})
offsets = ["gray","red", "green", "yellow", "blue", "magenta", "cyan","white"]
# I don't think below LOC is quite right due to the fact that xx and underline strings
# are appearing multiple times; will fix it when I am about to use this script.
styles = ["normal","bold","dark","italic","underline","xx","xx","underline","xx","strikethrough"]
start = options[:bg] ? 40 : 30
color_code = start + offsets.index(color)
style_code = styles.index(options[:style]) || 0
"\e[#{style_code};#{color_code}m#{string}\e[0m"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment