From ced1cfebfffd841336daf34c0eec0281dd8c9f91 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 19 Feb 2022 09:22:36 +0900 Subject: [PATCH] add --no-hardlinks option --- lib/colorls/core.rb | 6 ++++-- lib/colorls/flags.rb | 4 +++- spec/color_ls/flags_spec.rb | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index aea56d6..f9c5298 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -28,7 +28,7 @@ module ColorLS def initialize(all: false, sort: false, show: false, mode: nil, git_status: false, almost_all: false, colors: [], group: nil, reverse: false, hyperlink: false, tree_depth: nil, show_group: true, show_user: true, - indicator_style: 'slash', time_style: '') + indicator_style: 'slash', time_style: '', hard_links_count: true) @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @all = all @almost_all = almost_all @@ -44,6 +44,7 @@ module ColorLS @git_status = init_git_status(git_status) @time_style = time_style @indicator_style = indicator_style + @hard_links_count = hard_links_count init_colors colors @@ -295,7 +296,8 @@ module ColorLS links = content.nlink.to_s.rjust(@linklength) - line_array = [mode_info(content.stats), links] + line_array = [mode_info(content.stats)] + line_array.push links if @hard_links_count line_array.push user_info(content) if @show_user line_array.push group_info(content.group) if @show_group line_array.concat [size_info(content.size), mtime_info(content.mtime)] diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index ea93855..ae666e7 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -108,7 +108,8 @@ module ColorLS show_group: true, show_user: true, indicator_style: 'slash', - time_style: '' + time_style: '', + hard_links_count: true } end @@ -187,6 +188,7 @@ module ColorLS options.on('--time-style=FORMAT', String, 'use time display format') do |time_style| @opts[:time_style] = time_style end + options.on('--no-hardlinks', 'show no hard links count in a long listing') { @opts[:hard_links_count] = false } end def add_general_options(options) diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index d45efd2..8752e63 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -558,4 +558,44 @@ RSpec.describe ColorLS::Flags do it { expect { subject }.to output(/#{mtime.strftime("%y-%m-%d %k:%M")}/).to_stdout } end + + context 'with --no-hardlinks flag in a listing format' do + let(:args) { ['-l', '--no-hardlink', "#{FIXTURES}/a.txt"] } + + before do + file_info = instance_double( + 'FileInfo', + group: 'sys', + mtime: Time.now, + directory?: false, + owner: 'user', + name: 'a.txt', + show: 'a.txt', + nlink: 987, + size: 128, + blockdev?: false, + chardev?: false, + socket?: false, + symlink?: false, + stats: OpenStruct.new( + mode: 0o444, # read for user, owner, other + setuid?: true, + setgid?: true, + sticky?: true + ), + executable?: false + ) + + 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 'lists without hard links count' do + expect { subject }.not_to output(/987/).to_stdout + end + end end