diff --git a/colorls.rb b/colorls.rb index 454869b..533b944 100755 --- a/colorls.rb +++ b/colorls.rb @@ -7,7 +7,7 @@ require 'terminfo' # Source for icons unicode: http://nerdfonts.com/ class ColorLS # rubocop:disable ClassLength - def initialize(input, report:, sort:, show:, one_per_line:) + def initialize(input = nil, report:, sort:, show:, one_per_line:) @input = input || Dir.pwd @count = { folders: 0, recognized_files: 0, unrecognized_files: 0 } @report = report @@ -17,62 +17,13 @@ class ColorLS # rubocop:disable ClassLength @screen_width = TermInfo.screen_size.last init_contents - @max_widths = @contents.map(&:length) - init_icons end - def init_contents - unless Dir.exist?(@input) - raise ArgumentError, "Specified directory doesn't exist: " + @input - end - - @contents = Dir.entries(@input) - ['.', '..'] - - filter_contents - sort_contents - end - - def filter_contents - return unless @show - - @contents.keep_if do |x| - if @show == 'dirs' - Dir.exist?("#{@input}/#{x}") - else - !Dir.exist?("#{@input}/#{x}") - end - end - end - - def sort_contents - return unless @sort - - @contents.sort! do |a, b| - cmp_by_dirs(a, b) - end - end - - def cmp_by_dirs(a, b) - is_a_dir = Dir.exist?("#{@input}/#{a}") - is_b_dir = Dir.exist?("#{@input}/#{b}") - - if is_a_dir ^ is_b_dir - result = is_a_dir ? -1 : 1 - result *= -1 if @sort == 'files' - result - else - cmp_by_alpha(a, b) - end - end - - def cmp_by_alpha(a, b) - a.downcase <=> b.downcase - end - def ls @contents = chunkify + @max_widths = @contents.transpose.map { |c| c.map(&:length).max } @contents.each { |chunk| ls_line(chunk) } print "\n" display_report if @report @@ -82,6 +33,41 @@ class ColorLS # rubocop:disable ClassLength private + def init_contents + @contents = Dir.entries(@input) - ['.', '..'] + + filter_contents if @show + sort_contents if @sort + + @total_content_length = @contents.length + end + + def filter_contents + @contents.keep_if do |x| + next Dir.exist?("#{@input}/#{x}") if @show == :dirs + !Dir.exist?("#{@input}/#{x}") + end + end + + def sort_contents + @contents.sort! { |a, b| cmp_by_dirs(a, b) } + end + + def cmp_by_dirs(a, b) + is_a_dir = Dir.exist?("#{@input}/#{a}") + is_b_dir = Dir.exist?("#{@input}/#{b}") + + return cmp_by_alpha(a, b) unless is_a_dir ^ is_b_dir + + result = is_a_dir ? -1 : 1 + result *= -1 if @sort == :files + result + end + + def cmp_by_alpha(a, b) + a.downcase <=> b.downcase + end + def init_icons @files = load_from_yaml('files.yaml') @file_aliases = load_from_yaml('file_aliases.yaml', true) @@ -98,10 +84,7 @@ class ColorLS # rubocop:disable ClassLength end def chunkify - if @one_per_line - @max_widths = [@max_widths.max] - return @contents.zip - end + return @contents.zip if @one_per_line chunk_size = @contents.count @@ -126,7 +109,7 @@ class ColorLS # rubocop:disable ClassLength end def display_report - print "\n Found #{@contents.flatten.length} contents in directory " + print "\n Found #{@total_content_length} contents in directory " .colorize(:white) print File.expand_path(@input).to_s.colorize(:blue) @@ -185,52 +168,52 @@ class ColorLS # rubocop:disable ClassLength end args = *ARGV -report = args.include?('--report') || args.include?('-r') -one_per_line = args.include?('-1') -sort_files_first = args.include?('-sf') || args.include?('--sort-files') +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') -show_files_only = args.include?('-f') || args.include?('--files') -show_dirs_only = args.include?('-d') || args.include?('--dirs') +sort_files_first = args.include?('-sf') || args.include?('--sort-files') if sort_dirs_first && sort_files_first - raise ArgumentError, 'Restrain from using -sd and -sf flags together' + STDERR.puts "\n Restrain from using -sd and -sf flags together." + .colorize(:red) + return end if show_files_only && show_dirs_only - raise ArgumentError, 'Restrain from using -d and -f flags together' + STDERR.puts "\n Restrain from using -d and -f flags together." + .colorize(:red) + return end -sort = if sort_files_first - 'files' - elsif sort_dirs_first - 'dirs' - else - false - end +opts[:sort] = if sort_files_first + :files + elsif sort_dirs_first + :dirs + end -show = if show_files_only - 'files' - elsif show_dirs_only - 'dirs' - else - false - 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(nil, - report: report, - sort: sort, - show: show, - one_per_line: one_per_line).ls + ColorLS.new(opts).ls else args.each do |path| - ColorLS.new(path, - report: report, - sort: sort, - show: show, - one_per_line: one_per_line).ls + 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