Add -X / --sort=extension flags

This allows the listing to be sorted alphabetically by file extension.

Fixes #199.
This commit is contained in:
Claudio Bley 2018-07-10 21:53:32 +02:00
parent 6cecaf4654
commit 012aeb8493
7 changed files with 18 additions and 4 deletions

View file

@ -92,6 +92,13 @@ module ColorLS
def sort_contents def sort_contents
case @sort case @sort
when :extension
@contents.sort_by! do |f|
name = f.name
ext = File.extname(name)
name = name.chomp(ext) unless ext.empty?
[ext, name].map { |s| CLocale.strxfrm(s) }
end
when :time when :time
@contents.sort_by! { |a| -a.mtime.to_f } @contents.sort_by! { |a| -a.mtime.to_f }
when :size when :size

View file

@ -76,15 +76,15 @@ module ColorLS
options.on('-t', 'sort by modification time, newest first') { @opts[:sort] = :time } 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('-U', 'do not sort; list entries in directory order') { @opts[:sort] = false }
options.on('-S', 'sort by file size, largest first') { @opts[:sort] = :size } options.on('-S', 'sort by file size, largest first') { @opts[:sort] = :size }
options.on('-X', 'sort by file extension') { @opts[:sort] = :extension }
options.on( options.on(
'--sort=WORD', '--sort=WORD',
%w[none time size], %w[none time size extension],
'sort by WORD instead of name: none, size (-S), time (-t)' 'sort by WORD instead of name: none, size (-S), time (-t), extension (-X)'
) do |word| ) do |word|
@opts[:sort] = case word @opts[:sort] = case word
when 'none' then false when 'none' then false
when 'time' then :time else word.to_sym
when 'size' then :size
end end
end end

View file

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

0
spec/fixtures/a.md vendored Normal file
View file

0
spec/fixtures/a.txt vendored Normal file
View file

0
spec/fixtures/z.txt vendored Normal file
View file

View file

@ -27,6 +27,7 @@ _arguments -s -S \
"-t[sort by modification time, newest first]" \ "-t[sort by modification time, newest first]" \
"-U[do not sort; list entries in directory order]" \ "-U[do not sort; list entries in directory order]" \
"-S[sort by file size, largest first]" \ "-S[sort by file size, largest first]" \
"-X[sort by file extension]" \
"--sort[sort by WORD instead of name: none, size (-S), time (-t)]" \ "--sort[sort by WORD instead of name: none, size (-S), time (-t)]" \
"-r[reverse order while sorting]" \ "-r[reverse order while sorting]" \
"--reverse[reverse order while sorting]" \ "--reverse[reverse order while sorting]" \