From c4f3885410e22ed16c7ca7d5f9a94dfafac3958c Mon Sep 17 00:00:00 2001 From: bl7awy Date: Fri, 18 Mar 2022 18:02:46 +0300 Subject: [PATCH 1/4] Add the option for a short report --- lib/colorls/core.rb | 24 ++++++++++++++++-------- lib/colorls/flags.rb | 9 ++++++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 0fecb2c8..49673bc2 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -77,16 +77,24 @@ def ls_files(files) ls end - def display_report - puts <<~REPORT + def display_report(report_mode) + if report_mode == :short + puts <<~REPORT - Found #{@count.values.sum} items in total. + \s\s\s\sFolders: #{@count[:folders]}, Files: #{@count[:recognized_files] + @count[:unrecognized_files]}. + REPORT + .colorize(@colors[:report]) + else + puts <<~REPORT + + Found #{@count.values.sum} items in total. - \tFolders\t\t\t: #{@count[:folders]} - \tRecognized files\t: #{@count[:recognized_files]} - \tUnrecognized files\t: #{@count[:unrecognized_files]} - REPORT - .colorize(@colors[:report]) + \tFolders\t\t\t: #{@count[:folders]} + \tRecognized files\t: #{@count[:recognized_files]} + \tUnrecognized files\t: #{@count[:unrecognized_files]} + REPORT + .colorize(@colors[:report]) + end end private diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index ea93855d..ad223221 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -11,7 +11,7 @@ def initialize(*args) @light_colors = false @opts = default_opts - @show_report = false + @report_mode = false @exit_status_code = 0 parse_options @@ -88,7 +88,7 @@ def process_args $stderr.puts "#{dir}: #{e}".colorize(:red) end - core.display_report if @show_report + core.display_report(@report_mode) if @report_mode @exit_status_code end @@ -142,8 +142,11 @@ def add_common_options(options) options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs } options.on('-f', '--files', 'show only files') { @opts[:show] = :files } options.on('--gs', '--git-status', 'show git status for each file') { @opts[:git_status] = true } - options.on('--report', 'show brief report') { @show_report = true } options.on('-p', 'append / indicator to directories') { @opts[:indicator_style] = 'slash' } + options.on('--report=[WORD]', %w[short long], 'show report') do |word| + word ||= :long + @report_mode = word.to_sym + end options.on( '--indicator-style=[STYLE]', %w[none slash], 'append indicator with style STYLE to entry names: none, slash (-p) (default)' From f43c9b9e7e239b90a604130d63c26a83b78f2c0e Mon Sep 17 00:00:00 2001 From: bl7awy Date: Fri, 18 Mar 2022 22:28:26 +0300 Subject: [PATCH 2/4] Add tests for --report flag --- spec/color_ls/flags_spec.rb | 22 +++++++++++++++------- test/checks | 3 +++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index d45efd29..6c03082b 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -20,9 +20,9 @@ end.not_to output(/((r|-).*(w|-).*(x|-).*){3}/).to_stdout } - it('does not display hidden files') { expect { subject }.not_to output(/\.hidden-file/).to_stdout } - it('does not show a report') { expect { subject }.not_to output(/Found \d+ contents/).to_stdout } - it('displays dirs & files alphabetically') { expect { subject }.to output(/a-file.+symlinks.+z-file/m).to_stdout } + it('does not display hidden files') { expect { subject }.not_to output(/\.hidden-file/).to_stdout } + it('does not show a report') { expect { subject }.not_to output(/(Found \d+ items in total\.)|(Folders: \d+, Files: \d+\.)/).to_stdout } + it('displays dirs & files alphabetically') { expect { subject }.to output(/a-file.+symlinks.+z-file/m).to_stdout } it 'displays multiple files per line' do allow($stdout).to receive(:tty?).and_return(true) @@ -352,11 +352,19 @@ end end - context 'with unrecognized files' do - let(:args) { ['--report', FIXTURES] } + context 'with --report flag' do + let(:args) { ['--report', '--report=long', FIXTURES] } - it 'shows a report with unrecognized files' do - expect { subject }.to output(/Unrecognized files\s+: 3/).to_stdout + it 'shows a report with recognized and unrecognized files' do + expect { subject }.to output(/Recognized files\s+: 3\n.+Unrecognized files\s+: 3/).to_stdout + end + end + + context 'with --report=short flag' do + let(:args) { ['--report=short', FIXTURES] } + + it 'shows a brief report' do + expect { subject }.to output(/Folders: \d+, Files: \d+\./).to_stdout end end diff --git a/test/checks b/test/checks index 66338905..0714f5a8 100644 --- a/test/checks +++ b/test/checks @@ -34,6 +34,9 @@ OK colorls --color=never OK colorls --color=always OK colorls --tree spec OK colorls --tree=1 +OK colorls --report +OK colorls --report=long +OK colorls --report=short LC_ALL=C OK colorls spec/fixtures/ LC_ALL=C OK colorls --git spec/fixtures/ From 29f2162a779c81e587c89e6ba325d17a9be4e611 Mon Sep 17 00:00:00 2001 From: bl7awy Date: Fri, 18 Mar 2022 22:59:27 +0300 Subject: [PATCH 3/4] Edit --report flag description --- lib/colorls/flags.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index ad223221..2a64973b 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -143,7 +143,7 @@ def add_common_options(options) options.on('-f', '--files', 'show only files') { @opts[:show] = :files } options.on('--gs', '--git-status', 'show git status for each file') { @opts[:git_status] = true } options.on('-p', 'append / indicator to directories') { @opts[:indicator_style] = 'slash' } - options.on('--report=[WORD]', %w[short long], 'show report') do |word| + options.on('--report=[WORD]', %w[short long], 'show report: short, long (default if omitted)') do |word| word ||= :long @report_mode = word.to_sym end From e9166e8c15fc0f95c78afbd741d19dd7a05f1e26 Mon Sep 17 00:00:00 2001 From: bl7awy Date: Fri, 18 Mar 2022 23:41:36 +0300 Subject: [PATCH 4/4] Resolve long line offense --- spec/color_ls/flags_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index 6c03082b..99d76f7a 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -21,9 +21,12 @@ } it('does not display hidden files') { expect { subject }.not_to output(/\.hidden-file/).to_stdout } - it('does not show a report') { expect { subject }.not_to output(/(Found \d+ items in total\.)|(Folders: \d+, Files: \d+\.)/).to_stdout } it('displays dirs & files alphabetically') { expect { subject }.to output(/a-file.+symlinks.+z-file/m).to_stdout } + it 'does not show a report' do + expect { subject }.not_to output(/(Found \d+ items in total\.)|(Folders: \d+, Files: \d+\.)/).to_stdout + end + it 'displays multiple files per line' do allow($stdout).to receive(:tty?).and_return(true)