From 02f2838f5d90e4fa6a5ac1b79b1d2230741fbee5 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sun, 19 Nov 2023 20:40:04 +0900 Subject: [PATCH] Add --with-record-number option --- mrblib/rf/config.rb | 5 ++- mrblib/rf/container.rb | 7 ++-- mrblib/rf/runner.rb | 8 +++-- spec/filter/json_spec.rb | 22 ++++++------ spec/filter/text_spec.rb | 32 ++++++++--------- spec/filter/yaml_spec.rb | 22 ++++++------ spec/global_options/recursive_option_spec.rb | 4 +-- .../with_record_number_option_spec.rb | 36 +++++++++++++++++++ 8 files changed, 86 insertions(+), 50 deletions(-) create mode 100644 spec/global_options/with_record_number_option_spec.rb diff --git a/mrblib/rf/config.rb b/mrblib/rf/config.rb index a86dbee..23b5a44 100644 --- a/mrblib/rf/config.rb +++ b/mrblib/rf/config.rb @@ -1,7 +1,7 @@ module Rf class Config attr_accessor :command, :files, :filter, :grep_mode, :include_filename, :in_place, - :slurp, :script_file, :quiet, :recursive, :with_filename + :slurp, :script_file, :quiet, :recursive, :with_filename, :with_record_number def self.parse(argv) Parser.new.parse(argv) @@ -99,6 +99,9 @@ def banner_and_filter_options def global_options # rubocop:disable Metrics/AbcSize @global_options ||= OptionMap.new do |opt| opt.on('-H', '--with-filename', 'print filename with output lines') { @config.with_filename = true } + opt.on('--with-record-number', 'print record number with output lines') do + @config.with_record_number = true + end opt.on('-R', '--recursive', 'read all files under each directory recursively') { @config.recursive = true } opt.on('--include-filename', 'searches for files matching a regex pattern') do |p| @config.include_filename = p diff --git a/mrblib/rf/container.rb b/mrblib/rf/container.rb index d3320c9..9bba06b 100644 --- a/mrblib/rf/container.rb +++ b/mrblib/rf/container.rb @@ -2,11 +2,12 @@ module Rf class Container - attr_accessor :filename, :with_filename + attr_accessor :filename def initialize(opts = {}) @filenem = opts[:filename] @with_filename = opts[:with_filename] || false + @with_record_number = opts[:with_record_number] || false end def _ @@ -37,7 +38,9 @@ def hash? end def puts(*) - $output.write("#{filename}: ") if with_filename && filename + $output.write("#{filename}:") if @with_filename && filename + $output.write("#{NR}:") if @with_record_number + $output.puts(*) end diff --git a/mrblib/rf/runner.rb b/mrblib/rf/runner.rb index b7bb7e6..7e4324e 100644 --- a/mrblib/rf/runner.rb +++ b/mrblib/rf/runner.rb @@ -8,7 +8,7 @@ def self.run(...) %w[ command filter files grep_mode in_place include_filename - slurp? quiet? recursive? + slurp? quiet? recursive? with_record_number? ].each do |name| n = name.delete_suffix('?') define_method(name.to_sym) { config.send(n) } @@ -34,9 +34,11 @@ def initialize(cfg) # enclose the scope of binding def setup_container - @container = Container.new + @container = Container.new({ + with_filename: with_filename?, + with_record_number: with_record_number? + }) @bind = container.instance_eval { binding } - @container.with_filename = with_filename? end def run # rubocop:disable Metrics/AbcSize diff --git a/spec/filter/json_spec.rb b/spec/filter/json_spec.rb index a60d554..ba279e0 100644 --- a/spec/filter/json_spec.rb +++ b/spec/filter/json_spec.rb @@ -53,17 +53,16 @@ context 'when use -H option' do let(:input) { '"foobar"' } - let(:output) do - input.split("\n").map { |line| "testfile: #{line}" }.join("\n") + let(:args) { '-j -H true testfile' } + let(:expect_output) do + input.split("\n").map { |line| "testfile:#{line}" }.join("\n") end before do write_file 'testfile', input - run_rf('-j -H true testfile') end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output_string_eq output } + it_behaves_like 'a successful exec' end context 'with -R option' do @@ -113,30 +112,29 @@ { 'without -H option' => { option: '', - output: <<~OUTPUT + expect_output: <<~OUTPUT "foobar" "foobar" OUTPUT }, 'with -H option' => { option: '-H', - output: <<~OUTPUT - testfile1: "foobar" - testfile2: "foobar" + expect_output: <<~OUTPUT + testfile1:"foobar" + testfile2:"foobar" OUTPUT } } end with_them do + let(:args) { "-j #{option} true testfile1 testfile2" } before do write_file 'testfile1', input write_file 'testfile2', input - run_rf("-j #{option} 'true' testfile1 testfile2") end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } + it_behaves_like 'a successful exec' end end diff --git a/spec/filter/text_spec.rb b/spec/filter/text_spec.rb index 9f7f43a..a93da48 100644 --- a/spec/filter/text_spec.rb +++ b/spec/filter/text_spec.rb @@ -34,24 +34,24 @@ end context 'when use -H option' do - let(:output) do - input.split("\n").map { |line| "testfile: #{line}" }.join("\n") + let(:args) { '-H true testfile' } + let(:expect_output) do + input.split("\n").map { |line| "testfile:#{line}" }.join("\n") end before do write_file 'testfile', input - run_rf('-H true testfile') end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output_string_eq output } + it_behaves_like 'a successful exec' end context 'with -R option' do - let(:output) do + let(:args) { '-R _ .' } + let(:expect_output) do <<~OUTPUT - ./a/b/c: abc - ./foo/bar: foobar + ./a/b/c:abc + ./foo/bar:foobar OUTPUT end @@ -60,12 +60,9 @@ write_file('a/b/c', 'abc') FileUtils.mkdir_p(expand_path('foo')) write_file('foo/bar', 'foobar') - - run_rf('-R _ .') end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } + it_behaves_like 'a successful exec' end context 'with -g option' do @@ -85,21 +82,20 @@ end context 'when multiple files' do - let(:output) do + let(:args) { 'true testfile1 testfile2' } + let(:expect_output) do [ - input.split("\n").map { |line| "testfile1: #{line}" }.join("\n"), - input.split("\n").map { |line| "testfile2: #{line}" }.join("\n") + input.split("\n").map { |line| "testfile1:#{line}" }.join("\n"), + input.split("\n").map { |line| "testfile2:#{line}" }.join("\n") ].join("\n") end before do write_file 'testfile1', input write_file 'testfile2', input - run_rf('true testfile1 testfile2') end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output_string_eq output } + it_behaves_like 'a successful exec' end context 'when input from stdin' do diff --git a/spec/filter/yaml_spec.rb b/spec/filter/yaml_spec.rb index 5f6cdbe..00c40f9 100644 --- a/spec/filter/yaml_spec.rb +++ b/spec/filter/yaml_spec.rb @@ -278,17 +278,16 @@ context 'when use -H option' do let(:input) { 'foobar' } - let(:output) do - input.split("\n").map { |line| "testfile: #{line}" }.join("\n") + let(:args) { '-y -H true testfile' } + let(:expect_output) do + input.split("\n").map { |line| "testfile:#{line}" }.join("\n") end before do write_file 'testfile', input - run_rf('-y -H true testfile') end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output_on_stdout output_string_eq output } + it_behaves_like 'a successful exec' end context 'when multiple files' do @@ -298,30 +297,29 @@ { 'without -H option' => { option: '', - output: <<~OUTPUT + expect_output: <<~OUTPUT foobar foobar OUTPUT }, 'with -H option' => { option: '-H', - output: <<~OUTPUT - testfile1: foobar - testfile2: foobar + expect_output: <<~OUTPUT + testfile1:foobar + testfile2:foobar OUTPUT } } end with_them do + let(:args) { "-y #{option} true testfile1 testfile2" } before do write_file 'testfile1', input write_file 'testfile2', input - run_rf("-y #{option} 'true' testfile1 testfile2") end - it { expect(last_command_started).to be_successfully_executed } - it { expect(last_command_started).to have_output output_string_eq output } + it_behaves_like 'a successful exec' end end diff --git a/spec/global_options/recursive_option_spec.rb b/spec/global_options/recursive_option_spec.rb index f44f8a8..da99e7f 100644 --- a/spec/global_options/recursive_option_spec.rb +++ b/spec/global_options/recursive_option_spec.rb @@ -7,8 +7,8 @@ let(:args) { "#{opts} _ ." } let(:expect_output) do <<~OUTPUT - ./a/b/c: abc - ./foo/bar: foobar + ./a/b/c:abc + ./foo/bar:foobar OUTPUT end diff --git a/spec/global_options/with_record_number_option_spec.rb b/spec/global_options/with_record_number_option_spec.rb new file mode 100644 index 0000000..9b89b5e --- /dev/null +++ b/spec/global_options/with_record_number_option_spec.rb @@ -0,0 +1,36 @@ +describe 'Behavior with --with-record-number option' do + let(:input) do + <<~INPUT + foo + bar + baz + INPUT + end + let(:args) { '--with-record-number _' } + let(:expect_output) do + <<~OUTPUT + 1:foo + 2:bar + 3:baz + OUTPUT + end + + it_behaves_like 'a successful exec' + + context 'with --with-file-name option' do + let(:args) { '--with-record-number --with-filename _ testfile' } + let(:expect_output) do + <<~OUTPUT + testfile:1:foo + testfile:2:bar + testfile:3:baz + OUTPUT + end + + before do + write_file 'testfile', input + end + + it_behaves_like 'a successful exec' + end +end