diff --git a/.rubocop.yml b/.rubocop.yml index 19b7d95..607eb78 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -58,7 +58,7 @@ Metrics/ClassLength: Max: 350 Metrics/ParameterLists: - Max: 15 + Max: 16 Naming/FileName: Enabled: false diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 49673bc..7ffb55b 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -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 diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 2a64973..0a84fbc 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -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 diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index a379b56..37f4510 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -36,6 +36,7 @@ tree: cyan empty: yellow error: red normal: darkkhaki +inode: moccasin # Git addition: chartreuse diff --git a/lib/yaml/light_colors.yaml b/lib/yaml/light_colors.yaml index 7df1fc0..dff3283 100644 --- a/lib/yaml/light_colors.yaml +++ b/lib/yaml/light_colors.yaml @@ -36,6 +36,7 @@ tree: cyan empty: yellow error: red normal: black +inode: black # Git addition: chartreuse diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index 99d76f7..c0045ae 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -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'] } diff --git a/test/checks b/test/checks index 0714f5a..ee0b7f0 100644 --- a/test/checks +++ b/test/checks @@ -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/