Fix search command for single characters

This commit is contained in:
Alan Foster
2021-03-22 18:52:57 +00:00
parent 2126caf6a7
commit d63ff8a559
2 changed files with 12 additions and 12 deletions
@@ -5,25 +5,18 @@ module Msf
module Console
module TablePrint
class HighlightSubstringStyler
COLOR = '%bgmag'
HIGHLIGHT_COLOR = '%bgmag'
RESET_COLOR = '%clr'
def initialize(substrings)
@substrings = substrings
end
def style(value)
value_cp = value.clone
search_terms = @substrings.map { |substring| Regexp.escape(substring) }
search_pattern = /#{search_terms.join('|')}/i
@substrings.each do |s|
# Regex used to pull out matches and preserve case sensitivity
matches = value_cp.scan(%r{#{Regexp.escape(s)}}i)
matches.each do |m|
value_cp.gsub!(m, COLOR + m + '%clr')
end
end
value_cp
value.gsub(search_pattern) { |match| "#{HIGHLIGHT_COLOR}#{match}#{RESET_COLOR}" }
end
end
end
@@ -23,5 +23,12 @@ RSpec.describe Msf::Ui::Console::TablePrint::HighlightSubstringStyler do
expect(styler.style(str)).to eql "AAAAA%bgmagBBB%clrAAAAA%bgmagCCC%clr"
end
it 'should highlight single characters' do
str = 'ABCABC'
styler = described_class.new(%w(a b c))
expect(styler.style(str)).to eql "%bgmagA%clr%bgmagB%clr%bgmagC%clr%bgmagA%clr%bgmagB%clr%bgmagC%clr"
end
end
end