Add support for reverse sorting

Re-assign the `-r` short option from `--report` to `--reverse` and mention this
change in the post install message.
This commit is contained in:
Claudio Bley 2017-10-27 12:53:04 +02:00
parent dcf5951167
commit 25b21c442a
4 changed files with 15 additions and 2 deletions

View file

@ -16,6 +16,7 @@ ColorLS::POST_INSTALL_MESSAGE = %(
Tab completion enabled for flags
-t flag : Previously short for --tree, has been re-allocated to sort results by time
-r flag : Previously short for --report, has been re-allocated to reverse sort results
*******************************************************************
).freeze

View file

@ -1,13 +1,15 @@
module ColorLS
class Core
def initialize(input=nil, all: false, report: false, sort: false, show: false,
one_per_line: false, git_status: false,long: false, almost_all: false, tree: false, colors: [], group: nil)
one_per_line: false, git_status: false,long: false, almost_all: false, tree: false, colors: [], group: nil,
reverse: false)
@input = init_input_path(input)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@almost_all = almost_all
@report = report
@sort = sort
@reverse = reverse
@group = group
@show = show
@one_per_line = one_per_line
@ -115,6 +117,7 @@ module ColorLS
else
@contents.sort! { |a, b| a.casecmp(b) }
end
@contents.reverse! if @reverse
end
def group_contents(path)

View file

@ -11,6 +11,7 @@ module ColorLS
@opts = {
show: false,
sort: true,
reverse: false,
group: nil,
all: false,
almost_all: false,
@ -62,6 +63,8 @@ module ColorLS
when 'time' then :time
end
end
options.on('-r', '--reverse', 'reverse order while sorting') { @opts[:reverse] = true }
end
def add_common_options(options)
@ -69,7 +72,7 @@ module ColorLS
options.on('-A', '--almost-all', 'do not list . and ..') { @opts[:almost_all] = true }
options.on('-l', '--long', 'use a long listing format') { @mode = :long }
options.on('--tree', 'shows tree view of the directory') { @mode = :tree }
options.on('-r', '--report', 'show brief report') { @opts[:report] = true }
options.on('--report', 'show brief report') { @opts[:report] = true }
options.on('-1', 'list one file per line') { @mode = :one_per_line }
options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs }
options.on('-f', '--files', 'show only files') { @opts[:show] = :files }

View file

@ -26,6 +26,12 @@ RSpec.describe ColorLS::Flags do
it { is_expected.to_not match(/├──/) } # does not display file hierarchy
end
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
end
context 'with --long flag & file path' do
let(:args) { ['--long', "#{FIXTURES}/.hidden-file"] }