From d38f444770e35b5b17be87de89b2b3be2cb93670 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Tue, 23 Oct 2018 10:50:22 -0700 Subject: [PATCH 1/8] add custom dir suffix colors --- lib/colorls/core.rb | 11 ++++++++++- lib/colorls/yaml.rb | 15 ++++++++++++++- lib/yaml/dark_colors.yaml | 4 +++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index f4989ed1..6ac14ab8 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -296,7 +296,9 @@ def slash?(content) def fetch_string(path, content, key, color, increment) @count[increment] += 1 value = increment == :folders ? @folders[key] : @files[key] + logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } + name = content.name name = make_link(path, name) if @hyperlink @@ -321,7 +323,14 @@ def ls_line(chunk) def options(content) if content.directory? key = content.name.to_sym - color = @colors[:dir] + + color = if @colors[:dir_suffixes] && content.name[/\.|_/] + suffix = content.name.split(/['.', '_']/).last.to_sym + @colors[:dir_suffixes][suffix] || @colors[:dir] + else + @colors[:dir] + end + return [:folder, color, :folders] unless @all_folders.include?(key) key = @folder_aliases[key] unless @folder_keys.include?(key) return [key, color, :folders] diff --git a/lib/colorls/yaml.rb b/lib/colorls/yaml.rb index 436357a5..b189385e 100644 --- a/lib/colorls/yaml.rb +++ b/lib/colorls/yaml.rb @@ -5,6 +5,19 @@ def initialize(filename) @user_config_filepath = File.join(Dir.home, ".config/colorls/#{filename}") end + def deep_transform_key_vals_in_object(object, &block) + case object + when Hash + object.each_with_object({}) do |(key, value), result| + result[yield(key)] = deep_transform_key_vals_in_object(value, &block) + end + when Array + object.map { |e| deep_transform_key_vals_in_object(e, &block) } + else + yield object + end + end + def load(aliase: false) yaml = read_file(@filepath) if File.exist?(@user_config_filepath) @@ -13,7 +26,7 @@ def load(aliase: false) end return yaml unless aliase - yaml.to_a.map! { |k, v| [k, v.to_sym] }.to_h + deep_transform_key_vals_in_object(yaml.to_a) { |item| item.to_sym }.to_h end def read_file(filepath) diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index e8c24405..b2d54794 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -2,6 +2,8 @@ unrecognized_file: gold recognized_file: lime dir: dodgerblue +dir_suffixes: + venv: green # Link dead_link: red @@ -36,4 +38,4 @@ addition: chartreuse modification: darkkhaki deletion: darkred untracked: darkorange -unchanged: forestgreen \ No newline at end of file +unchanged: forestgreen From c68ce138717ceac2bdcda5d7c24aea1c7ec975d4 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Tue, 23 Oct 2018 11:34:35 -0700 Subject: [PATCH 2/8] remove period dir delimiter until feedback --- lib/colorls/core.rb | 15 +++++++++++++-- lib/colorls/yaml.rb | 15 ++++++++++++++- lib/yaml/dark_colors.yaml | 4 +++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index f4989ed1..450b1dd8 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -290,13 +290,15 @@ def symlink_info(content) end def slash?(content) - content.directory? ? '/'.colorize(@colors[:dir]) : ' ' + content.directory? ? '/'.colorize(dir_color(content)) : ' ' end def fetch_string(path, content, key, color, increment) @count[increment] += 1 value = increment == :folders ? @folders[key] : @files[key] + logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } + name = content.name name = make_link(path, name) if @hyperlink @@ -318,10 +320,19 @@ def ls_line(chunk) print "\n" end + def dir_color(content) + if @colors[:dir_suffixes] && content.name['_'] + suffix = content.name.split(/[_]/).last.to_sym + @colors[:dir_suffixes][suffix] + end || @colors[:dir] + end + def options(content) if content.directory? key = content.name.to_sym - color = @colors[:dir] + + color = dir_color(content) + return [:folder, color, :folders] unless @all_folders.include?(key) key = @folder_aliases[key] unless @folder_keys.include?(key) return [key, color, :folders] diff --git a/lib/colorls/yaml.rb b/lib/colorls/yaml.rb index 436357a5..07252ef9 100644 --- a/lib/colorls/yaml.rb +++ b/lib/colorls/yaml.rb @@ -5,6 +5,19 @@ def initialize(filename) @user_config_filepath = File.join(Dir.home, ".config/colorls/#{filename}") end + def deep_transform_key_vals_in_object(object, &block) + case object + when Hash + object.each_with_object({}) do |(key, value), result| + result[yield(key)] = deep_transform_key_vals_in_object(value, &block) + end + when Array + object.map { |e| deep_transform_key_vals_in_object(e, &block) } + else + yield object + end + end + def load(aliase: false) yaml = read_file(@filepath) if File.exist?(@user_config_filepath) @@ -13,7 +26,7 @@ def load(aliase: false) end return yaml unless aliase - yaml.to_a.map! { |k, v| [k, v.to_sym] }.to_h + deep_transform_key_vals_in_object(yaml.to_a, &:to_sym).to_h end def read_file(filepath) diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index e8c24405..cdf5dc0f 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -2,6 +2,8 @@ unrecognized_file: gold recognized_file: lime dir: dodgerblue +dir_suffixes: + venv: royalblue # Link dead_link: red @@ -36,4 +38,4 @@ addition: chartreuse modification: darkkhaki deletion: darkred untracked: darkorange -unchanged: forestgreen \ No newline at end of file +unchanged: forestgreen From 2bf863359b69eb9bcb4055fa0707e12df347b422 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Tue, 23 Oct 2018 12:01:02 -0700 Subject: [PATCH 3/8] generalize suffixes --- lib/colorls/core.rb | 12 ++++++++---- lib/yaml/dark_colors.yaml | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 450b1dd8..f63609a8 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -321,10 +321,14 @@ def ls_line(chunk) end def dir_color(content) - if @colors[:dir_suffixes] && content.name['_'] - suffix = content.name.split(/[_]/).last.to_sym - @colors[:dir_suffixes][suffix] - end || @colors[:dir] + return @colors[:dir] unless @colors[:dir_suffixes] + name = content.name + + suffix = @colors[:dir_suffixes].find do |suffix| + name.end_with? suffix[0].to_s + end + + suffix ? @colors[:dir_suffixes][suffix.first] : @colors[:dir] end def options(content) diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index cdf5dc0f..e3d4f0f6 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -3,7 +3,7 @@ unrecognized_file: gold recognized_file: lime dir: dodgerblue dir_suffixes: - venv: royalblue + _venv: royalblue # Link dead_link: red From e2c54d7f0e9c5435150d3941395ed471b5f5b2bc Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Tue, 23 Oct 2018 12:03:34 -0700 Subject: [PATCH 4/8] fix lint issues --- lib/colorls/core.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index f63609a8..5c94e510 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -324,11 +324,11 @@ def dir_color(content) return @colors[:dir] unless @colors[:dir_suffixes] name = content.name - suffix = @colors[:dir_suffixes].find do |suffix| + suffix_arr = @colors[:dir_suffixes].find do |suffix| name.end_with? suffix[0].to_s end - suffix ? @colors[:dir_suffixes][suffix.first] : @colors[:dir] + suffix_arr ? @colors[:dir_suffixes][suffix_arr.first] : @colors[:dir] end def options(content) From ad546f4786ce67a9a1f28e6856eefde37582ebd3 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Mon, 19 Nov 2018 10:30:22 -0800 Subject: [PATCH 5/8] require forwardable --- lib/colorls/fileinfo.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/colorls/fileinfo.rb b/lib/colorls/fileinfo.rb index bb4c324e..bd02f987 100644 --- a/lib/colorls/fileinfo.rb +++ b/lib/colorls/fileinfo.rb @@ -1,3 +1,5 @@ +require "forwardable" + module ColorLS class FileInfo extend Forwardable From 81ddcbf339a6c8a576d4c5aabdcbb96ad6368235 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Mon, 19 Nov 2018 10:33:54 -0800 Subject: [PATCH 6/8] use folders var --- lib/colorls/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 9eecd8e0..fe838f43 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -339,7 +339,7 @@ def options(content) group = :folders color = dir_color(content) key = - if @all_folders.include?(key) && @folder_keys.include?(key) + if @folders.include?(key) && @folder_keys.include?(key) @folder_aliases[key] else :folder From 0e5a9c329702e8b9ed5a12b6076d76dab61a61d1 Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Mon, 19 Nov 2018 10:45:57 -0800 Subject: [PATCH 7/8] reduce complexity of options --- lib/colorls/core.rb | 41 ++++++++++++++++++++++++----------------- lib/colorls/fileinfo.rb | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index fe838f43..1866da4b 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -334,27 +334,34 @@ def file_color(file, key) @colors[color_key] end - def options(content) - if content.directory? - group = :folders - color = dir_color(content) - key = - if @folders.include?(key) && @folder_keys.include?(key) - @folder_aliases[key] - else - :folder - end - else - key = content.name.split('.').last.downcase.to_sym - key = @file_aliases[key] unless @files.key? key - key = :file if key.nil? - color = file_color(content, key) - group = @files.key?(key) ? :recognized_files : :unrecognized_files - end + def dir_options(content) + group = :folders + color = dir_color(content) + key = + if @folders.include?(key) && @folder_keys.include?(key) + @folder_aliases[key] + else + :folder + end + + [key, color, group] + end + + def file_options(content) + key = content.name.split('.').last.downcase.to_sym + key = @file_aliases[key] unless @files.key? key + key = :file if key.nil? + color = file_color(content, key) + group = @files.key?(key) ? :recognized_files : :unrecognized_files [key, color, group] end + def options(content) + return dir_options(content) if content.directory? + file_options(content) + end + def tree_traverse(path, prespace, indent) contents = init_contents(path) contents.each do |content| diff --git a/lib/colorls/fileinfo.rb b/lib/colorls/fileinfo.rb index bd02f987..fa75f1dd 100644 --- a/lib/colorls/fileinfo.rb +++ b/lib/colorls/fileinfo.rb @@ -1,4 +1,4 @@ -require "forwardable" +require 'forwardable' module ColorLS class FileInfo From 7ea7775c0b8dadef3d2a0b52c8c20038dd7387cf Mon Sep 17 00:00:00 2001 From: Justin Hallquist Date: Tue, 22 Jan 2019 10:33:25 -0800 Subject: [PATCH 8/8] merge master and fix conflicts/dead code --- .rubocop.yml | 2 +- .travis.yml | 16 +++++++++++--- README.md | 7 ++++-- Rakefile | 4 +++- colorls.gemspec | 9 +++++--- exe/colorls | 1 + lib/colorls.rb | 2 ++ lib/colorls/core.rb | 43 +++++++++++++++++++++++++------------ lib/colorls/fileinfo.rb | 5 +++++ lib/colorls/flags.rb | 13 ++++++++--- lib/colorls/git.rb | 10 +++++---- lib/colorls/monkeys.rb | 6 +++--- lib/colorls/version.rb | 4 +++- lib/colorls/yaml.rb | 3 +++ man/colorls.1 | 10 ++++----- spec/color_ls/flags_spec.rb | 29 +++++++++++++++++++++++-- spec/spec_helper.rb | 10 ++++++++- zsh/_colorls | 2 +- 18 files changed, 132 insertions(+), 44 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 572b0d7d..484dbb6f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,7 +8,7 @@ AllCops: - 'lib/yaml/*' - 'lib/**/*.sh' DisplayCopNames: true - TargetRubyVersion: 2.1 + TargetRubyVersion: 2.3 # Preferred codebase style --------------------------------------------- diff --git a/.travis.yml b/.travis.yml index dc12ec81..619756e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,7 @@ +conditions: v1 + +if: branch = master OR tag IS present + language: ruby @@ -5,15 +9,19 @@ cache: bundler rvm: - - '2.1' - - '2.2' - '2.3' - '2.4' - '2.5' + - '2.6' + +before_install: + - gem update --system + - gem --version + - gem install bundler -v '~> 2' script: - bundle exec rubocop - - bundle exec rspec + - bundle exec rake spec - bundle exec rake install - bundle binstubs colorls - PATH=$PWD/bin:$PATH @@ -47,4 +55,6 @@ script: - colorls --color=auto - colorls --color=never - colorls --color=always + - colorls --tree + - colorls --tree=1 diff --git a/README.md b/README.md index 652f4a51..4c011d5e 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,11 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the - `-h` (or) `--help` - `-l` (or) `--long` - `-r` (or) `--report` - - `-t` (or) `--tree` + - `--tree` (or) `--tree=[DEPTH]` - `--gs` (or) `--git-status` - `--sd` (or) `--sort-dirs` or `--group-directories-first` - `--sf` (or) `--sort-files` + - `-t` - [Combination of flags](#combination-of-flags) - [Installation](#installation) - [Recommended configurations](#recommended-configurations) @@ -79,7 +80,7 @@ Man pages have been added. Checkout `man colorls`. ![image](https://user-images.githubusercontent.com/17109060/32149082-96a83fec-bd25-11e7-9081-7f77e4c90e90.png) -- With `-t` (or) `--tree` : Shows tree view of the directory +- With `--tree` (or) `--tree=[DEPTH]` : Shows tree view of the directory with the specified depth (default 3) ![image](https://user-images.githubusercontent.com/17109060/32149051-32e596e4-bd25-11e7-93a9-5e50c8d2bb19.png) @@ -95,6 +96,8 @@ Man pages have been added. Checkout `man colorls`. ![image](https://user-images.githubusercontent.com/17109060/32149071-6b379de4-bd25-11e7-8764-a0c577e526a1.png) +- With `-t` : Sort by modification time, newest first (NEED TO ADD IMAGE) + - With color options : `--light` or `--dark` can be passed as a flag, to choose the appropriate color scheme. By default, the dark color scheme is chosen. In order to tweak any color, read [Custom configurations](#custom-configurations). ### Combination of flags diff --git a/Rakefile b/Rakefile index b615f9f0..a4196686 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,9 @@ require 'rubygems/tasks' Gem::Tasks.new require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new +RSpec::Core::RakeTask.new(:spec) do |t| + t.rspec_opts = "--warnings" +end require 'rubocop/rake_task' RuboCop::RakeTask.new do |task| diff --git a/colorls.gemspec b/colorls.gemspec index 98df2053..64700426 100644 --- a/colorls.gemspec +++ b/colorls.gemspec @@ -33,6 +33,8 @@ Gem::Specification.new do |spec| spec.homepage = 'https://github.com/athityakumar/colorls' spec.license = 'MIT' + spec.required_ruby_version = '>= 2.3.0' + spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) end @@ -48,14 +50,15 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'manpages', '~> 0' spec.add_runtime_dependency 'rainbow', '>= 2.2', '< 4.0' - spec.add_development_dependency 'bundler', '~> 1.15' + spec.add_development_dependency 'bundler', '~> 2.0' + spec.add_development_dependency 'codecov', '~> 0.1' spec.add_development_dependency 'diffy', '~> 3' spec.add_development_dependency 'rake', '~> 12' - spec.add_development_dependency 'rdoc', '~> 5.1' + spec.add_development_dependency 'rdoc', '~> 6.1' spec.add_development_dependency 'ronn', '~> 0' spec.add_development_dependency 'rspec', '~> 3.7' spec.add_development_dependency 'rspec-its', '~> 1.2' - spec.add_development_dependency 'rubocop', '~> 0.57.2' + spec.add_development_dependency 'rubocop', '~> 0.63.0' spec.add_development_dependency 'rubocop-rspec', '~> 1.27' spec.add_development_dependency 'rubygems-tasks', '~> 0' spec.add_development_dependency 'simplecov', '~> 0.16.1' diff --git a/exe/colorls b/exe/colorls index 1f529435..e7ad5f33 100755 --- a/exe/colorls +++ b/exe/colorls @@ -1,4 +1,5 @@ #!/usr/bin/env ruby +# frozen_string_literal: true require 'colorls' diff --git a/lib/colorls.rb b/lib/colorls.rb index c1af7393..eec01cc3 100644 --- a/lib/colorls.rb +++ b/lib/colorls.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'yaml' require 'etc' require 'English' diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 1866da4b..8aeb484c 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + module ColorLS class Core def initialize(input, all: false, report: false, sort: false, show: false, mode: nil, git_status: false, almost_all: false, colors: [], group: nil, - reverse: false, hyperlink: false) + reverse: false, hyperlink: false, tree_depth: nil) @input = File.absolute_path(input) @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @all = all @@ -15,7 +17,7 @@ def initialize(input, all: false, report: false, sort: false, show: false, @show = show @one_per_line = mode == :one_per_line @long = mode == :long - @tree = mode == :tree + @tree = {mode: mode == :tree, depth: tree_depth} process_git_status_details(git_status) @screen_width = IO.console.winsize[1] @@ -31,9 +33,9 @@ def initialize(input, all: false, report: false, sort: false, show: false, def ls return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty? - if @tree + if @tree[:mode] print "\n" - tree_traverse(@input, 0, 2) + tree_traverse(@input, 0, 1, 2) else @contents = chunkify @contents.each { |chunk| ls_line(chunk) } @@ -76,6 +78,7 @@ def init_contents(path) @total_content_length = @contents.length return @contents unless @long + init_user_lengths init_group_lengths @contents @@ -210,6 +213,7 @@ def size_info(filesize) size = "#{size[0][0..-4].rjust(4,' ')} #{size[1].ljust(3,' ')}" return size.colorize(@colors[:file_large]) if filesize >= 512 * 1024 ** 2 return size.colorize(@colors[:file_medium]) if filesize >= 128 * 1024 ** 2 + size.colorize(@colors[:file_small]) end @@ -218,11 +222,14 @@ def mtime_info(file_mtime) now = Time.now return mtime.colorize(@colors[:hour_old]) if now - file_mtime < 60 * 60 return mtime.colorize(@colors[:day_old]) if now - file_mtime < 24 * 60 * 60 + mtime.colorize(@colors[:no_modifier]) end def process_git_status_details(git_status) - return false unless git_status + @git_status = nil + + return unless git_status @git_root_path = IO.popen(['git', '-C', @input, 'rev-parse', '--show-toplevel'], err: :close, &:gets) @@ -250,6 +257,7 @@ def git_info(path, content) def git_file_info(path) return ' ✓ '.colorize(@colors[:unchanged]) unless @git_status[path] + Git.colored_status_symbols(@git_status[path].uniq, @colors) end @@ -267,12 +275,14 @@ def git_dir_info(path) def long_info(content) return '' unless @long + [mode_info(content.stats), user_info(content), group_info(content.group), size_info(content.size), mtime_info(content.mtime)].join(' ') end def symlink_info(content) return '' unless @long && content.symlink? + link_info = " ⇒ #{content.link_target}" if content.dead? "#{link_info} [Dead link]".colorize(@colors[:dead_link]) @@ -314,13 +324,14 @@ def ls_line(chunk) def dir_color(content) return @colors[:dir] unless @colors[:dir_suffixes] + name = content.name suffix_arr = @colors[:dir_suffixes].find do |suffix| name.end_with? suffix[0].to_s end - suffix_arr ? @colors[:dir_suffixes][suffix_arr.first] : @colors[:dir] + suffix_arr ? suffix_arr[1] : @colors[:dir] end def file_color(file, key) @@ -335,14 +346,11 @@ def file_color(file, key) end def dir_options(content) + key = content.name.to_sym + key = @folder_aliases[key] unless @folders.key? key + key = :folder if key.nil? group = :folders color = dir_color(content) - key = - if @folders.include?(key) && @folder_keys.include?(key) - @folder_aliases[key] - else - :folder - end [key, color, group] end @@ -359,22 +367,29 @@ def file_options(content) def options(content) return dir_options(content) if content.directory? + file_options(content) end - def tree_traverse(path, prespace, indent) + def tree_traverse(path, prespace, depth, indent) contents = init_contents(path) contents.each do |content| icon = content == contents.last || content.directory? ? ' └──' : ' ├──' print tree_branch_preprint(prespace, indent, icon).colorize(@colors[:tree]) print " #{fetch_string(path, content, *options(content))} \n" next unless content.directory? - tree_traverse("#{path}/#{content}", prespace + indent, indent) + + tree_traverse("#{path}/#{content}", prespace + indent, depth + 1, indent) if keep_going(depth) end end + def keep_going(depth) + @tree[:depth].nil? || depth < @tree[:depth] + end + def tree_branch_preprint(prespace, indent, prespace_icon) return prespace_icon if prespace.zero? + ' │ ' * (prespace/indent) + prespace_icon + '─' * indent end diff --git a/lib/colorls/fileinfo.rb b/lib/colorls/fileinfo.rb index fa75f1dd..4d8045ec 100644 --- a/lib/colorls/fileinfo.rb +++ b/lib/colorls/fileinfo.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'forwardable' module ColorLS @@ -15,6 +17,7 @@ def initialize(path, link_info=true) @stats = File.lstat(path) return unless link_info && @stats.symlink? + @dead = !File.exist?(path) @target = File.readlink(path) end @@ -29,6 +32,7 @@ def dead? def owner return @@users[@stats.uid] if @@users.key? @stats.uid + user = Etc.getpwuid(@stats.uid) @@users[@stats.uid] = user.nil? ? @stats.uid.to_s : user.name rescue ArgumentError @@ -37,6 +41,7 @@ def owner def group return @@groups[@stats.gid] if @@groups.key? @stats.gid + group = Etc.getgrgid(@stats.gid) @@groups[@stats.gid] = group.nil? ? @stats.gid.to_s : group.name rescue ArgumentError diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 9610ee6d..d1255ef2 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'optparse' require 'colorls/version' require 'ostruct' @@ -18,7 +20,8 @@ def initialize(*args) almost_all: false, report: false, git_status: false, - colors: [] + colors: [], + tree_depth: 3 } parse_options @@ -41,7 +44,8 @@ def process @args.sort!.each_with_index do |path, i| begin next STDERR.puts "\n Specified path '#{path}' doesn't exist.".colorize(:red) unless File.exist?(path) - puts '' if i > 0 + + puts '' if i.positive? puts "\n#{path}:" if Dir.exist?(path) && @args.size > 1 Core.new(path, @opts).ls rescue SystemCallError => e @@ -113,8 +117,11 @@ def add_format_options(options) end options.on('-1', 'list one file per line') { @opts[:mode] = :one_per_line } options.on('-l', '--long', 'use a long listing format') { @opts[:mode] = :long } - options.on('--tree', 'shows tree view of the directory') { @opts[:mode] = :tree } options.on('-x', 'list entries by lines instead of by columns') { @opts[:mode] = true } + options.on('--tree=[DEPTH]', Integer, 'shows tree view of the directory') do |depth| + @opts[:tree_depth] = depth + @opts[:mode] = :tree + end end def add_general_options(options) diff --git a/lib/colorls/git.rb b/lib/colorls/git.rb index ee0c1240..0e8f05a8 100644 --- a/lib/colorls/git.rb +++ b/lib/colorls/git.rb @@ -1,13 +1,15 @@ +# frozen_string_literal: true + module ColorLS - class Git < Core + module Git def self.status(repo_path) - @git_status = {} + git_status = {} IO.popen(['git', '-C', repo_path, 'status', '--porcelain', '-z', '-unormal', '--ignored']) do |output| while (status_line = output.gets "\x0") mode, file = status_line.chomp("\x0").split(' ', 2) - @git_status[file] = mode + git_status[file] = mode # skip the next \x0 separated original path for renames, issue #185 output.gets("\x0") if mode.start_with? 'R' @@ -15,7 +17,7 @@ def self.status(repo_path) end warn "git status failed in #{repo_path}" unless $CHILD_STATUS.success? - @git_status + git_status end def self.colored_status_symbols(modes, colors) diff --git a/lib/colorls/monkeys.rb b/lib/colorls/monkeys.rb index a1421da2..454478c5 100644 --- a/lib/colorls/monkeys.rb +++ b/lib/colorls/monkeys.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class String def colorize(color) self.color(color.to_sym) @@ -23,7 +25,5 @@ def symbolize_keys end class Array - def sum - inject(:+) - end + define_method(:sum) { inject(:+) } unless instance_methods.include? :sum end diff --git a/lib/colorls/version.rb b/lib/colorls/version.rb index a61aa627..6331e750 100644 --- a/lib/colorls/version.rb +++ b/lib/colorls/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ColorLS - VERSION = '1.1.1'.freeze + VERSION = '1.1.1' end diff --git a/lib/colorls/yaml.rb b/lib/colorls/yaml.rb index 07252ef9..d61b9a0b 100644 --- a/lib/colorls/yaml.rb +++ b/lib/colorls/yaml.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ColorLS class Yaml def initialize(filename) @@ -26,6 +28,7 @@ def load(aliase: false) end return yaml unless aliase + deep_transform_key_vals_in_object(yaml.to_a, &:to_sym).to_h end diff --git a/man/colorls.1 b/man/colorls.1 index 71dd8bb9..0fdae15c 100644 --- a/man/colorls.1 +++ b/man/colorls.1 @@ -1,7 +1,7 @@ .\" generated with Ronn/v0.7.3 .\" http://github.com/rtomayko/ronn/tree/0.7.3 . -.TH "COLORLS" "1" "October 2018" "colorls 1.1.1" "colorls Manual" +.TH "COLORLS" "1" "January 2019" "colorls 1.1.1" "colorls Manual" . .SH "NAME" \fBcolorls\fR \- list directory contents with colors and icons @@ -59,14 +59,14 @@ list one file per line use a long listing format . .TP -\fB\-\-tree\fR -shows tree view of the directory -. -.TP \fB\-x\fR list entries by lines instead of by columns . .TP +\fB\-\-tree\fR +shows tree view of the directory +. +.TP \fB\-\-sd\fR, \fB\-\-sort\-dirs\fR, \fB\-\-group\-directories\-first\fR sort directories first . diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index ce29e549..9ba2ddad 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -21,7 +21,13 @@ def capture_stdout it { is_expected.not_to match(/\.hidden-file/) } # does not display hidden files it { is_expected.not_to match(/Found \d+ contents/) } # does not show a report it { is_expected.to match(/a-file.+symlinks.+z-file/m) } # displays dirs & files alphabetically - it { is_expected.not_to match(/(.*\n){3}/) } # displays multiple files per line + + it 'displays multiple files per line' do + expect(::STDOUT).to receive(:tty?).and_return(true) + + is_expected.not_to match(/(.*\n){3}/) + end + it { is_expected.not_to match(%r(\.{1,2}/)) } # does not display ./ or ../ it { is_expected.not_to match(/├──/) } # does not display file hierarchy end @@ -137,7 +143,11 @@ def capture_stdout context 'with --sort=size flag' do let(:args) { ['--sort=size', '--group-directories-first', FIXTURES] } - it { is_expected.to match(/symlinks.+a-file.+z-file/) } # sorts results by size + it 'sorts results by size' do + expect(::STDOUT).to receive(:tty?).and_return(true) + + is_expected.to match(/symlinks.+a-file.+z-file/) + end end context 'with --sort=extension flag' do @@ -174,6 +184,21 @@ def capture_stdout let(:args) { ['--tree', FIXTURES] } it { is_expected.to match(/├──/) } # displays file hierarchy + it { is_expected.to match(/third-level-file.txt/) } + end + + context 'with --tree=1 flag' do + let(:args) { ['--tree=1', FIXTURES] } + + it { is_expected.to match(/├──/) } # displays file hierarchy + it { is_expected.not_to match(/ReadmeLink.md|Supportlink|doesnotexisttest.txt|third-level-file.txt/) } + end + + context 'with --tree=3 flag' do + let(:args) { ['--tree=3', FIXTURES] } + + it { is_expected.to match(/├──/) } # displays file hierarchy + it { is_expected.to match(/third-level-file.txt/) } end context 'with --hyperlink flag' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cc0fd886..876d1fe1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,13 @@ require 'simplecov' -SimpleCov.start + +SimpleCov.start do + add_filter '/spec/' +end + +if ENV['CI'] == 'true' + require 'codecov' + SimpleCov.formatter = SimpleCov::Formatter::Codecov +end require 'bundler/setup' require 'colorls' diff --git a/zsh/_colorls b/zsh/_colorls index 6084edcb..692084cf 100644 --- a/zsh/_colorls +++ b/zsh/_colorls @@ -19,8 +19,8 @@ _arguments -s -S \ "-1[list one file per line]" \ "-l[use a long listing format]" \ "--long[use a long listing format]" \ - "--tree[shows tree view of the directory]" \ "-x[list entries by lines instead of by columns]" \ + "--tree[shows tree view of the directory]" \ "--sd[sort directories first]" \ "--sort-dirs[sort directories first]" \ "--group-directories-first[sort directories first]" \