Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Text filter to highlight the substring that matches the Regexp #46

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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