Skip to content

Commit

Permalink
Merge pull request #506 from goar5670/add-inode
Browse files Browse the repository at this point in the history
  • Loading branch information
avdv committed Mar 20, 2022
2 parents 697577c + cd394c0 commit 092e25a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Metrics/ClassLength:
Max: 350

Metrics/ParameterLists:
Max: 15
Max: 16

Naming/FileName:
Enabled: false
Expand Down
25 changes: 16 additions & 9 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,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,
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
Expand All @@ -38,12 +38,16 @@ def initialize(all: false, sort: false, show: false,
@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

Expand Down Expand Up @@ -149,11 +153,8 @@ def init_git_status(show_git)
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
Expand Down Expand Up @@ -298,6 +299,12 @@ def git_dir_info(content, status)
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

Expand Down Expand Up @@ -336,7 +343,7 @@ def fetch_string(content, key, color, increment)
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)
Expand All @@ -346,7 +353,7 @@ def ls_line(chunk, widths)
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
Expand Down
6 changes: 4 additions & 2 deletions lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ def default_opts
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',
Expand Down Expand Up @@ -141,8 +142,9 @@ def add_common_options(options)
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
Expand Down
1 change: 1 addition & 0 deletions lib/yaml/dark_colors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tree: cyan
empty: yellow
error: red
normal: darkkhaki
inode: moccasin

# Git
addition: chartreuse
Expand Down
1 change: 1 addition & 0 deletions lib/yaml/light_colors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tree: cyan
empty: yellow
error: red
normal: black
inode: black

# Git
addition: chartreuse
Expand Down
8 changes: 8 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@
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'] }

Expand Down
1 change: 1 addition & 0 deletions test/checks
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down

0 comments on commit 092e25a

Please sign in to comment.