Skip to content
Open
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
37 changes: 35 additions & 2 deletions lib/colorls/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ def self.screen_width
class Core # rubocop:disable Metrics/ClassLength
MIN_SIZE_CHARS = 4

# rubocop:disable Metrics/MethodLength
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,
indicator_style: 'slash', long_style_options: {}, icons: true)
indicator_style: 'slash', long_style_options: {}, icons: true, recursive: false)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@almost_all = almost_all
Expand All @@ -49,15 +50,18 @@ def initialize(all: false, sort: false, show: false,
@indicator_style = indicator_style
@hard_links_count = long_style_options.key?(:hard_links_count) ? long_style_options[:hard_links_count] : true
@icons = icons
@recursive = recursive

init_colors colors
init_icons
end
# rubocop:enable Metrics/MethodLength

def additional_chars_per_item
12 + (@show_git ? 4 : 0) + (@show_inode ? 10 : 0)
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def ls_dir(info)
if @tree[:mode]
print "\n"
Expand All @@ -77,7 +81,18 @@ def ls_dir(info)
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?

ls

return unless @recursive

@contents.each do |content|
next unless content.directory?
next if content.name == '.' || content.name == '..'

puts "\n#{content.path}:"
ls_dir(content)
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def ls_files(files)
@contents = files
Expand Down Expand Up @@ -379,7 +394,7 @@ def fetch_string(content, key, color, increment)
value = increment == :folders ? @folders[key] : @files[key]
logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..].to_i(16)].pack('U') }
name = @hyperlink ? make_link(content) : content.show
name += content.directory? && @indicator_style != 'none' ? '/' : ' '
name += get_indicator(content)
entry = @icons ? "#{out_encode(logo)} #{out_encode(name)}" : out_encode(name).to_s
entry = entry.bright if !content.directory? && content.executable?

Expand Down Expand Up @@ -484,5 +499,23 @@ def make_link(content)
uri = Addressable::URI.convert_path(File.absolute_path(content.path))
"\033]8;;#{uri}\007#{content.show}\033]8;;\007"
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def get_indicator(content)
return '' if @indicator_style == 'none'

if @indicator_style == :classify
return '/' if content.directory?
return '@' if content.symlink?
return '*' if content.executable?
return '|' if content.stats.pipe?
return '=' if content.stats.socket?

return ' '
end

content.directory? ? '/' : ' '
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end
end
2 changes: 1 addition & 1 deletion lib/colorls/fileinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def to_s
end

def_delegators :@stats, :directory?, :socket?, :chardev?, :symlink?, :blockdev?, :mtime, :nlink, :size, :owned?,
:executable?
:executable?, :pipe?

private

Expand Down
10 changes: 9 additions & 1 deletion lib/colorls/flags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def default_opts
tree_depth: 3,
show_inode: false,
indicator_style: 'slash',
recursive: false,
long_style_options: {}
}
end
Expand Down Expand Up @@ -136,25 +137,32 @@ def add_sort_options(options)
options.on('-r', '--reverse', 'reverse order while sorting') { @opts[:reverse] = true }
end

# rubocop:disable Metrics/MethodLength
def add_common_options(options)
options.on('-a', '--all', 'do not ignore entries starting with .') { @opts[:all] = true }
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[:show_git] = true }
options.on('-p', 'append / indicator to directories') { @opts[:indicator_style] = 'slash' }
options.on('-F', '--classify', 'append indicator (one of */=>@|) to entries') do
@opts[:indicator_style] = :classify
end
options.on('-R', '--recursive', 'list subdirectories recursively') { @opts[:recursive] = true }
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
end
options.on(
'--indicator-style=[STYLE]',
%w[none slash], 'append indicator with style STYLE to entry names: none, slash (-p) (default)'
%w[none slash classify],
'append indicator with style STYLE to entry names: none, slash (-p) (default), classify (-F)'
) do |style|
@opts[:indicator_style] = style
end
end
# rubocop:enable Metrics/MethodLength

def add_format_options(options)
options.on(
Expand Down
48 changes: 48 additions & 0 deletions spec/color_ls/flags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,52 @@
expect { subject }.to output(/#{args.first}/).to_stdout
end
end

context 'with -F/--classify flag' do
let(:args) { ['-F', '-1', FIXTURES] }

it 'appends / to directories' do
expect { subject }.to output(/symlinks\//).to_stdout
end
end

context 'with -F flag on symlinks directory' do
let(:args) { ['-F', '-1', File.join(FIXTURES, 'symlinks')] }

it 'appends @ to symlinks' do
expect { subject }.to output(/ReadmeLink.md@/).to_stdout
end
end

context 'with --indicator-style=classify' do
let(:args) { ['--indicator-style=classify', '-1', FIXTURES] }

it 'appends / to directories' do
expect { subject }.to output(/symlinks\//).to_stdout
end
end

context 'with -R/--recursive flag' do
let(:args) { ['-R', FIXTURES] }

it 'lists subdirectories recursively' do
expect { subject }.to output(/second-level:/).to_stdout
end

it 'lists files in subdirectories' do
expect { subject }.to output(/third-level-file.txt/).to_stdout
end
end

context 'with -FR flags combined' do
let(:args) { ['-FR', '-1', FIXTURES] }

it 'applies both classify and recursive' do
expect { subject }.to output(/symlinks\//).to_stdout
end

it 'shows recursive content' do
expect { subject }.to output(/third-level-file.txt/).to_stdout
end
end
end