Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add --no-hardlinks option #499

Merged
merged 6 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 11 additions & 9 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
30 changes: 22 additions & 8 deletions lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/checks
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
4 changes: 2 additions & 2 deletions test/run
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down