show relative filepath when argument is file

This commit is contained in:
Ayush Poddar 2023-04-29 20:15:50 +05:30
parent d1e28edcd0
commit 688188de6f
2 changed files with 23 additions and 9 deletions

View file

@ -11,20 +11,21 @@ module ColorLS
attr_reader :stats, :name, :path, :parent attr_reader :stats, :name, :path, :parent
def initialize(name:, parent:, path: nil, link_info: true) def initialize(name:, parent:, path: nil, link_info: true, show_filepath: false)
@name = name @name = name
@parent = parent @parent = parent
@path = path.nil? ? File.join(parent, name) : +path @path = path.nil? ? File.join(parent, name) : +path
@stats = File.lstat(@path) @stats = File.lstat(@path)
@show_name = nil
@path.force_encoding(ColorLS.file_encoding) @path.force_encoding(ColorLS.file_encoding)
handle_symlink(@path) if link_info && @stats.symlink? handle_symlink(@path) if link_info && @stats.symlink?
set_show_name(use_path: show_filepath)
end end
def self.info(path, link_info: true) def self.info(path, link_info: true, show_filepath: false)
FileInfo.new(name: File.basename(path), parent: File.dirname(path), path: path, link_info: link_info) FileInfo.new(name: File.basename(path), parent: File.dirname(path), path: path, link_info: link_info,
show_filepath: show_filepath)
end end
def self.dir_entry(dir, child, link_info: true) def self.dir_entry(dir, child, link_info: true)
@ -32,10 +33,7 @@ module ColorLS
end end
def show def show
return @show_name unless @show_name.nil? @show_name
@show_name = @name.encode(Encoding.find('filesystem'), Encoding.default_external,
invalid: :replace, undef: :replace)
end end
def dead? def dead?
@ -80,5 +78,21 @@ module ColorLS
rescue SystemCallError => e rescue SystemCallError => e
$stderr.puts "cannot read symbolic link: #{e}" $stderr.puts "cannot read symbolic link: #{e}"
end end
def show_basename
@name.encode(Encoding.find('filesystem'), Encoding.default_external,
invalid: :replace, undef: :replace)
end
def show_relative_path
@path.encode(Encoding.find('filesystem'), Encoding.default_external,
invalid: :replace, undef: :replace)
end
def set_show_name(use_path: false)
@show_name = show_basename unless use_path
@show_name = show_basename if directory?
@show_name = show_relative_path if use_path
end
end end
end end

View file

@ -58,7 +58,7 @@ module ColorLS
def group_files_and_directories def group_files_and_directories
infos = @args.flat_map do |arg| infos = @args.flat_map do |arg|
FileInfo.info(arg) FileInfo.info(arg, show_filepath: true)
rescue Errno::ENOENT rescue Errno::ENOENT
$stderr.puts "colorls: Specified path '#{arg}' doesn't exist.".colorize(:red) $stderr.puts "colorls: Specified path '#{arg}' doesn't exist.".colorize(:red)
@exit_status_code = 2 @exit_status_code = 2