add '-H' option

This commit is contained in:
t-mangoe 2023-01-07 10:36:26 +09:00
parent 574a055dd5
commit 77cf0b0770
3 changed files with 72 additions and 8 deletions

View file

@ -139,6 +139,7 @@ module ColorLS
@long = mode == :long @long = mode == :long
@show_group = long_style_options.key?(:show_group) ? long_style_options[:show_group] : true @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_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 end
def init_git_status(show_git) def init_git_status(show_git)
@ -333,6 +334,15 @@ module ColorLS
end end
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) def out_encode(str)
str.encode(Encoding.default_external, undef: :replace, replace: '') str.encode(Encoding.default_external, undef: :replace, replace: '')
end end
@ -346,7 +356,10 @@ module ColorLS
entry = @icons ? "#{out_encode(logo)} #{out_encode(name)}" : out_encode(name).to_s entry = @icons ? "#{out_encode(logo)} #{out_encode(name)}" : out_encode(name).to_s
entry = entry.bright if !content.directory? && content.executable? 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 end
def ls_line(chunk, widths) def ls_line(chunk, widths)

View file

@ -183,13 +183,26 @@ module ColorLS
show_group: true, show_group: true,
show_user: true, show_user: true,
time_style: '', time_style: '',
hard_links_count: true hard_links_count: true,
show_symbol_dest: false
} }
end end
def add_long_style_options(options) def add_long_style_options(options)
long_style_options = default_long_style_options long_style_options = default_long_style_options
options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long } 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 options.on('-o', 'use a long listing format without group information') do
@opts[:mode] = :long @opts[:mode] = :long
long_style_options[:show_group] = false long_style_options[:show_group] = false
@ -201,13 +214,14 @@ module ColorLS
options.on('-G', '--no-group', 'show no group information in a long listing') do options.on('-G', '--no-group', 'show no group information in a long listing') do
long_style_options[:show_group] = false long_style_options[:show_group] = false
end end
options.on('--time-style=FORMAT', String, 'use time display format') do |time_style| long_style_options
long_style_options[:time_style] = time_style 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 end
options.on('--no-hardlinks', 'show no hard links count in a long listing') do long_style_options
long_style_options[:hard_links_count] = false
end
@opts[:long_style_options] = long_style_options
end end
def add_general_options(options) def add_general_options(options)

View file

@ -613,4 +613,41 @@ RSpec.describe ColorLS::Flags do
expect { subject }.not_to output(/987/).to_stdout expect { subject }.not_to output(/987/).to_stdout
end end
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 end