mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Added clubbed flags and renamed multi-character shorthand arguments (#125)
* Added clubbed flags and renamed multi-character shorthand arguments to not interfere with clubbed flags * Cleaned up code * Fixed parsing arguments without dash * Made -h flag clubbable * Minor text fixes * Cleaned up incompatible flags method
This commit is contained in:
parent
8dec4115ef
commit
3af107627b
3 changed files with 31 additions and 19 deletions
|
@ -22,11 +22,11 @@ script:
|
||||||
- ( cd spec/fixtures ; colorls .hidden-file ) | fgrep '.hidden-file'
|
- ( cd spec/fixtures ; colorls .hidden-file ) | fgrep '.hidden-file'
|
||||||
- colorls -l README.md
|
- colorls -l README.md
|
||||||
- colorls -r
|
- colorls -r
|
||||||
- colorls -sd
|
- colorls --sd
|
||||||
- colorls -sf
|
- colorls --sf
|
||||||
- colorls -t
|
- colorls -t
|
||||||
- colorls -h
|
- colorls -h
|
||||||
- colorls -gs
|
- colorls --gs
|
||||||
- colorls spec/fixtures/symlinks
|
- colorls spec/fixtures/symlinks
|
||||||
- colorls README.md
|
- colorls README.md
|
||||||
- colorls *
|
- colorls *
|
||||||
|
|
|
@ -25,7 +25,7 @@ module ColorLS
|
||||||
def ls
|
def ls
|
||||||
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
|
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
|
||||||
|
|
||||||
return helplog if @help
|
helplog if @help
|
||||||
|
|
||||||
if @tree
|
if @tree
|
||||||
print "\n"
|
print "\n"
|
||||||
|
|
|
@ -5,16 +5,16 @@ module ColorLS
|
||||||
set_color_opts
|
set_color_opts
|
||||||
|
|
||||||
@opts = {
|
@opts = {
|
||||||
show: fetch_show_opts,
|
show: fetch_show_opts,
|
||||||
sort: fetch_sort_opts,
|
sort: fetch_sort_opts,
|
||||||
all: flag_given?(%w[-a --all]),
|
all: flag_given?(%w[-a --all]),
|
||||||
almost_all: flag_given?(%w[-A --almost-all]),
|
almost_all: flag_given?(%w[-A --almost-all]),
|
||||||
report: flag_given?(%w[-r --report]),
|
report: flag_given?(%w[-r --report]),
|
||||||
one_per_line: flag_given?(%w[-1]) || !STDOUT.tty?,
|
one_per_line: flag_given?(%w[-1]) || !STDOUT.tty?,
|
||||||
long: flag_given?(%w[-l --long]),
|
long: flag_given?(%w[-l --long]),
|
||||||
tree: flag_given?(%w[-t --tree]),
|
tree: flag_given?(%w[-t --tree]),
|
||||||
help: flag_given?(%w[-h --help]),
|
help: flag_given?(%w[-h --help]),
|
||||||
git_status: flag_given?(%w[-gs --git-status]),
|
git_status: flag_given?(%w[--gs --git-status]),
|
||||||
colors: @colors
|
colors: @colors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,14 @@ module ColorLS
|
||||||
private
|
private
|
||||||
|
|
||||||
def flag_given?(flags)
|
def flag_given?(flags)
|
||||||
flags.any? { |flag| @args.include?(flag) }
|
return true if flags.any? { |flag| @args.include?(flag) }
|
||||||
|
|
||||||
|
clubbable_flags = flags.reject { |flag| flag.start_with?('--') }
|
||||||
|
.map { |flag| flag[1..-1] }
|
||||||
|
|
||||||
|
# Some flags should be not be able to be clubbed with other flags
|
||||||
|
@args.select { |arg| !arg.start_with?('--') && arg.start_with?('-') }
|
||||||
|
.any? { |arg| clubbable_flags.any? { |flag| arg.include?(flag) } }
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_color_opts
|
def set_color_opts
|
||||||
|
@ -65,11 +72,11 @@ module ColorLS
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_sort_opts
|
def fetch_sort_opts
|
||||||
sort_dirs_first = flag_given? %w[-sd --sort-dirs --group-directories-first]
|
sort_dirs_first = flag_given? %w[--sd --sort-dirs --group-directories-first]
|
||||||
sort_files_first = flag_given? %w[-sf --sort-files]
|
sort_files_first = flag_given? %w[--sf --sort-files]
|
||||||
|
|
||||||
if sort_dirs_first && sort_files_first
|
if sort_dirs_first && sort_files_first
|
||||||
STDERR.puts "\n Restrain from using -sd and -sf flags together."
|
STDERR.puts "\n Restrain from using --sd and -sf flags together."
|
||||||
.colorize(@colors[:error])
|
.colorize(@colors[:error])
|
||||||
else
|
else
|
||||||
return :files if sort_files_first
|
return :files if sort_files_first
|
||||||
|
@ -81,8 +88,13 @@ module ColorLS
|
||||||
def incompatible_flags?
|
def incompatible_flags?
|
||||||
return '' if @opts[:show].nil? || @opts[:sort].nil?
|
return '' if @opts[:show].nil? || @opts[:sort].nil?
|
||||||
|
|
||||||
return 'Restrain from using -t (--tree) and -r (--report) flags together.' if @opts[:tree] && @opts[:report]
|
[
|
||||||
return 'Restrain from using -t (--tree) and -a (--all) flags together.' if @opts[:tree] && @opts[:all]
|
['-t (--tree)', @opts[:tree], '-r (--report)', @opts[:report]],
|
||||||
|
['-t (--tree)', @opts[:tree], '-l (--long)', @opts[:long]],
|
||||||
|
['-t (--tree)', @opts[:tree], '-a (--all)', @opts[:all]]
|
||||||
|
].each do |flag1, hasflag1, flag2, hasflag2|
|
||||||
|
return "Restrain from using #{flag1} and #{flag2} flags together." if hasflag1 && hasflag2
|
||||||
|
end
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue