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
|
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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
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]" \
|
"-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]" \
|
||||||
|
|
Loading…
Reference in a new issue