From 25b21c442ad60cd7611663eafa34dbe43aab2bf3 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Fri, 27 Oct 2017 12:53:04 +0200 Subject: [PATCH] Add support for reverse sorting Re-assign the `-r` short option from `--report` to `--reverse` and mention this change in the post install message. --- colorls.gemspec | 1 + lib/colorls/core.rb | 5 ++++- lib/colorls/flags.rb | 5 ++++- spec/flags_spec.rb | 6 ++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/colorls.gemspec b/colorls.gemspec index 4dd6128..26f6afa 100644 --- a/colorls.gemspec +++ b/colorls.gemspec @@ -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 diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index c76251b..391e56e 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -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) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 7f6ad42..936851f 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -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 } diff --git a/spec/flags_spec.rb b/spec/flags_spec.rb index f68a31c..edca523 100644 --- a/spec/flags_spec.rb +++ b/spec/flags_spec.rb @@ -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"] }