Skip to content

Commit

Permalink
String highlighting improvements
Browse files Browse the repository at this point in the history
- IMPROVED: Better way to insert a colored string into a another string and return the
the previous color at the end of the inserted string. Whew, that was a
weird way to say it.
- FIXED: `doing view` not respecting search.highlight config
  • Loading branch information
ttscoff committed Jul 1, 2022
1 parent 7f0af45 commit 7f27109
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _If you're one of the rare people like me who find this useful, feel free to

<!--README-->

The current version of `doing` is <!--VER-->2.1.60<!--END VER-->.
The current version of `doing` is <!--VER-->2.1.61<!--END VER-->.

Find all of the documentation in the [doing wiki][wiki].

Expand Down
2 changes: 1 addition & 1 deletion bin/commands/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 53 additions & 0 deletions lib/doing/colors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions lib/doing/string/highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/doing/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Doing
VERSION = '2.1.61'
VERSION = '2.1.62'
end

0 comments on commit 7f27109

Please sign in to comment.