diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2023-09-13 12:51:09 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2023-09-13 12:51:09 +0200 |
commit | 8d2dcd9113c5d970cb336bee0efe73e7cdccc92a (patch) | |
tree | 3150287208f14bd860c8d219a98bc75d56c0dd06 /lib/colonial_twilight/colorized_string.rb | |
parent | f10fbbdd656a435ac6b063f6ac54232cddf67694 (diff) | |
download | colonial-twilight-8d2dcd9113c5d970cb336bee0efe73e7cdccc92a.zip colonial-twilight-8d2dcd9113c5d970cb336bee0efe73e7cdccc92a.tar.gz |
ColorizedString : small improvement
Diffstat (limited to 'lib/colonial_twilight/colorized_string.rb')
-rw-r--r-- | lib/colonial_twilight/colorized_string.rb | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/colonial_twilight/colorized_string.rb b/lib/colonial_twilight/colorized_string.rb index 640bcca..db13bbd 100644 --- a/lib/colonial_twilight/colorized_string.rb +++ b/lib/colonial_twilight/colorized_string.rb @@ -18,12 +18,13 @@ class String @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) + 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] @@ -68,9 +69,18 @@ class String # replace all not ending reset with ascii code if self =~ START_CODE prev_start_code = ::Regexp.last_match(1) - code = "\033[#{prev_start_code};#{code}m" - s = sub(START_CODE, code).gsub(/(?<!^)\033\[#{prev_start_code}m(?!$)/, code) + # puts "merge : #{code} to #{prev_start_code}" + # merge in front to preserve previous formating + mcode = "\033[#{code};#{prev_start_code}m" + # replace starting code and middle prev code with merged one + # replace middle reset code with new code + # the latter does not work on multiple pass like ' '.red.on_blue + # reset middle code will become red, nothin will be done with blue + s = sub(START_CODE, mcode) + .gsub(MIDDLE_RESET, "\033[#{code}m") + .gsub(/(?<!^)\033\[#{prev_start_code}m(?!$)/, mcode) else + # puts "add : #{code}" code = "\033[#{code}m" s = (code + self).gsub(MIDDLE_RESET, code) end |