From 77cf0b077092af167c2a58d3452e880060c3b3d6 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 7 Jan 2023 10:36:26 +0900 Subject: [PATCH 1/2] add '-H' option --- lib/colorls/core.rb | 15 ++++++++++++++- lib/colorls/flags.rb | 28 +++++++++++++++++++++------- spec/color_ls/flags_spec.rb | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 7448e830..ff02b4ad 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..1d2b3321 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('-H', '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 33d3c927..6f618cdf 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 -H flag in a listing format' do + let(:args) { ['-l', '-H', "#{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 From cbad7578a9b668fdc6551270a9cb187d73df3168 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 11 Feb 2023 13:39:46 +0900 Subject: [PATCH 2/2] change the option name from '-H' to '-L' --- lib/colorls/flags.rb | 2 +- spec/color_ls/flags_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 1d2b3321..4f5f5366 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -218,7 +218,7 @@ def set_long_style_user_and_group_options(options, long_style_options) end def get_long_style_symlink_options(options, long_style_options) - options.on('-H', 'show information on the destination of symbolic links') do + options.on('-L', 'show information on the destination of symbolic links') do long_style_options[:show_symbol_dest] = true end long_style_options diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index 326e489f..06779539 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -614,8 +614,8 @@ end end - context 'with -H flag in a listing format' do - let(:args) { ['-l', '-H', "#{FIXTURES}/a.txt"] } + context 'with -L flag in a listing format' do + let(:args) { ['-l', '-L', "#{FIXTURES}/a.txt"] } before do file_info = instance_double(