Add support for sorting by size

* add `--sort=size` flag
* add `-S` flag
* adjust `.travis.yml` to check the new flags
This commit is contained in:
Matthew Vincent 2017-10-31 15:17:13 +00:00 committed by Claudio Bley
parent 25b21c442a
commit ba257b970b
6 changed files with 22 additions and 2 deletions

View file

@ -28,6 +28,8 @@ script:
- colorls --sort=time
- colorls -U
- colorls --sort=none
- colorls -S
- colorls --sort=size
- colorls -h
- colorls --gs
- colorls spec/fixtures/symlinks

View file

@ -66,7 +66,7 @@ module ColorLS
sort_contents(path) if @sort
group_contents(path) if @group
@total_content_length = @contents.length
@total_content_length = @contents.count
return @contents unless @long
init_user_lengths(path)
@ -114,6 +114,8 @@ module ColorLS
case @sort
when :time
@contents.sort_by! { |a| -File.mtime(File.join(path, a)).to_f }
when :size
@contents.sort_by! { |a| -File.size(File.join(path, a)) }
else
@contents.sort! { |a, b| a.casecmp(b) }
end

View file

@ -57,10 +57,16 @@ module ColorLS
options.on('--sf', '--sort-files', 'sort files first') { @opts[:group] = :files }
options.on('-t', 'sort by modification time, newest first') { @opts[:sort] = :time }
options.on('-U', 'do not sort; list entries in directory order') { @opts[:sort] = false }
options.on('--sort=WORD', %w[none time], 'sort by WORD instead of name: none, time (-t)') do |word|
options.on('-S', 'sort by file size, largest first') { @opts[:sort] = :size }
options.on(
'--sort=WORD',
%w[none time size],
'sort by WORD instead of name: none, size (-S), time (-t)'
) do |word|
@opts[:sort] = case word
when 'none' then false
when 'time' then :time
when 'size' then :size
end
end

View file

@ -0,0 +1,3 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

View file

@ -0,0 +1 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

View file

@ -79,6 +79,12 @@ RSpec.describe ColorLS::Flags do
it { is_expected.to match(expected) }
end
context 'with --sort=size flag' do
let(:args) { ['--sort=size', FIXTURES] }
it { is_expected.to match(/a-file.+z-file.+symlinks/) } # sorts results by size
end
context 'with --dirs flag' do
let(:args) { ['--dirs', FIXTURES] }