From 02db514d1518f62e2604e02ee43b25a3cbb3e68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 7 Sep 2023 18:50:04 +0200 Subject: ColorizedString : clean up --- lib/colonial_twilight/colorized_string.rb | 117 ++++++++++++++++-------------- 1 file changed, 62 insertions(+), 55 deletions(-) diff --git a/lib/colonial_twilight/colorized_string.rb b/lib/colonial_twilight/colorized_string.rb index 6f1d55b..640bcca 100644 --- a/lib/colonial_twilight/colorized_string.rb +++ b/lib/colonial_twilight/colorized_string.rb @@ -1,85 +1,92 @@ #! /usr/bin/env ruby -# -*- coding: UTF-8 -*- +# frozen_string_literal: true +# this adds ascii colorization to String class class String - - CLS="\033[0;0f\033\[2J".freeze - + 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 + 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_codes.default = 9 @color_modes = { - :default => 0, # Turn off all attributes - :bold => 1, # Set bold mode - :italic => 3, # Set italic mode - :underline => 4, # Set underline mode - :blink => 5, # Set blink mode - :swap => 7, # Exchange foreground and background colors - :hide => 8 # Hide text (foreground color would be the same as background) + default: 0, # Turn off all attributes + bold: 1, # Set bold mode + italic: 3, # Set italic mode + underline: 4, # Set underline mode + blink: 5, # Set blink mode + swap: 7, # Exchange foreground and background colors + hide: 8 # Hide text (foreground color would be the same as background) } - @color_modes.default=0 - @syms = [:fg, :bg, :mode] + @color_modes.default = 0 + @syms = %i[fg bg mode] class << self attr_reader :color_codes, :color_modes, :syms + def create_methods - color_codes.keys.each do |cc| + color_codes.each_key do |cc| next if cc == :default - define_method cc do colorize(:fg=>cc) end - define_method "on_#{cc}" do colorize(:bg=>cc) end + + define_method(cc) { colorize(fg: cc) } + define_method("on_#{cc}") { colorize(bg: cc) } end - color_modes.keys.each do |cc| + color_modes.each_key do |cc| next if cc == :default - define_method cc do colorize(:mode=>cc) end - end + define_method(cc) { colorize(mode: cc) } + end end end create_methods - START="\033[".freeze - RESET="\033[0m".freeze - START_RE=/^\033\[([0-9;]+)m/ - RESET_RE=/(? is not at the start of the line + # negative lookahead : (?! ) + $ => is not at the end of the line + MIDDLE_RESET = /(?> #{"blue".colorize(:fg=>:blue,:bg=>nil)} #{"green".colorize(:fg=>nil).on_green} << DER".white.on_red.underline + a = 'blue'.colorize(fg: :blue, bg: nil) + b = 'green'.colorize(fg: nil).on_green + puts "RED >> #{a} #{b} << DER".white.on_red.underline end -- cgit v1.1-2-g2b99