Skip to content

Instantly share code, notes, and snippets.

@bf4

bf4/colorize.rb

Last active Aug 29, 2015
Embed
What would you like to do?
ansi terminal colorize
# see https://github.com/rubychan/coderay/blob/master/lib/coderay/encoders/terminal.rb
# https://rubygems.org/gems/text-highlight
module Colorize
ESCAPE_CODES_PATTERN = Regexp.new('\e\[(?:\d;)?\d{1,2}m')
module ColorCode
module_function
def color(name, background=false)
number = case name
when 'black' then 30
when 'red' then 31
when 'green' then 32
when 'yellow' then 33
when 'blue' then 34
when 'magenta' then 35
when 'cyan' then 36
when 'white' then 37
else fail "No code for color #{name}"
end
number = number + 10 if background
"#{number}m"
end
end
module_function
def escape_codes(string)
string.to_s.scan(ESCAPE_CODES_PATTERN)
end
def escape_code_start
"\033"
end
def escape_code_reset
"\033[0m"
end
def escape_code_color(ansi_code, bold=false)
"[#{bold ? '1;' : '0;'}#{ansi_code}"
end
def colorize(ansi_code, msg, bold)
"\033[#{bold ? '1;' : '0;'}#{ansi_code}#{msg}\033[0m"
end
module Text
module_function
def black(msg, bold=false)
Colorize.colorize("30m", msg, bold)
end
def red(msg, bold=false)
Colorize.colorize("31m", msg, bold)
end
def green(msg, bold=false)
Colorize.colorize("32m", msg, bold)
end
def yellow(msg, bold=false)
Colorize.colorize("33m", msg, bold)
end
def blue(msg, bold=false)
Colorize.colorize("34m", msg, bold)
end
def magenta(msg, bold=false)
Colorize.colorize("35m", msg, bold)
end
def cyan(msg, bold=false)
Colorize.colorize("36m", msg, bold)
end
def white(msg, bold=false)
Colorize.colorize("37m", msg, bold)
end
end
module Background
module_function
def black(msg, bold=false)
Colorize.colorize("40m", msg, bold)
end
def red(msg, bold=false)
Colorize.colorize("41m", msg, bold)
end
def green(msg, bold=false)
Colorize.colorize("42m", msg, bold)
end
def yellow(msg, bold=false)
Colorize.colorize("43m", msg, bold)
end
def blue(msg, bold=false)
Colorize.colorize("44m", msg, bold)
end
def magenta(msg, bold=false)
Colorize.colorize("45m", msg, bold)
end
def cyan(msg, bold=false)
Colorize.colorize("46m", msg, bold)
end
def white(msg, bold=false)
Colorize.colorize("47m", msg, bold)
end
end
require 'English'
def highlight_pattern(c_text, pattern)
c_text = c_text.gsub(Regexp.new(pattern)) do
prematch = $PREMATCH #$`
term = $MATCH # $&
postmatch = $POSTMATCH # $'
start = Colorize.escape_codes(prematch).first
reset = Colorize.escape_codes(postmatch).first
highlighted_term = Colorize::Background.cyan(term)
if start and reset
reset + highlighted_term + start
else
highlighted_term
end
end
end
end
#!/bin/bash
# from http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html
#
# This file echoes a bunch of color codes to the
# terminal to demonstrate what's available. Each
# line is the color code of one forground color,
# out of 17 (default + 16 escapes), followed by a
# test use of that color on all nine background
# colors (default + 8 escapes).
#
T='gYw' # The test text
echo -e "\n 40m 41m 42m 43m\
44m 45m 46m 47m";
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
'1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
' 36m' '1;36m' ' 37m' '1;37m';
do FG=${FGs// /}
echo -en " $FGs \033[$FG $T "
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
done
echo;
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.