Skip to content

Commit

Permalink
Merge pull request #622 from ayushpoddar/fix/rounding-large-files
Browse files Browse the repository at this point in the history
Fix: rounding large files
  • Loading branch information
avdv authored Sep 5, 2024
2 parents e3292e0 + 6679c38 commit 9914606
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion colorls.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'diffy', '3.4.2'
spec.add_development_dependency 'rake', '~> 13'
spec.add_development_dependency 'rdoc', '~> 6.1'
spec.add_development_dependency 'ronn', '~> 0'
spec.add_development_dependency 'ronn-ng', '~> 0'
spec.add_development_dependency 'rspec', '~> 3.7'
spec.add_development_dependency 'rspec-its', '~> 1.2'
spec.add_development_dependency 'rubocop', '~> 1.50.1'
Expand Down
4 changes: 2 additions & 2 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def group_info(group)

def size_info(filesize)
filesize = Filesize.new(filesize)
size = @show_human_readable_size ? filesize.pretty : filesize.to_s
size = @show_human_readable_size ? filesize.pretty(precision: 0) : filesize.to_s('B', precision: 0)
size = size.split
size = justify_size_info(size)
return size.colorize(@colors[:file_large]) if filesize >= 512 * (1024 ** 2)
Expand All @@ -280,7 +280,7 @@ def chars_for_size
end

def justify_size_info(size)
size_num = size[0][0..-4].rjust(chars_for_size, ' ')
size_num = size[0].rjust(chars_for_size, ' ')
size_unit = @show_human_readable_size ? size[1].ljust(3, ' ') : size[1]
"#{size_num} #{size_unit}"
end
Expand Down
12 changes: 10 additions & 2 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
it 'displays multiple files per line' do
allow($stdout).to receive(:tty?).and_return(true)

expect { subject }.not_to output(/(.*\n){4}/).to_stdout
expect { subject }.not_to output(/(.*\n){7}/).to_stdout
end

it('does not display ./ or ../') { expect { subject }.not_to output(%r(\.{1,2}/)).to_stdout }
Expand Down Expand Up @@ -103,6 +103,14 @@
let(:args) { ['--long', FIXTURES] }

it('lists file info') { expect { subject }.to output(/((r|-).*(w|-).*(x|-).*){3}/).to_stdout }

it 'rounds up file size to nearest MiB' do
expect { subject }.to output(/2 MiB[^\n]* 20kb-less-than-2mb\.txt/).to_stdout
end

it 'rounds down file size to nearest MiB' do
expect { subject }.to output(/1 MiB[^\n]* 20kb-more-than-1mb\.txt/).to_stdout
end
end

context 'with --long flag for `a.txt`' do
Expand Down Expand Up @@ -347,7 +355,7 @@
let(:args) { ['--report', '--report=long', FIXTURES] }

it 'shows a report with recognized and unrecognized files' do
expect { subject }.to output(/Recognized files\s+: 4\n.+Unrecognized files\s+: 3/).to_stdout
expect { subject }.to output(/Recognized files\s+: 6\n.+Unrecognized files\s+: 3/).to_stdout
end
end

Expand Down
Binary file added spec/fixtures/20kb-less-than-2mb.txt
Binary file not shown.
Binary file added spec/fixtures/20kb-more-than-1mb.txt
Binary file not shown.

0 comments on commit 9914606

Please sign in to comment.