Merge pull request #200 from avdv/sort-by-extension

Add `-X` / `--sort=extension` flags
This commit is contained in:
Claudio Bley 2018-08-12 18:33:04 +02:00 committed by GitHub
commit 159649ab65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 34 additions and 15 deletions

View file

@ -13,6 +13,7 @@ end
desc 'Build the manual'
file 'man/colorls.1' => ['man/colorls.1.ronn', 'lib/colorls/flags.rb'] do
require 'colorls'
require 'date'
require 'ronn'
flags = ColorLS::Flags.new

View file

@ -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

View file

@ -72,19 +72,19 @@ module ColorLS
options.separator 'sorting options:'
options.separator ''
options.on('--sd', '--sort-dirs', '--group-directories-first', 'sort directories first') { @opts[:group] = :dirs }
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('-S', 'sort by file size, largest first') { @opts[:sort] = :size }
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('-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

View file

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "COLORLS" "1" "November 2017" "colorls 1.1.0" "colorls Manual"
.TH "COLORLS" "1" "January 2018" "colorls 1.1.1" "colorls Manual"
.
.SH "NAME"
\fBcolorls\fR \- list directory contents with colors and icons
@ -79,8 +79,12 @@ do not sort; list entries in directory order
sort by file size, largest first
.
.TP
\fB\-X\fR
sort by file extension
.
.TP
\fB\-\-sort\fR
sort by WORD instead of name: none, size (\-S), time (\-t)
sort by WORD instead of name: none, size (\-S), time (\-t), extension (\-X)
.
.TP
\fB\-r\fR, \fB\-\-reverse\fR

View file

@ -20,7 +20,7 @@ RSpec.describe ColorLS::Flags do
it { is_expected.not_to match(/((r|-).*(w|-).*(x|-).*){3}/) } # does not list file info
it { is_expected.not_to match(/\.hidden-file/) } # does not display hidden files
it { is_expected.not_to match(/Found \d+ contents/) } # does not show a report
it { is_expected.to match(/a-file.+symlinks.+z-file/) } # displays dirs & files alphabetically
it { is_expected.to match(/a-file.+symlinks.+z-file/m) } # displays dirs & files alphabetically
it { is_expected.not_to match(/(.*\n){3}/) } # displays multiple files per line
it { is_expected.not_to match(%r(\.{1,2}/)) } # does not display ./ or ../
it { is_expected.not_to match(/├──/) } # does not display file hierarchy
@ -29,7 +29,7 @@ RSpec.describe ColorLS::Flags do
context 'with --reverse flag' do
let(:args) { ['--reverse', FIXTURES] }
it { is_expected.to match(/z-file.+symlinks.+a-file/) } # displays dirs & files in reverse alphabetical order
it { is_expected.to match(/z-file.+symlinks.+a-file/m) } # displays dirs & files in reverse alphabetical order
end
context 'with --long flag & file path' do
@ -53,13 +53,13 @@ RSpec.describe ColorLS::Flags do
context 'with --sort-dirs flag' do
let(:args) { ['--sort-dirs', FIXTURES] }
it { is_expected.to match(/symlinks.+a-file.+z-file/) } # sorts results alphabetically, directories first
it { is_expected.to match(/symlinks.+a-file.+z-file/m) } # sorts results alphabetically, directories first
end
context 'with --sort-files flag' do
let(:args) { ['--sort-files', FIXTURES] }
it { is_expected.to match(/a-file.+z-file.+symlinks/) } # sorts results alphabetically, files first
it { is_expected.to match(/a-file.+z-file.+symlinks/m) } # sorts results alphabetically, files first
end
context 'with --sort=time' do
@ -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/m) } # sorts results by extension
end
context 'with --dirs flag' do
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,7 +27,8 @@ _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]" \
"--sort[sort by WORD instead of name: none, size (-S), time (-t)]" \
"-X[sort by file extension]" \
"--sort[sort by WORD instead of name: none, size (-S), time (-t), extension (-X)]" \
"-r[reverse order while sorting]" \
"--reverse[reverse order while sorting]" \
"--light[use light color scheme]" \