module TerminalUtils::Color

This is a refinement on String that adds some properties to the String that represent ANSI Escape Sequences

Examples

# Set the String foreground color property to red 'hello'.fg_red.tu_color_prefix

> “e[31m”

'hello'.fg_red.tu_color_suffix

> “e[0m”

# Set the background color property to red 'hello'.bg_red.tu_color_prefix

> “e[41m”

'hello'.bg_red.tu_color_suffix

> “e[0m”

# Set the foreground to red and the background to black 'hello'.fg_red.bg_black.tu_color_prefix

> “e[31me[40m”

'hello'.fg_red.bg_black.tu_color_suffix

> “e[0m”

Public Instance Methods

attr_bold(ansi_code) click to toggle source
# File lib/terminal_utils.rb, line 82
def attr_bold(ansi_code)
        @attr_bold = ansi_code

        return color_string
end
attr_invisible(ansi_code) click to toggle source
# File lib/terminal_utils.rb, line 76
def attr_invisible(ansi_code)
        @attr_invisible = ansi_code

        return color_string
end
attr_underline(ansi_code) click to toggle source
# File lib/terminal_utils.rb, line 88
def attr_underline(ansi_code)
        @attr_underline = ansi_code

        return color_string
end
bg_black() click to toggle source
# File lib/terminal_utils.rb, line 51
def bg_black;          return bg_color("\e[40m");      end
bg_blue() click to toggle source
# File lib/terminal_utils.rb, line 55
def bg_blue;           return bg_color("\e[44m");      end
bg_bright_black() click to toggle source
# File lib/terminal_utils.rb, line 59
def bg_bright_black;   return bg_color("\e[100m");     end
bg_bright_blue() click to toggle source
# File lib/terminal_utils.rb, line 63
def bg_bright_blue;    return bg_color("\e[104m");     end
bg_bright_cyan() click to toggle source
# File lib/terminal_utils.rb, line 65
def bg_bright_cyan;    return bg_color("\e[106m");     end
bg_bright_green() click to toggle source
# File lib/terminal_utils.rb, line 61
def bg_bright_green;   return bg_color("\e[102m");     end
bg_bright_grey() click to toggle source
# File lib/terminal_utils.rb, line 66
def bg_bright_grey;    return bg_color("\e[107m");     end
bg_bright_magenta() click to toggle source
# File lib/terminal_utils.rb, line 64
def bg_bright_magenta; return bg_color("\e[105m");     end
bg_bright_red() click to toggle source
# File lib/terminal_utils.rb, line 60
def bg_bright_red;     return bg_color("\e[101m");     end
bg_bright_yellow() click to toggle source
# File lib/terminal_utils.rb, line 62
def bg_bright_yellow;  return bg_color("\e[103m");     end
bg_color(ansi_code) click to toggle source
# File lib/terminal_utils.rb, line 106
def bg_color(ansi_code)
        @tu_bg_color = ansi_code

        return color_string
end
bg_cyan() click to toggle source
# File lib/terminal_utils.rb, line 57
def bg_cyan;           return bg_color("\e[46m");      end
bg_default() click to toggle source
# File lib/terminal_utils.rb, line 50
def bg_default;        return fg_color("\e[49m");      end
bg_green() click to toggle source
# File lib/terminal_utils.rb, line 53
def bg_green;          return bg_color("\e[42m");      end
bg_grey() click to toggle source
# File lib/terminal_utils.rb, line 58
def bg_grey;           return bg_color("\e[47m");      end
bg_magenta() click to toggle source
# File lib/terminal_utils.rb, line 56
def bg_magenta;        return bg_color("\e[45m");      end
bg_red() click to toggle source
# File lib/terminal_utils.rb, line 52
def bg_red;            return bg_color("\e[41m");      end
bg_yellow() click to toggle source
# File lib/terminal_utils.rb, line 54
def bg_yellow;         return bg_color("\e[43m");      end
bold() click to toggle source
# File lib/terminal_utils.rb, line 69
def bold;              return attr_bold("\e[1m");      end
color_string() click to toggle source
# File lib/terminal_utils.rb, line 112
def color_string
        @tu_color_prefix = [@tu_fg_color, @tu_bg_color, @attr_invisible, @attr_bold, @attr_underline, @attr_blink].compact.join('')
        @tu_color_suffix = "\e[0m"

        return self
end
fg_black() click to toggle source
# File lib/terminal_utils.rb, line 33
def fg_black;          return fg_color("\e[30m");      end
fg_blue() click to toggle source
# File lib/terminal_utils.rb, line 37
def fg_blue;           return fg_color("\e[34m");      end
fg_bright_black() click to toggle source
# File lib/terminal_utils.rb, line 41
def fg_bright_black;   return fg_color("\e[90m");      end
fg_bright_blue() click to toggle source
# File lib/terminal_utils.rb, line 45
def fg_bright_blue;    return fg_color("\e[94m");      end
fg_bright_cyan() click to toggle source
# File lib/terminal_utils.rb, line 47
def fg_bright_cyan;    return fg_color("\e[96m");      end
fg_bright_green() click to toggle source
# File lib/terminal_utils.rb, line 43
def fg_bright_green;   return fg_color("\e[92m");      end
fg_bright_grey() click to toggle source
# File lib/terminal_utils.rb, line 48
def fg_bright_grey;    return fg_color("\e[97m");      end
fg_bright_magenta() click to toggle source
# File lib/terminal_utils.rb, line 46
def fg_bright_magenta; return fg_color("\e[95m");      end
fg_bright_red() click to toggle source
# File lib/terminal_utils.rb, line 42
def fg_bright_red;     return fg_color("\e[91m");      end
fg_bright_yellow() click to toggle source
# File lib/terminal_utils.rb, line 44
def fg_bright_yellow;  return fg_color("\e[93m");      end
fg_color(ansi_code) click to toggle source
# File lib/terminal_utils.rb, line 100
def fg_color(ansi_code)
        @tu_fg_color = ansi_code

        return color_string
end
fg_cyan() click to toggle source
# File lib/terminal_utils.rb, line 39
def fg_cyan;           return fg_color("\e[36m");      end
fg_default() click to toggle source
# File lib/terminal_utils.rb, line 32
def fg_default;        return fg_color("\e[39m");      end
fg_green() click to toggle source
# File lib/terminal_utils.rb, line 35
def fg_green;          return fg_color("\e[32m");      end
fg_grey() click to toggle source
# File lib/terminal_utils.rb, line 40
def fg_grey;           return fg_color("\e[37m");      end
fg_magenta() click to toggle source
# File lib/terminal_utils.rb, line 38
def fg_magenta;        return fg_color("\e[35m");      end
fg_red() click to toggle source
# File lib/terminal_utils.rb, line 34
def fg_red;            return fg_color("\e[31m");      end
fg_yellow() click to toggle source
# File lib/terminal_utils.rb, line 36
def fg_yellow;         return fg_color("\e[33m");      end
invisible() click to toggle source
# File lib/terminal_utils.rb, line 68
def invisible;         return attr_invisible("\e[8m"); end
underline() click to toggle source
# File lib/terminal_utils.rb, line 70
def underline;         return attr_underline("\e[4m"); end