diff --git a/Gemfile.lock b/Gemfile.lock index b8878246..4dd5961f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - doing (2.1.61) + doing (2.1.62) chronic (~> 0.10, >= 0.10.2) deep_merge (~> 1.2, >= 1.2.1) gli (~> 2.20, >= 2.20.1) diff --git a/README.md b/README.md index 06150800..2f91bf68 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ _If you're one of the rare people like me who find this useful, feel free to -The current version of `doing` is 2.1.60. +The current version of `doing` is 2.1.61. Find all of the documentation in the [doing wiki][wiki]. diff --git a/bin/commands/view.rb b/bin/commands/view.rb index 9b24565a..30f4e15d 100644 --- a/bin/commands/view.rb +++ b/bin/commands/view.rb @@ -32,7 +32,7 @@ c.switch [:color], negatable: true c.desc "Highlight search matches in output. Only affects command line output" - c.switch %i[h hilite], default_value: false, negatable: true + c.switch %i[h hilite], default_value: Doing.settings.dig('search', 'highlight'), negatable: true c.desc 'Sort tags by (name|time)' c.arg_name 'KEY' diff --git a/lib/doing/colors.rb b/lib/doing/colors.rb index 2e5ffde7..d382490b 100644 --- a/lib/doing/colors.rb +++ b/lib/doing/colors.rb @@ -4,6 +4,7 @@ module Doing # Terminal output color functions. module Color + ESCAPE_REGEX = /(?<=\[)(?:(?:(?:[349]|10)[0-9]|[0-9])?;?)+(?=m)/.freeze # All available color names. Available as methods and string extensions. # # @example Use a color as a method. Color reset will be added to end of string. @@ -125,6 +126,58 @@ def validate_color def normalize_color gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg') end + + # Get the final color setting of the string + def last_color_code + m = scan(ESCAPE_REGEX) + + em = ['0'] + fg = nil + bg = nil + rgbf = nil + rgbb = nil + + m.each do |c| + case c + when '0' + em = ['0'] + fg, bg, rgbf, rgbb = nil + when /^[34]8/ + case c + when /^3/ + fg = nil + rgbf = c + when /^4/ + bg = nil + rgbb = c + end + else + c.split(/;/).each do |i| + x = i.to_i + if x <= 9 + em << x + elsif x >= 30 && x <= 39 + rgbf = nil + fg = x + elsif x >= 40 && x <= 49 + rgbb = nil + bg = x + elsif x >= 90 && x <= 97 + rgbf = nil + fg = x + elsif x >= 100 && x <= 107 + rgbb = nil + bg = x + end + end + end + end + + escape = "\e[#{em.join(';')}m" + escape += "\e[#{rgbb}m" if rgbb + escape += "\e[#{rgbf}m" if rgbf + escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m" + end end class << self diff --git a/lib/doing/string/highlight.rb b/lib/doing/string/highlight.rb index 4a5a5f55..3bbff6c1 100644 --- a/lib/doing/string/highlight.rb +++ b/lib/doing/string/highlight.rb @@ -17,14 +17,9 @@ def highlight_tags!(color = 'yellow', last_color: nil) ## def highlight_tags(color = 'yellow', last_color: nil) unless last_color - escapes = scan(/(\e\[[\d;]+m)[^\e]+@/) color = color.split(' ') unless color.is_a?(Array) tag_color = color.each_with_object([]) { |c, arr| arr << Doing::Color.send(c) }.join('') - last_color = if escapes.good? - (escapes.count > 1 ? escapes[-2..-1] : [escapes[-1]]).map { |v| v[0] }.join('') - else - Doing::Color.default - end + last_color = last_color_code end gsub(/(\s|m)(@[^ ("']+)/, "\\1#{tag_color}\\2#{last_color}") end @@ -54,7 +49,11 @@ def highlight_search(search, distance: nil, negate: false, case_type: nil) qs.concat(query[:should]) if query[:should] qs.each do |s| rx = Regexp.new(s.wildcard_to_rx, ignore_case(s, case_type)) - out.gsub!(rx) { |m| m.bgyellow.black } + out.gsub!(rx) do + m = Regexp.last_match + last = m.pre_match.last_color_code + "#{m[0].bgyellow.black}#{last}" + end end end out diff --git a/lib/doing/version.rb b/lib/doing/version.rb index dd31b164..30ee951e 100644 --- a/lib/doing/version.rb +++ b/lib/doing/version.rb @@ -1,3 +1,3 @@ module Doing - VERSION = '2.1.61' + VERSION = '2.1.62' end