From 793cd20cc81c13bfd96f383c9a0e9f10b56bad3c Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 00:17:08 +0300 Subject: [PATCH 1/6] Add basic --inode flag --- lib/colorls/core.rb | 11 ++++++++--- lib/colorls/flags.rb | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 49673bc..8f2e27b 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -26,8 +26,7 @@ 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, + 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,6 +37,7 @@ 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 @@ -298,6 +298,11 @@ module ColorLS end end + def get_inode(content) + return '' unless @show_inode + content.stats.ino.to_s + end + def long_info(content) return '' unless @long @@ -336,7 +341,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)}" + "#{get_inode(content)} #{long_info(content)} #{git_info(content)} #{entry.colorize(color)}#{symlink_info(content)}" end def ls_line(chunk, widths) diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 2a64973..d877afe 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -105,6 +105,7 @@ module ColorLS git_status: false, colors: [], tree_depth: 3, + show_inode: false, show_group: true, show_user: true, indicator_style: 'slash', @@ -143,6 +144,7 @@ module ColorLS 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('-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 From fb102b17000d0749aafb7c2062fc0793206ac7cc Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 00:29:33 +0300 Subject: [PATCH 2/6] Account for git status and inode in item widths --- lib/colorls/core.rb | 12 +++++++++--- lib/colorls/flags.rb | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 8f2e27b..15485ee 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -26,6 +26,7 @@ module ColorLS 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: '') @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @@ -41,7 +42,8 @@ module ColorLS 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 @@ -152,8 +154,12 @@ module ColorLS # how much characters an item occupies besides its name CHARS_PER_ITEM = 12 + def additional_chars + CHARS_PER_ITEM + (@show_git ? 4 : 0) + (@show_inode ? 10 : 0) + end + def item_widths - @contents.map { |item| Unicode::DisplayWidth.of(item.show) + CHARS_PER_ITEM } + @contents.map { |item| Unicode::DisplayWidth.of(item.show) + additional_chars } end def filter_hidden_contents @@ -351,7 +357,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 end print line << "\n" end diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index d877afe..0a84fbc 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -102,7 +102,7 @@ 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, @@ -142,7 +142,7 @@ 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| From 57feb070322a7ed6c746227bdc9134adc152b6ab Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 01:18:26 +0300 Subject: [PATCH 3/6] Add colors for inode text --- lib/colorls/core.rb | 2 +- lib/yaml/dark_colors.yaml | 1 + lib/yaml/light_colors.yaml | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 15485ee..c49ac14 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -306,7 +306,7 @@ module ColorLS def get_inode(content) return '' unless @show_inode - content.stats.ino.to_s + content.stats.ino.to_s.colorize(@colors[:inode]) end def long_info(content) 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 From eb2656a21962095ab173a84eab1f7fb80232a2fc Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 02:16:51 +0300 Subject: [PATCH 4/6] Fix rubocop offenses --- .rubocop.yml | 2 +- lib/colorls/core.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) 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 c49ac14..b9ef205 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -304,8 +304,9 @@ module ColorLS end end - def get_inode(content) + def inode(content) return '' unless @show_inode + content.stats.ino.to_s.colorize(@colors[:inode]) end @@ -347,7 +348,7 @@ module ColorLS entry = "#{out_encode(logo)} #{out_encode(name)}" entry = entry.bright if !content.directory? && content.executable? - "#{get_inode(content)} #{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) From c1469a41967abc473ff9f60abd10e342af5641ea Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 02:31:01 +0300 Subject: [PATCH 5/6] Cover the new --inode flag with tests --- spec/color_ls/flags_spec.rb | 8 ++++++++ test/checks | 1 + 2 files changed, 9 insertions(+) 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/ From cd394c0febd41eaf8fef98f973fb6cf3e97fcb56 Mon Sep 17 00:00:00 2001 From: goar5670 Date: Sun, 20 Mar 2022 21:22:43 +0300 Subject: [PATCH 6/6] Fix different inode number lengths --- lib/colorls/core.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index b9ef205..7ffb55b 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -46,6 +46,8 @@ module ColorLS @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 @@ -151,15 +153,8 @@ module ColorLS end end - # how much characters an item occupies besides its name - CHARS_PER_ITEM = 12 - - def additional_chars - CHARS_PER_ITEM + (@show_git ? 4 : 0) + (@show_inode ? 10 : 0) - end - def item_widths - @contents.map { |item| Unicode::DisplayWidth.of(item.show) + additional_chars } + @contents.map { |item| Unicode::DisplayWidth.of(item.show) + @additional_chars_per_item } end def filter_hidden_contents @@ -307,7 +302,7 @@ module ColorLS def inode(content) return '' unless @show_inode - content.stats.ino.to_s.colorize(@colors[:inode]) + content.stats.ino.to_s.rjust(10).colorize(@colors[:inode]) end def long_info(content) @@ -358,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) - additional_chars + padding = widths[i] - Unicode::DisplayWidth.of(content.show) - @additional_chars_per_item end print line << "\n" end