mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Updates Ruby gem for release 0.1.0
This commit is contained in:
parent
f993755f0e
commit
4905321dca
4 changed files with 216 additions and 202 deletions
52
exe/lc
52
exe/lc
|
@ -1,55 +1,5 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require 'colorls'
|
||||
|
||||
args = *ARGV
|
||||
opts = {}
|
||||
|
||||
opts[:report] = args.include?('-r') || args.include?('--report')
|
||||
opts[:one_per_line] = args.include?('-1')
|
||||
|
||||
show_dirs_only = args.include?('-d') || args.include?('--dirs')
|
||||
show_files_only = args.include?('-f') || args.include?('--files')
|
||||
sort_dirs_first = args.include?('-sd') || args.include?('--sort-dirs')
|
||||
sort_files_first = args.include?('-sf') || args.include?('--sort-files')
|
||||
|
||||
if sort_dirs_first && sort_files_first
|
||||
STDERR.puts "\n Restrain from using -sd and -sf flags together."
|
||||
.colorize(:red)
|
||||
return
|
||||
end
|
||||
|
||||
if show_files_only && show_dirs_only
|
||||
STDERR.puts "\n Restrain from using -d and -f flags together."
|
||||
.colorize(:red)
|
||||
return
|
||||
end
|
||||
|
||||
opts[:sort] = if sort_files_first
|
||||
:files
|
||||
elsif sort_dirs_first
|
||||
:dirs
|
||||
end
|
||||
|
||||
opts[:show] = if show_files_only
|
||||
:files
|
||||
elsif show_dirs_only
|
||||
:dirs
|
||||
end
|
||||
|
||||
args.keep_if { |arg| !arg.start_with?('-') }
|
||||
|
||||
if args.empty?
|
||||
ColorLS.new(opts).ls
|
||||
else
|
||||
args.each do |path|
|
||||
if Dir.exist?(path)
|
||||
ColorLS.new(path, opts).ls
|
||||
else
|
||||
next STDERR.puts "\n Specified directory '#{path}' doesn't exist."
|
||||
.colorize(:red)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ColorLS::Flags.new(*ARGV).process
|
||||
true
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
require 'colorls/version'
|
||||
|
||||
require 'colorize'
|
||||
require 'yaml'
|
||||
require 'facets'
|
||||
require 'terminfo'
|
||||
|
||||
require 'colorls/flags'
|
||||
# Source for icons unicode: http://nerdfonts.com/
|
||||
class ColorLS
|
||||
def initialize(input=nil, report:, sort:, show:, one_per_line:)
|
||||
module ColorLS
|
||||
class Core
|
||||
def initialize(input=nil, report: false, sort: false, show: false, one_per_line: false)
|
||||
@input = input || Dir.pwd
|
||||
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
|
||||
@report = report
|
||||
|
@ -34,7 +34,7 @@ class ColorLS
|
|||
private
|
||||
|
||||
def init_contents
|
||||
@contents = Dir.entries(@input) - ['.', '..']
|
||||
@contents = Dir.entries(@input) - %w[. ..]
|
||||
|
||||
filter_contents if @show
|
||||
sort_contents if @sort
|
||||
|
@ -164,4 +164,5 @@ class ColorLS
|
|||
key = @file_aliases[key] unless @file_keys.include?(key)
|
||||
[key, :green, :recognized_files]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
63
lib/colorls/flags.rb
Normal file
63
lib/colorls/flags.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
module ColorLS
|
||||
class Flags
|
||||
def initialize(*args)
|
||||
@args = args
|
||||
@opts = {
|
||||
show: fetch_show_opts,
|
||||
sort: fetch_sort_opts,
|
||||
report: flag_given?(%w[-r --report]),
|
||||
one_per_line: flag_given?(%w[-1])
|
||||
}
|
||||
|
||||
return if @opts[:show].nil? || @opts[:sort].nil?
|
||||
|
||||
@args.keep_if { |arg| !arg.start_with?('-') }
|
||||
end
|
||||
|
||||
def process
|
||||
return Core.new(@opts).ls if @args.empty?
|
||||
|
||||
@args.each do |path|
|
||||
next STDERR.puts "\n Specified directory '#{path}' doesn't exist.".colorize(:red) unless Dir.exist?(path)
|
||||
Core.new(path, @opts).ls
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def flag_given?(flags)
|
||||
flags.each { |flag| return true if @args.include?(flag) }
|
||||
false
|
||||
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(:red)
|
||||
return nil
|
||||
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]
|
||||
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(:red)
|
||||
return nil
|
||||
else
|
||||
return :files if sort_files_first
|
||||
return :dirs if sort_dirs_first
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Binary file not shown.
Loading…
Reference in a new issue