Skip to content

Commit

Permalink
Add --with-record-number option
Browse files Browse the repository at this point in the history
  • Loading branch information
buty4649 committed Nov 19, 2023
1 parent 25bad7c commit 02f2838
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 50 deletions.
5 changes: 4 additions & 1 deletion mrblib/rf/config.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions mrblib/rf/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions mrblib/rf/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions spec/filter/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
32 changes: 14 additions & 18 deletions spec/filter/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
22 changes: 10 additions & 12 deletions spec/filter/yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/global_options/recursive_option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
36 changes: 36 additions & 0 deletions spec/global_options/with_record_number_option_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 02f2838

Please sign in to comment.