mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Corrected rubocop issues
This commit is contained in:
parent
dd4bdc85f3
commit
056657b97e
1 changed files with 52 additions and 42 deletions
94
colorls.rb
94
colorls.rb
|
@ -6,47 +6,63 @@ require 'facets'
|
||||||
require 'terminfo'
|
require 'terminfo'
|
||||||
|
|
||||||
# Source for icons unicode: http://nerdfonts.com/
|
# Source for icons unicode: http://nerdfonts.com/
|
||||||
class ColorLS
|
class ColorLS # rubocop:disable ClassLength
|
||||||
def initialize(input, report, sort, one_per_line)
|
def initialize(input, report, sort, one_per_line)
|
||||||
@input = input || Dir.pwd
|
@input = input || Dir.pwd
|
||||||
|
|
||||||
if Dir.exists?(@input)
|
|
||||||
@contents = Dir.entries(@input) - ['.', '..']
|
|
||||||
else
|
|
||||||
if File.exists?(@input)
|
|
||||||
@contents = [@input]
|
|
||||||
else
|
|
||||||
raise ArgumentError, "Specified path doesn't exist: " + @input
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@count = { folders: 0, recognized_files: 0, unrecognized_files: 0 }
|
@count = { folders: 0, recognized_files: 0, unrecognized_files: 0 }
|
||||||
@report = report
|
@report = report
|
||||||
@screen_width = TermInfo.screen_size.last
|
@sort = sort
|
||||||
@one_per_line = one_per_line
|
@one_per_line = one_per_line
|
||||||
|
@screen_width = TermInfo.screen_size.last
|
||||||
|
|
||||||
@contents.sort! { |a, b|
|
init_contents
|
||||||
if sort == 'dirs-first'
|
|
||||||
case
|
|
||||||
when Dir.exist?("#{@input}/#{a}") && ! Dir.exist?("#{@input}/#{b}")
|
|
||||||
-1
|
|
||||||
when ! Dir.exist?("#{@input}/#{a}") && Dir.exist?("#{@input}/#{b}")
|
|
||||||
1
|
|
||||||
else
|
|
||||||
a.downcase <=> b.downcase
|
|
||||||
end
|
|
||||||
else
|
|
||||||
a.downcase <=> b.downcase
|
|
||||||
end
|
|
||||||
} if sort
|
|
||||||
|
|
||||||
@max_widths = @contents.map(&:length)
|
@max_widths = @contents.map(&:length)
|
||||||
|
|
||||||
init_icons
|
init_icons
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def init_contents
|
||||||
|
if Dir.exist?(@input)
|
||||||
|
@contents = Dir.entries(@input) - ['.', '..']
|
||||||
|
elsif File.exist?(@input)
|
||||||
|
@contents = [@input]
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Specified path doesn't exist: " + @input
|
||||||
|
end
|
||||||
|
|
||||||
|
sort_contents
|
||||||
|
end
|
||||||
|
|
||||||
|
def sort_contents
|
||||||
|
return unless @sort
|
||||||
|
|
||||||
|
@contents.sort! do |a, b|
|
||||||
|
if @sort == 'dirs-first'
|
||||||
|
cmp_by_dirs(a, b)
|
||||||
|
else
|
||||||
|
cmp_by_alpha(a, b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cmp_by_dirs(a, b)
|
||||||
|
if Dir.exist?("#{@input}/#{a}") && !Dir.exist?("#{@input}/#{b}")
|
||||||
|
-1
|
||||||
|
elsif !Dir.exist?("#{@input}/#{a}") && Dir.exist?("#{@input}/#{b}")
|
||||||
|
1
|
||||||
|
else
|
||||||
|
cmp_by_alpha(a, b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def cmp_by_alpha(a, b)
|
||||||
|
a.downcase <=> b.downcase
|
||||||
|
end
|
||||||
|
|
||||||
def ls
|
def ls
|
||||||
@contents = chunkify
|
@contents = chunkify
|
||||||
|
@max_widths = @contents.transpose.map { |c| c.map(&:length).max }
|
||||||
@contents.each { |chunk| ls_line(chunk) }
|
@contents.each { |chunk| ls_line(chunk) }
|
||||||
print "\n"
|
print "\n"
|
||||||
display_report if @report
|
display_report if @report
|
||||||
|
@ -73,9 +89,7 @@ class ColorLS
|
||||||
|
|
||||||
def chunkify
|
def chunkify
|
||||||
if @one_per_line
|
if @one_per_line
|
||||||
chunks = @contents.map { |x| [x] }
|
@contents.map { |x| [x] }
|
||||||
@max_widths = chunks.transpose.map { |c| c.map(&:length).max }
|
|
||||||
chunks
|
|
||||||
else
|
else
|
||||||
chunk_size = @contents.count
|
chunk_size = @contents.count
|
||||||
|
|
||||||
|
@ -91,7 +105,6 @@ class ColorLS
|
||||||
def get_chunk(chunk_size)
|
def get_chunk(chunk_size)
|
||||||
chunk = @contents.each_slice(chunk_size).to_a
|
chunk = @contents.each_slice(chunk_size).to_a
|
||||||
chunk.last += [''] * (chunk_size - chunk.last.count)
|
chunk.last += [''] * (chunk_size - chunk.last.count)
|
||||||
@max_widths = chunk.transpose.map { |c| c.map(&:length).max }
|
|
||||||
chunk
|
chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -164,19 +177,16 @@ report = false
|
||||||
sort = false
|
sort = false
|
||||||
one_per_line = false
|
one_per_line = false
|
||||||
|
|
||||||
args.each { |arg|
|
args.each do |arg|
|
||||||
if arg == '--report' || arg == '-r'
|
report = true if ['--report', '-r'].include?(arg)
|
||||||
report = true
|
one_per_line = true if arg == '-1'
|
||||||
end
|
|
||||||
|
|
||||||
if arg == '-1'
|
match = arg.match(/^--sort=?(.*)?$/)
|
||||||
one_per_line = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if match = arg.match(/^--sort=?(.*)?$/)
|
if match
|
||||||
sort = match.captures && match.captures[0] == 'dirs-first' ? 'dirs-first' : true
|
sort = match.captures[0] == 'dirs-first' ? 'dirs-first' : true
|
||||||
end
|
end
|
||||||
}
|
end
|
||||||
|
|
||||||
args.keep_if { |arg| !arg.start_with?('-') }
|
args.keep_if { |arg| !arg.start_with?('-') }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue