diff --git a/.travis.yml b/.travis.yml index 361f7e5..ba49a38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 391e56e..542bf5e 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -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 diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 936851f..1f79e50 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -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 diff --git a/spec/fixtures/a-file b/spec/fixtures/a-file index e69de29..1f0e485 100644 --- a/spec/fixtures/a-file +++ b/spec/fixtures/a-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. diff --git a/spec/fixtures/z-file b/spec/fixtures/z-file index e69de29..cee7a92 100644 --- a/spec/fixtures/z-file +++ b/spec/fixtures/z-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. diff --git a/spec/flags_spec.rb b/spec/flags_spec.rb index edca523..685497e 100644 --- a/spec/flags_spec.rb +++ b/spec/flags_spec.rb @@ -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] }