Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add '-L' option #565

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
28 changes: 21 additions & 7 deletions lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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