diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 7ffb55b1..e2bd7f53 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -27,8 +27,8 @@ def self.screen_width class Core def initialize(all: false, sort: false, show: false, mode: nil, show_git: false, almost_all: false, colors: [], group: nil, - reverse: false, hyperlink: false, tree_depth: nil, show_inode: false, show_group: true, show_user: true, - indicator_style: 'slash', time_style: '') + reverse: false, hyperlink: false, tree_depth: nil, show_inode: false, + indicator_style: 'slash', long_style_options: {}) @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @all = all @almost_all = almost_all @@ -39,13 +39,13 @@ def initialize(all: false, sort: false, show: false, @show = show @one_per_line = mode == :one_per_line @show_inode = show_inode - init_long_format(mode,show_group,show_user) + init_long_format(mode,long_style_options) @tree = {mode: mode == :tree, depth: tree_depth} @horizontal = mode == :horizontal - @show_git = show_git @git_status = init_git_status(show_git) - @time_style = time_style + @time_style = long_style_options.key?(:time_style) ? long_style_options[:time_style] : '' @indicator_style = indicator_style + @hard_links_count = long_style_options.key?(:hard_links_count) ? long_style_options[:hard_links_count] : true # how much characters an item occupies besides its name @additional_chars_per_item = 12 + (@show_git ? 4 : 0) + (@show_inode ? 10 : 0) @@ -133,13 +133,14 @@ def init_colors(colors) end end - def init_long_format(mode, show_group, show_user) + def init_long_format(mode, long_style_options) @long = mode == :long - @show_group = show_group - @show_user = show_user + @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 end def init_git_status(show_git) + @show_git = show_git return {}.freeze unless show_git # stores git status information per directory @@ -310,7 +311,8 @@ def long_info(content) 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 0a84fbc2..f37d6cee 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -106,10 +106,8 @@ def default_opts colors: [], tree_depth: 3, show_inode: false, - show_group: true, - show_user: true, indicator_style: 'slash', - time_style: '' + long_style_options: {} } end @@ -178,20 +176,36 @@ def add_format_options(options) options.on('-C', 'list entries by columns instead of by lines') { @opts[:mode] = :vertical } end + def default_long_style_options + { + show_group: true, + show_user: true, + time_style: '', + hard_links_count: true + } + end + def add_long_style_options(options) - options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long } + long_style_options = default_long_style_options + options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long } options.on('-o', 'use a long listing format without group information') do @opts[:mode] = :long - @opts[:show_group] = false + long_style_options[:show_group] = false end options.on('-g', 'use a long listing format without owner information') do @opts[:mode] = :long - @opts[:show_user] = false + long_style_options[:show_user] = false + end + options.on('-G', '--no-group', 'show no group information in a long listing') do + long_style_options[:show_group] = false end - options.on('-G', '--no-group', 'show no group information in a long listing') { @opts[:show_group] = false } options.on('--time-style=FORMAT', String, 'use time display format') do |time_style| - @opts[:time_style] = 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 + @opts[: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 129696e0..389e5570 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -577,4 +577,44 @@ 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 diff --git a/test/checks b/test/checks index ee0b7f01..4820e693 100644 --- a/test/checks +++ b/test/checks @@ -38,6 +38,7 @@ OK colorls --report OK colorls --report=long OK colorls --report=short OK colorls --inode +OK colorls -l --no-hardlinks LC_ALL=C OK colorls spec/fixtures/ LC_ALL=C OK colorls --git spec/fixtures/ diff --git a/test/run b/test/run index 08c6db92..a821be69 100755 --- a/test/run +++ b/test/run @@ -94,10 +94,10 @@ function OUT() { function summary() { if [[ $ERRORS -gt 0 ]]; then - printf '\n\n %d of %d tests failed.' "$ERRORS" "$TESTS" >&2 + printf '\n\n %d of %d tests failed.\n\n' "$ERRORS" "$TESTS" >&2 exit 1 else - printf '\n\n %d tests passed.' "$TESTS" >&2 + printf '\n\n %d tests passed.\n\n' "$TESTS" >&2 fi }