Merge pull request #506 from goar5670/add-inode

This commit is contained in:
Claudio Bley 2022-03-20 22:20:17 +01:00 committed by GitHub
commit 092e25a83b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 12 deletions

View file

@ -58,7 +58,7 @@ Metrics/ClassLength:
Max: 350
Metrics/ParameterLists:
Max: 15
Max: 16
Naming/FileName:
Enabled: false

View file

@ -26,8 +26,8 @@ module ColorLS
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,
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: '')
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@ -38,12 +38,16 @@ module ColorLS
@group = group
@show = show
@one_per_line = mode == :one_per_line
@show_inode = show_inode
init_long_format(mode,show_group,show_user)
@tree = {mode: mode == :tree, depth: tree_depth}
@horizontal = mode == :horizontal
@git_status = init_git_status(git_status)
@show_git = show_git
@git_status = init_git_status(show_git)
@time_style = time_style
@indicator_style = indicator_style
# how much characters an item occupies besides its name
@additional_chars_per_item = 12 + (@show_git ? 4 : 0) + (@show_inode ? 10 : 0)
init_colors colors
@ -149,11 +153,8 @@ module ColorLS
end
end
# how much characters an item occupies besides its name
CHARS_PER_ITEM = 12
def item_widths
@contents.map { |item| Unicode::DisplayWidth.of(item.show) + CHARS_PER_ITEM }
@contents.map { |item| Unicode::DisplayWidth.of(item.show) + @additional_chars_per_item }
end
def filter_hidden_contents
@ -298,6 +299,12 @@ module ColorLS
end
end
def inode(content)
return '' unless @show_inode
content.stats.ino.to_s.rjust(10).colorize(@colors[:inode])
end
def long_info(content)
return '' unless @long
@ -336,7 +343,7 @@ module ColorLS
entry = "#{out_encode(logo)} #{out_encode(name)}"
entry = entry.bright if !content.directory? && content.executable?
"#{long_info(content)} #{git_info(content)} #{entry.colorize(color)}#{symlink_info(content)}"
"#{inode(content)} #{long_info(content)} #{git_info(content)} #{entry.colorize(color)}#{symlink_info(content)}"
end
def ls_line(chunk, widths)
@ -346,7 +353,7 @@ module ColorLS
entry = fetch_string(content, *options(content))
line << (' ' * padding)
line << ' ' << entry.encode(Encoding.default_external, undef: :replace)
padding = widths[i] - Unicode::DisplayWidth.of(content.show) - CHARS_PER_ITEM
padding = widths[i] - Unicode::DisplayWidth.of(content.show) - @additional_chars_per_item
end
print line << "\n"
end

View file

@ -102,9 +102,10 @@ module ColorLS
mode: STDOUT.tty? ? :vertical : :one_per_line, # rubocop:disable Style/GlobalStdStream
all: false,
almost_all: false,
git_status: false,
show_git: false,
colors: [],
tree_depth: 3,
show_inode: false,
show_group: true,
show_user: true,
indicator_style: 'slash',
@ -141,8 +142,9 @@ module ColorLS
options.on('-A', '--almost-all', 'do not list . and ..') { @opts[:almost_all] = true }
options.on('-d', '--dirs', 'show only directories') { @opts[:show] = :dirs }
options.on('-f', '--files', 'show only files') { @opts[:show] = :files }
options.on('--gs', '--git-status', 'show git status for each file') { @opts[:git_status] = true }
options.on('--gs', '--git-status', 'show git status for each file') { @opts[:show_git] = true }
options.on('-p', 'append / indicator to directories') { @opts[:indicator_style] = 'slash' }
options.on('-i', '--inode', 'show inode number') { @opts[:show_inode] = true }
options.on('--report=[WORD]', %w[short long], 'show report: short, long (default if omitted)') do |word|
word ||= :long
@report_mode = word.to_sym

View file

@ -36,6 +36,7 @@ tree: cyan
empty: yellow
error: red
normal: darkkhaki
inode: moccasin
# Git
addition: chartreuse

View file

@ -36,6 +36,7 @@ tree: cyan
empty: yellow
error: red
normal: black
inode: black
# Git
addition: chartreuse

View file

@ -371,6 +371,14 @@ RSpec.describe ColorLS::Flags do
end
end
context 'with --inode flag' do
let(:args) { ['--inode', '-i', 'show inode number', FIXTURES] }
it 'shows inode number' do
expect { subject }.to output(/\d{7,}/).to_stdout
end
end
context 'with non-existent path' do
let(:args) { ['not_exist_file'] }

View file

@ -37,6 +37,7 @@ OK colorls --tree=1
OK colorls --report
OK colorls --report=long
OK colorls --report=short
OK colorls --inode
LC_ALL=C OK colorls spec/fixtures/
LC_ALL=C OK colorls --git spec/fixtures/