diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index aa6a09b1..68acc70f 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -139,6 +139,7 @@ def init_long_format(mode, long_style_options) @long = mode == :long @show_group = long_style_options.key?(:show_group) ? long_style_options[:show_group] : true @show_user = long_style_options.key?(:show_user) ? long_style_options[:show_user] : true + @show_symbol_dest = long_style_options.key?(:show_symbol_dest) ? long_style_options[:show_symbol_dest] : false end def init_git_status(show_git) @@ -333,6 +334,15 @@ def symlink_info(content) end end + def update_content_if_show_symbol_dest(content, show_symbol_dest_flag) + return content unless show_symbol_dest_flag + return content unless content.symlink? + return content if content.link_target.nil? + return content if content.dead? + + FileInfo.info(content.link_target) + end + def out_encode(str) str.encode(Encoding.default_external, undef: :replace, replace: '') end @@ -346,7 +356,10 @@ def fetch_string(content, key, color, increment) entry = @icons ? "#{out_encode(logo)} #{out_encode(name)}" : out_encode(name).to_s entry = entry.bright if !content.directory? && content.executable? - "#{inode(content)} #{long_info(content)} #{git_info(content)} #{entry.colorize(color)}#{symlink_info(content)}" + symlink_info_string = symlink_info(content) + content = update_content_if_show_symbol_dest(content,@show_symbol_dest) + + "#{inode(content)} #{long_info(content)} #{git_info(content)} #{entry.colorize(color)}#{symlink_info_string}" end def ls_line(chunk, widths) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 41b288c6..4f5f5366 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -183,13 +183,26 @@ def default_long_style_options show_group: true, show_user: true, time_style: '', - hard_links_count: true + hard_links_count: true, + show_symbol_dest: false } end def add_long_style_options(options) long_style_options = default_long_style_options options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long } + long_style_options = set_long_style_user_and_group_options(options, long_style_options) + options.on('--time-style=FORMAT', String, 'use time display format') do |time_style| + long_style_options[:time_style] = time_style + end + options.on('--no-hardlinks', 'show no hard links count in a long listing') do + long_style_options[:hard_links_count] = false + end + long_style_options = get_long_style_symlink_options(options, long_style_options) + @opts[:long_style_options] = long_style_options + end + + def set_long_style_user_and_group_options(options, long_style_options) options.on('-o', 'use a long listing format without group information') do @opts[:mode] = :long long_style_options[:show_group] = false @@ -201,13 +214,14 @@ def add_long_style_options(options) options.on('-G', '--no-group', 'show no group information in a long listing') do long_style_options[:show_group] = false end - options.on('--time-style=FORMAT', String, 'use time display format') do |time_style| - long_style_options[:time_style] = time_style - end - options.on('--no-hardlinks', 'show no hard links count in a long listing') do - long_style_options[:hard_links_count] = false + long_style_options + end + + def get_long_style_symlink_options(options, long_style_options) + options.on('-L', 'show information on the destination of symbolic links') do + long_style_options[:show_symbol_dest] = true end - @opts[:long_style_options] = long_style_options + long_style_options end def add_general_options(options) diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index d4e8992a..06779539 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -613,4 +613,41 @@ expect { subject }.not_to output(/987/).to_stdout end end + + context 'with -L flag in a listing format' do + let(:args) { ['-l', '-L', "#{FIXTURES}/a.txt"] } + + before do + file_info = instance_double( + ColorLS::FileInfo, + group: 'sys', + mtime: Time.now, + directory?: false, + owner: 'user', + name: 'a.txt', + show: 'a.txt', + nlink: 1, + size: 128, + blockdev?: false, + chardev?: false, + socket?: false, + symlink?: true, + link_target: "#{FIXTURES}/z.txt", + dead?: false, + executable?: false + ) + + allow(ColorLS::FileInfo).to receive(:new).and_call_original + allow(ColorLS::FileInfo).to receive(:new).with( + path: File.join(FIXTURES, 'a.txt'), + parent: FIXTURES, + name: 'a.txt', + link_info: true + ) { file_info } + end + + it 'show information on the destination of symbolic links' do + expect { subject }.not_to output(/128/).to_stdout + end + end end