mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Add -X
/ --sort=extension
flags
This allows the listing to be sorted alphabetically by file extension. Fixes #199.
This commit is contained in:
parent
6cecaf4654
commit
012aeb8493
7 changed files with 18 additions and 4 deletions
|
@ -92,6 +92,13 @@ module ColorLS
|
|||
|
||||
def sort_contents
|
||||
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
|
||||
@contents.sort_by! { |a| -a.mtime.to_f }
|
||||
when :size
|
||||
|
|
|
@ -76,15 +76,15 @@ module ColorLS
|
|||
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('-S', 'sort by file size, largest first') { @opts[:sort] = :size }
|
||||
options.on('-X', 'sort by file extension') { @opts[:sort] = :extension }
|
||||
options.on(
|
||||
'--sort=WORD',
|
||||
%w[none time size],
|
||||
'sort by WORD instead of name: none, size (-S), time (-t)'
|
||||
%w[none time size extension],
|
||||
'sort by WORD instead of name: none, size (-S), time (-t), extension (-X)'
|
||||
) do |word|
|
||||
@opts[:sort] = case word
|
||||
when 'none' then false
|
||||
when 'time' then :time
|
||||
when 'size' then :size
|
||||
else word.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -85,6 +85,12 @@ RSpec.describe ColorLS::Flags do
|
|||
it { is_expected.to match(/a-file.+z-file.+symlinks/) } # sorts results by size
|
||||
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
|
||||
let(:args) { ['--dirs', FIXTURES] }
|
||||
|
||||
|
|
0
spec/fixtures/a.md
vendored
Normal file
0
spec/fixtures/a.md
vendored
Normal file
0
spec/fixtures/a.txt
vendored
Normal file
0
spec/fixtures/a.txt
vendored
Normal file
0
spec/fixtures/z.txt
vendored
Normal file
0
spec/fixtures/z.txt
vendored
Normal file
|
@ -27,6 +27,7 @@ _arguments -s -S \
|
|||
"-t[sort by modification time, newest first]" \
|
||||
"-U[do not sort; list entries in directory order]" \
|
||||
"-S[sort by file size, largest first]" \
|
||||
"-X[sort by file extension]" \
|
||||
"--sort[sort by WORD instead of name: none, size (-S), time (-t)]" \
|
||||
"-r[reverse order while sorting]" \
|
||||
"--reverse[reverse order while sorting]" \
|
||||
|
|
Loading…
Reference in a new issue