From 1565333cb8876a8c803bb7462b110d1833b0457f Mon Sep 17 00:00:00 2001 From: Oskar Date: Wed, 12 Jul 2017 16:45:23 +0200 Subject: [PATCH] Adds symlink display for -l (#77) --- .travis.yml | 2 ++ lib/colorls/core.rb | 33 +++++++++++++++++---- spec/fixtures/symlinks/ReadmeLink.md | 1 + spec/fixtures/symlinks/Supportlink | 1 + spec/fixtures/symlinks/doesnotexisttest.txt | 1 + 5 files changed, 32 insertions(+), 6 deletions(-) create mode 120000 spec/fixtures/symlinks/ReadmeLink.md create mode 120000 spec/fixtures/symlinks/Supportlink create mode 120000 spec/fixtures/symlinks/doesnotexisttest.txt diff --git a/.travis.yml b/.travis.yml index 3039473..b2c5b3a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,8 @@ script: - colorls -1 - colorls -r - colorls -l + - colorls spec/fixtures/symlinks + - colorls -l spec/fixtures/symlinks - colorls README.md - colorls * - colorls | grep / diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index f1b6f65..d150be5 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -59,6 +59,7 @@ module ColorLS def init_user_lengths @userlength = @contents.map do |c| + next 0 unless File.exist?("#{@input}/#{c}") begin user = Etc.getpwuid(File.stat("#{@input}/#{c}").uid).name rescue ArgumentError @@ -70,6 +71,7 @@ module ColorLS def init_group_lengths @grouplength = @contents.map do |c| + next 0 unless File.exist?("#{@input}/#{c}") begin group = Etc.getgrgid(File.stat("#{@input}/#{c}").gid).name rescue ArgumentError @@ -141,7 +143,7 @@ module ColorLS end def in_line(chunk_size) - return false if @max_widths.sum + 6 * chunk_size > @screen_width + return false if @max_widths.sum + 7 * chunk_size > @screen_width true end @@ -205,8 +207,25 @@ module ColorLS end def long_info(content) + return '' unless @long + unless File.exist?("#{@input}/#{content}") + return '[No Info]'.colorize(:red) + ' ' * (39 + @userlength + @grouplength) + end stat = File.stat("#{@input}/#{content}") - "#{mode_info(stat)} #{user_info(stat)} #{group_info(stat)} #{size_info(stat)} #{mtime_info(stat)} " + [mode_info(stat), user_info(stat), group_info(stat), size_info(stat), mtime_info(stat)].join(' ') + end + + def symlink_info(content) + return '' unless @long && File.lstat("#{@input}/#{content}").symlink? + if File.exist?("#{@input}/#{content}") + " ⇒ #{File.readlink("#{@input}/#{content}")}/".colorize(:cyan) + else + ' ⇒ [Dead link]'.colorize(:red) + end + end + + def slash?(content) + Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' ' end def fetch_string(content, key, color, increment) @@ -214,7 +233,11 @@ module ColorLS value = increment == :folders ? @folders[key] : @files[key] logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } - "#{@long ? long_info(content) : ''} #{logo.colorize(color)} #{content.colorize(color)}" + [ + long_info(content), + logo.colorize(color), + "#{content.colorize(color)}#{slash?(content)}#{symlink_info(content)}" + ].join(' ') end def load_from_yaml(filename, aliase=false) @@ -231,10 +254,8 @@ module ColorLS print "\n" chunk.each_with_index do |content, i| break if content.empty? - print " #{fetch_string(content, *options(content))}" - print Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' ' - print ' ' * (@max_widths[i] - content.length) + print ' ' * (@max_widths[i] - content.length) unless @one_per_line || @long end end diff --git a/spec/fixtures/symlinks/ReadmeLink.md b/spec/fixtures/symlinks/ReadmeLink.md new file mode 120000 index 0000000..8a33348 --- /dev/null +++ b/spec/fixtures/symlinks/ReadmeLink.md @@ -0,0 +1 @@ +../../../README.md \ No newline at end of file diff --git a/spec/fixtures/symlinks/Supportlink b/spec/fixtures/symlinks/Supportlink new file mode 120000 index 0000000..321fcb5 --- /dev/null +++ b/spec/fixtures/symlinks/Supportlink @@ -0,0 +1 @@ +../../support \ No newline at end of file diff --git a/spec/fixtures/symlinks/doesnotexisttest.txt b/spec/fixtures/symlinks/doesnotexisttest.txt new file mode 120000 index 0000000..96a60b9 --- /dev/null +++ b/spec/fixtures/symlinks/doesnotexisttest.txt @@ -0,0 +1 @@ +thisfiledoesnotexist \ No newline at end of file