Merge pull request #166 from avdv/rescue-errors

Handle errors gracefully
This commit is contained in:
Athitya Kumar 2017-12-13 13:08:41 +05:30 committed by GitHub
commit 445cfc3836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 18 deletions

View file

@ -1,9 +1,9 @@
module ColorLS
class Core
def initialize(input=nil, all: false, report: false, sort: false, show: false,
def initialize(input, all: false, report: false, sort: false, show: false,
mode: nil, git_status: false, almost_all: false, colors: [], group: nil,
reverse: false)
@input = init_input_path(input)
@input = File.absolute_path(input)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@almost_all = almost_all
@ -44,17 +44,6 @@ module ColorLS
private
def init_input_path(input)
return Dir.pwd unless input
return input unless Dir.exist?(input)
actual = Dir.pwd
Dir.chdir(input)
input = Dir.pwd
Dir.chdir(actual)
input
end
def init_contents(path)
is_directory = Dir.exist?(path)
@contents = if is_directory

View file

@ -37,12 +37,16 @@ module ColorLS
# initialize locale from environment
CLocale.setlocale(CLocale::LC_COLLATE, '')
return Core.new(@opts).ls if @args.empty?
@args = [Dir.pwd] if @args.empty?
@args.sort!.each_with_index do |path, i|
next STDERR.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path)
puts '' if i > 0
puts "#{path}:" if Dir.exist?(path) && @args.size > 1
Core.new(path, @opts).ls
begin
next STDERR.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path)
puts '' if i > 0
puts "\n#{path}:" if Dir.exist?(path) && @args.size > 1
Core.new(path, @opts).ls
rescue SystemCallError => e
STDERR.puts "#{path}: #{e}".colorize(:red)
end
end
end