From ced1cfebfffd841336daf34c0eec0281dd8c9f91 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 19 Feb 2022 09:22:36 +0900 Subject: [PATCH 1/5] 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 aea56d69..f9c5298e 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -28,7 +28,7 @@ class Core 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 @@ def initialize(all: false, sort: false, show: false, @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 @@ 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 ea93855d..ae666e7d 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -108,7 +108,8 @@ def default_opts show_group: true, show_user: true, indicator_style: 'slash', - time_style: '' + time_style: '', + hard_links_count: true } end @@ -187,6 +188,7 @@ def add_long_style_options(options) 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 d45efd29..8752e632 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -558,4 +558,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 From ba547f299d5c3a8bbdd4f44994b0a88d23646070 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 19 Feb 2022 11:15:43 +0900 Subject: [PATCH 2/5] modify the code according to rubocop --- lib/colorls/core.rb | 16 ++++++++-------- lib/colorls/flags.rb | 32 ++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index f9c5298e..bd17f9dc 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, 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: '', hard_links_count: true) + reverse: false, hyperlink: false, tree_depth: nil, + indicator_style: 'slash', long_style_options: {}) @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @all = all @almost_all = almost_all @@ -38,13 +38,13 @@ def initialize(all: false, sort: false, show: false, @group = group @show = show @one_per_line = mode == :one_per_line - 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 @git_status = init_git_status(git_status) - @time_style = time_style + @time_style = long_style_options.key?(:time_style) ? long_style_options[:time_style] : '' @indicator_style = indicator_style - @hard_links_count = hard_links_count + @hard_links_count = long_style_options.key?(:hard_links_count) ? long_style_options[:hard_links_count] : true init_colors colors @@ -122,10 +122,10 @@ 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) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index ae666e7d..103df9b3 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -105,11 +105,8 @@ def default_opts git_status: false, colors: [], tree_depth: 3, - show_group: true, - show_user: true, indicator_style: 'slash', - time_style: '', - hard_links_count: true + long_style_options: {} } end @@ -174,21 +171,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 - options.on('--no-hardlinks', 'show no hard links count in a long listing') { @opts[:hard_links_count] = false } + @opts[:long_style_options] = long_style_options end def add_general_options(options) From 462fa61bc5337a1f6c89fdff757ed1f41813d8a2 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 26 Mar 2022 16:32:54 +0900 Subject: [PATCH 3/5] fix rubocop error --- lib/colorls/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 9f46d8ff..e2bd7f53 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -42,7 +42,6 @@ def initialize(all: false, sort: false, show: false, 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 = long_style_options.key?(:time_style) ? long_style_options[:time_style] : '' @indicator_style = indicator_style @@ -141,6 +140,7 @@ def init_long_format(mode, long_style_options) end def init_git_status(show_git) + @show_git = show_git return {}.freeze unless show_git # stores git status information per directory From 5a62fedc1a783f07f927264b855796606c3a17c8 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Sun, 27 Mar 2022 19:09:26 +0200 Subject: [PATCH 4/5] Print newlines after test summary --- test/run | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 } From a7cb1cc3857e0596d18a478d92b2fc7ec9edb974 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Sun, 27 Mar 2022 19:10:12 +0200 Subject: [PATCH 5/5] Add `colorls -l --no-hardlinks` call to checks --- test/checks | 1 + 1 file changed, 1 insertion(+) 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/