Skip to content

Commit

Permalink
Change Text filter to highlight the substring that matches the Regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Jun 15, 2023
1 parent c1ca81b commit 61e21e0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
5 changes: 5 additions & 0 deletions mrblib/core_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class String
def red
"\e[31m#{self}\e[0m"
end
end
10 changes: 8 additions & 2 deletions mrblib/rf/00filter/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ def gets

def decorate(val)
case val
when MatchData, true
when true
record
when Regexp
decorate(val.match(record))
return unless m = val.match(record)

[
m.pre_match,
m.to_s.red,
m.post_match
].join
when false, nil
nil
else
Expand Down
18 changes: 16 additions & 2 deletions spec/filter/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
describe 'Output only the lines that match the regexp' do
let(:output) do
<<~OUTPUT
1 foo
4 foobar
1 \e[31mfoo\e[0m
4 \e[31mfoo\e[0mbar
OUTPUT
end

Expand All @@ -59,6 +59,20 @@
it { expect(last_command_started).to have_output_on_stdout output_string_eq output }
end

describe 'Output only the substring that matches the regexp' do
let(:output) do
<<~OUTPUT
foo
foo
OUTPUT
end

before { run_rf('match(/foo/)', input) }

it { expect(last_command_started).to be_successfully_executed }
it { expect(last_command_started).to have_output_on_stdout output_string_eq output }
end

describe 'Output the sum of all the values in the first column' do
let(:output) do
<<~OUTPUT
Expand Down
1 change: 1 addition & 0 deletions spec/support/aruba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# need custom working directory to avoia conflict with parallel tests
working_directory = File.join('tmp/aruba', ENV['TEST_ENV_NUMBER'] || '1')
config.working_directory = working_directory
config.remove_ansi_escape_sequences = false
end

def rf_path
Expand Down

0 comments on commit 61e21e0

Please sign in to comment.