Merge pull request #502 from goar5670/short-report

This commit is contained in:
Claudio Bley 2022-03-19 09:12:48 +01:00 committed by GitHub
commit 1f2c7ee996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 18 deletions

View file

@ -77,7 +77,14 @@ module ColorLS
ls ls
end end
def display_report def display_report(report_mode)
if report_mode == :short
puts <<~REPORT
\s\s\s\sFolders: #{@count[:folders]}, Files: #{@count[:recognized_files] + @count[:unrecognized_files]}.
REPORT
.colorize(@colors[:report])
else
puts <<~REPORT puts <<~REPORT
Found #{@count.values.sum} items in total. Found #{@count.values.sum} items in total.
@ -88,6 +95,7 @@ module ColorLS
REPORT REPORT
.colorize(@colors[:report]) .colorize(@colors[:report])
end end
end
private private

View file

@ -11,7 +11,7 @@ module ColorLS
@light_colors = false @light_colors = false
@opts = default_opts @opts = default_opts
@show_report = false @report_mode = false
@exit_status_code = 0 @exit_status_code = 0
parse_options parse_options
@ -88,7 +88,7 @@ module ColorLS
$stderr.puts "#{dir}: #{e}".colorize(:red) $stderr.puts "#{dir}: #{e}".colorize(:red)
end end
core.display_report if @show_report core.display_report(@report_mode) if @report_mode
@exit_status_code @exit_status_code
end end
@ -142,8 +142,11 @@ module ColorLS
options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs } options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs }
options.on('-f', '--files', 'show only files') { @opts[:show] = :files } 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('--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('-p', 'append / indicator to directories') { @opts[:indicator_style] = 'slash' }
options.on('--report=[WORD]', %w[short long], 'show report: short, long (default if omitted)') do |word|
word ||= :long
@report_mode = word.to_sym
end
options.on( options.on(
'--indicator-style=[STYLE]', '--indicator-style=[STYLE]',
%w[none slash], 'append indicator with style STYLE to entry names: none, slash (-p) (default)' %w[none slash], 'append indicator with style STYLE to entry names: none, slash (-p) (default)'

View file

@ -21,9 +21,12 @@ RSpec.describe ColorLS::Flags do
} }
it('does not display hidden files') { expect { subject }.not_to output(/\.hidden-file/).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('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 it 'displays multiple files per line' do
allow($stdout).to receive(:tty?).and_return(true) allow($stdout).to receive(:tty?).and_return(true)
@ -352,11 +355,19 @@ RSpec.describe ColorLS::Flags do
end end
end end
context 'with unrecognized files' do context 'with --report flag' do
let(:args) { ['--report', FIXTURES] } let(:args) { ['--report', '--report=long', FIXTURES] }
it 'shows a report with unrecognized files' do it 'shows a report with recognized and unrecognized files' do
expect { subject }.to output(/Unrecognized files\s+: 3/).to_stdout 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
end end

View file

@ -34,6 +34,9 @@ OK colorls --color=never
OK colorls --color=always OK colorls --color=always
OK colorls --tree spec OK colorls --tree spec
OK colorls --tree=1 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 spec/fixtures/
LC_ALL=C OK colorls --git spec/fixtures/ LC_ALL=C OK colorls --git spec/fixtures/