mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Improves CLI operation with optparse module (#100)
* Revert commit d3e0cd3bb8
The help will be generated automatically.
* Use optparse module for options parsing
Use the `optparse` module instead of open coding the options parsing.
It can automatically generate a help message about available options, can
generate completion code for Bash and Zsh and also supports clubbed flags like
GNU's `getopt`.
This commit is contained in:
parent
50a0db1836
commit
a4c6766141
2 changed files with 89 additions and 85 deletions
|
@ -1,7 +1,7 @@
|
|||
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, help: false, colors: [])
|
||||
one_per_line: false, git_status: false,long: false, almost_all: false, tree: false, colors: [])
|
||||
@input = input || Dir.pwd
|
||||
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
|
||||
@all = all
|
||||
|
@ -12,7 +12,6 @@ module ColorLS
|
|||
@one_per_line = one_per_line
|
||||
@long = long
|
||||
@tree = tree
|
||||
@help = help
|
||||
@git_status = git_status
|
||||
@screen_width = ::TermInfo.screen_size.last
|
||||
@colors = colors
|
||||
|
@ -25,8 +24,6 @@ module ColorLS
|
|||
def ls
|
||||
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
|
||||
|
||||
helplog if @help
|
||||
|
||||
if @tree
|
||||
print "\n"
|
||||
tree_traverse(@input, 0, 2)
|
||||
|
@ -318,27 +315,5 @@ module ColorLS
|
|||
return prespace_icon if prespace.zero?
|
||||
' │ ' * (prespace/indent) + prespace_icon + '─' * indent
|
||||
end
|
||||
|
||||
def helplog
|
||||
print "\nUsage: colorls <command> [-<attributes> (or) --<attributes>] <path> <keyword>\n\n
|
||||
The available attributes are:\n
|
||||
\t1 list in a line
|
||||
\ta (or) all list inclding hidden files in the directory
|
||||
\tA (or) almost-all list almost all the files
|
||||
\td (or) dirs list directories only
|
||||
\tf (or) files list files only
|
||||
\tl (or) long show list with long format
|
||||
\tr (or) report detailed report of the files
|
||||
\tsd (or) sort-dirs sorted and grouped list of directiories followed by files
|
||||
\t (or) group-directories-first
|
||||
\tsf (or) sort-files sorted and grouped list of files followed by directiories
|
||||
\tgs (or) git-status shows the git status of the file [U:Untracked,A:Added,C:Changed]
|
||||
\tt (or) tree shows tree view of the directory
|
||||
\th (or) help show this page\n\n
|
||||
The available commands are:\n
|
||||
\tREADME.md lists the README file irrespective of current path
|
||||
\t* colorls called recursively for each subsequent directory
|
||||
\t| grep lists the files having the given keyword in the name\n\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,24 +1,25 @@
|
|||
require 'optparse'
|
||||
require 'colorls/version'
|
||||
|
||||
module ColorLS
|
||||
class Flags
|
||||
def initialize(*args)
|
||||
@args = args
|
||||
set_color_opts
|
||||
|
||||
@light_colors = false
|
||||
@opts = {
|
||||
show: fetch_show_opts,
|
||||
sort: fetch_sort_opts,
|
||||
all: flag_given?(%w[-a --all]),
|
||||
almost_all: flag_given?(%w[-A --almost-all]),
|
||||
report: flag_given?(%w[-r --report]),
|
||||
one_per_line: flag_given?(%w[-1]) || !STDOUT.tty?,
|
||||
long: flag_given?(%w[-l --long]),
|
||||
tree: flag_given?(%w[-t --tree]),
|
||||
help: flag_given?(%w[-h --help]),
|
||||
git_status: flag_given?(%w[--gs --git-status]),
|
||||
colors: @colors
|
||||
show: false,
|
||||
sort: false,
|
||||
all: false,
|
||||
almost_all: false,
|
||||
report: false,
|
||||
one_per_line: !STDOUT.tty?,
|
||||
long: false,
|
||||
tree: false,
|
||||
git_status: false,
|
||||
colors: []
|
||||
}
|
||||
|
||||
@args.keep_if { |arg| !arg.start_with?('-') }
|
||||
parse_options
|
||||
end
|
||||
|
||||
def process
|
||||
|
@ -33,56 +34,84 @@ module ColorLS
|
|||
|
||||
private
|
||||
|
||||
def flag_given?(flags)
|
||||
return true if flags.any? { |flag| @args.include?(flag) }
|
||||
def add_sort_options(options)
|
||||
options.separator ''
|
||||
options.separator 'sorting options:'
|
||||
options.separator ''
|
||||
options.on('--sd', '--sort-dirs', '--group-directories-first', 'sort directories first') { @opts[:sort] = :dirs }
|
||||
options.on('--sf', '--sort-files', 'sort files first') { @opts[:sort] = :files }
|
||||
end
|
||||
|
||||
clubbable_flags = flags.reject { |flag| flag.start_with?('--') }
|
||||
.map { |flag| flag[1..-1] }
|
||||
def add_common_options(options)
|
||||
options.on('-a', '--all', 'do not ignore entries starting with .') { @opts[:all] = true }
|
||||
options.on('-A', '--almost-all', 'do not list . and ..') { @opts[:almost_all] = true }
|
||||
options.on('-l', '--long', 'use a long listing format') { @opts[:long] = true }
|
||||
options.on('-t', '--tree', 'shows tree view of the directory') { @opts[:tree] = true }
|
||||
options.on('-r', '--report', 'show brief report') { @opts[:report] = true }
|
||||
options.on('-1', 'list one file per line') { @opts[:one_per_line] = true }
|
||||
options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs }
|
||||
options.on('-f', '--files', 'show only files') { @opts[:show] = :files }
|
||||
options.on('--gs', '--git-status', 'show git status for each file') { @opts[:git_status] = true }
|
||||
end
|
||||
|
||||
# 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) } }
|
||||
def add_general_options(options)
|
||||
options.separator ''
|
||||
options.separator 'general options:'
|
||||
|
||||
options.separator ''
|
||||
options.on('--light', 'use light color scheme') { @light_colors = true }
|
||||
options.on('--dark', 'use dark color scheme') { @light_colors = false }
|
||||
end
|
||||
|
||||
def show_examples
|
||||
puts <<EXAMPLES.gsub(/^ /, '')
|
||||
|
||||
examples:
|
||||
|
||||
* show the given file:
|
||||
|
||||
#{'colorls README.md'.colorize(:green)}
|
||||
|
||||
* show matching files and list matching directories:
|
||||
|
||||
#{'colorls *'.colorize(:green)}
|
||||
|
||||
* filter output by a regular expression:
|
||||
|
||||
#{'colorls | grep PATTERN'.colorize(:green)}
|
||||
|
||||
EXAMPLES
|
||||
end
|
||||
|
||||
def parse_options
|
||||
parser = OptionParser.new do |opts|
|
||||
opts.banner = 'Usage: colorls [OPTION]... [FILE]...'
|
||||
opts.separator ''
|
||||
|
||||
add_common_options(opts)
|
||||
add_sort_options(opts)
|
||||
add_general_options(opts)
|
||||
|
||||
opts.separator ''
|
||||
opts.on_tail('-h', '--help', 'prints this help') do
|
||||
puts parser
|
||||
show_examples
|
||||
exit
|
||||
end
|
||||
opts.on_tail('--version', 'show version') do
|
||||
puts ColorLS::VERSION
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
parser.parse!(@args)
|
||||
|
||||
set_color_opts
|
||||
end
|
||||
|
||||
def set_color_opts
|
||||
light_colors = flag_given? %w[--light]
|
||||
dark_colors = flag_given? %w[--dark]
|
||||
|
||||
if light_colors && dark_colors
|
||||
STDERR.puts "\n Restrain from using --light and --dark flags together."
|
||||
.colorize(@colors[:error])
|
||||
end
|
||||
|
||||
color_scheme_file = light_colors ? 'light_colors.yaml' : 'dark_colors.yaml'
|
||||
@colors = ColorLS.load_from_yaml(color_scheme_file, true)
|
||||
end
|
||||
|
||||
def fetch_show_opts
|
||||
show_dirs_only = flag_given? %w[-d --dirs]
|
||||
show_files_only = flag_given? %w[-f --files]
|
||||
|
||||
if show_files_only && show_dirs_only
|
||||
STDERR.puts "\n Restrain from using -d and -f flags together."
|
||||
.colorize(@colors[:error])
|
||||
else
|
||||
return :files if show_files_only
|
||||
return :dirs if show_dirs_only
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def fetch_sort_opts
|
||||
sort_dirs_first = flag_given? %w[--sd --sort-dirs --group-directories-first]
|
||||
sort_files_first = flag_given? %w[--sf --sort-files]
|
||||
|
||||
if sort_dirs_first && sort_files_first
|
||||
STDERR.puts "\n Restrain from using --sd and -sf flags together."
|
||||
.colorize(@colors[:error])
|
||||
else
|
||||
return :files if sort_files_first
|
||||
return :dirs if sort_dirs_first
|
||||
false
|
||||
end
|
||||
color_scheme_file = @light_colors ? 'light_colors.yaml' : 'dark_colors.yaml'
|
||||
@opts[:colors] = ColorLS.load_from_yaml(color_scheme_file, true)
|
||||
end
|
||||
|
||||
def incompatible_flags?
|
||||
|
|
Loading…
Reference in a new issue