#! /usr/bin/env ruby # frozen_string_literal: true # this adds ascii colorization to String class class String CLS = "\033[0;0f\033\[2J" @color_codes = { black: 0, light_black: 60, red: 1, light_red: 61, green: 2, light_green: 62, yellow: 3, light_yellow: 63, blue: 4, light_blue: 64, magenta: 5, light_magenta: 65, cyan: 6, light_cyan: 66, white: 7, light_white: 67, default: 9 } @color_codes.default = 9 @color_modes = { default: 0, # Turn off all attributes bold: 1, dim: 2, italic: 3, underline: 4, blink: 5, swap: 7, # Exchange foreground and background colors hide: 8 # Hide text (foreground color would be the same as background) } @color_modes.default = 0 @syms = %i[fg bg mode] class << self attr_reader :color_codes, :color_modes, :syms def create_methods color_codes.each_key do |cc| next if cc == :default define_method(cc) { colorize(fg: cc) } define_method("on_#{cc}") { colorize(bg: cc) } end color_modes.each_key do |cc| next if cc == :default define_method(cc) { colorize(mode: cc) } end end end create_methods def colorize(opts) # code = compile_code(opts) code = opts.each_with_object([]) { |(k, v), a| a << resolve(k, v) if self.class.syms.include? k }.join(';') return self if code.empty? apply_code(code) end private RESET = "\033[0m" START_CODE = /^\033\[([0-9;]+)m/.freeze # negative lookbehind : (? is not at the start of the line # negative lookahead : (?! ) + $ => is not at the end of the line MIDDLE_RESET = /(?> #{a} #{b} << DER".white.on_red.underline end