diff --git a/mrblib/core_ext.rb b/mrblib/core_ext.rb new file mode 100644 index 0000000..792bc58 --- /dev/null +++ b/mrblib/core_ext.rb @@ -0,0 +1,5 @@ +class String + def red + "\e[31m#{self}\e[0m" + end +end diff --git a/mrblib/rf/00filter/text.rb b/mrblib/rf/00filter/text.rb index 0ea15fb..2149e6f 100644 --- a/mrblib/rf/00filter/text.rb +++ b/mrblib/rf/00filter/text.rb @@ -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 diff --git a/spec/filter/text_spec.rb b/spec/filter/text_spec.rb index c4f1fb7..afd9d47 100644 --- a/spec/filter/text_spec.rb +++ b/spec/filter/text_spec.rb @@ -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 @@ -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 diff --git a/spec/support/aruba.rb b/spec/support/aruba.rb index 9332fb5..dfd9810 100644 --- a/spec/support/aruba.rb +++ b/spec/support/aruba.rb @@ -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