diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..450a1ef --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,94 @@ +AllCops: + Include: + - 'lib/**/*' + Exclude: + - 'vendor/**/*' + - 'benchmarks/*' + - 'profile/*' + - 'lib/yaml/*' + DisplayCopNames: true + TargetRubyVersion: 2.1 + +# Preferred codebase style --------------------------------------------- +Layout/ExtraSpacing: + AllowForAlignment: true + +Style/FormatString: + EnforcedStyle: percent + +Style/AndOr: + EnforcedStyle: conditionals + +Layout/SpaceAroundEqualsInParameterDefault: + EnforcedStyle: no_space + +Layout/SpaceInsideBlockBraces: + EnforcedStyle: space + +Layout/SpaceInsideHashLiteralBraces: + EnforcedStyle: no_space + +Layout/AlignParameters: + EnforcedStyle: with_fixed_indentation + +Style/EmptyElse: + EnforcedStyle: empty + +Metrics/BlockLength: + Exclude: + - 'spec/**/*' + +Metrics/LineLength: + Max: 120 + +Metrics/ModuleLength: + Max: 200 + +Metrics/ClassLength: + Max: 200 + +Metrics/ParameterLists: + Max: 10 + +Style/ParallelAssignment: + Enabled: false + +Style/DoubleNegation: + Enabled: false + +Style/SingleLineBlockParams: + Enabled: false + +Style/PerlBackrefs: + Enabled: false + +Layout/SpaceAfterComma: + Enabled: false + +Layout/SpaceAroundOperators: + Enabled: false + +Style/EmptyCaseCondition: + Enabled: false + +Style/FileName: + Enabled: false + +Style/MultilineBlockChain: + Enabled: false + +# Current preferred metrics -------------------------------------------- +# Better values are encouraged, but not required. +Metrics/AbcSize: + Max: 20 + +Metrics/MethodLength: + Max: 15 + +Metrics/CyclomaticComplexity: + Max: 7 + +# TODO ----------------------------------------------------------------- + +Style/Documentation: + Enabled: false \ No newline at end of file diff --git a/Gemfile b/Gemfile index 1f2ef00..fa75df1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,3 @@ source 'https://rubygems.org' -gem 'colorize' -gem 'facets' -gem 'rubocop' -gem 'ruby-terminfo' +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..d8d5c79 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Athitya + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4c774a2 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/colorls.gemspec b/colorls.gemspec new file mode 100644 index 0000000..2e8619f --- /dev/null +++ b/colorls.gemspec @@ -0,0 +1,32 @@ +# coding: utf-8 + +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +require 'colorls/version' + +Gem::Specification.new do |spec| + spec.name = 'colorls' + spec.version = ColorLS::VERSION + spec.authors = ['Athitya'] + spec.email = ['athityakumar@gmail.com'] + + spec.summary = "A Ruby CLI gem that beautifies the terminal's ls command, with color and font-awesome icons." + spec.homepage = 'https://github.com/athityakumar/colorls' + spec.license = 'MIT' + + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + spec.bindir = 'exe' + spec.executables = 'lc' + spec.require_paths = ['lib'] + + spec.add_runtime_dependency 'colorize' + spec.add_runtime_dependency 'facets' + spec.add_runtime_dependency 'ruby-terminfo' + + spec.add_development_dependency 'bundler', '~> 1.15' + spec.add_development_dependency 'rake' + spec.add_development_dependency 'rspec' + spec.add_development_dependency 'rubocop' +end diff --git a/exe/lc b/exe/lc new file mode 100755 index 0000000..bef39aa --- /dev/null +++ b/exe/lc @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby + +require 'colorls' + +args = *ARGV +opts = {} + +opts[:report] = args.include?('-r') || args.include?('--report') +opts[:one_per_line] = args.include?('-1') + +show_dirs_only = args.include?('-d') || args.include?('--dirs') +show_files_only = args.include?('-f') || args.include?('--files') +sort_dirs_first = args.include?('-sd') || args.include?('--sort-dirs') +sort_files_first = args.include?('-sf') || args.include?('--sort-files') + +if sort_dirs_first && sort_files_first + STDERR.puts "\n Restrain from using -sd and -sf flags together." + .colorize(:red) + return +end + +if show_files_only && show_dirs_only + STDERR.puts "\n Restrain from using -d and -f flags together." + .colorize(:red) + return +end + +opts[:sort] = if sort_files_first + :files + elsif sort_dirs_first + :dirs + end + +opts[:show] = if show_files_only + :files + elsif show_dirs_only + :dirs + end + +args.keep_if { |arg| !arg.start_with?('-') } + +if args.empty? + ColorLS.new(opts).ls +else + args.each do |path| + if Dir.exist?(path) + ColorLS.new(path, opts).ls + else + next STDERR.puts "\n Specified directory '#{path}' doesn't exist." + .colorize(:red) + end + end +end + +true diff --git a/colorls.rb b/lib/colorls.rb old mode 100755 new mode 100644 similarity index 69% rename from colorls.rb rename to lib/colorls.rb index 533b944..aa72029 --- a/colorls.rb +++ b/lib/colorls.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +require 'colorls/version' require 'colorize' require 'yaml' @@ -6,15 +6,15 @@ require 'facets' require 'terminfo' # Source for icons unicode: http://nerdfonts.com/ -class ColorLS # rubocop:disable ClassLength - def initialize(input = nil, report:, sort:, show:, one_per_line:) +class ColorLS + def initialize(input=nil, report:, sort:, show:, one_per_line:) @input = input || Dir.pwd - @count = { folders: 0, recognized_files: 0, unrecognized_files: 0 } + @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @report = report @sort = sort @show = show @one_per_line = one_per_line - @screen_width = TermInfo.screen_size.last + @screen_width = ::TermInfo.screen_size.last init_contents @max_widths = @contents.map(&:length) @@ -128,10 +128,9 @@ class ColorLS # rubocop:disable ClassLength "#{logo} #{content}".colorize(color) end - def load_from_yaml(filename, aliase = false) - prog = $PROGRAM_NAME - path = prog.include?('/colorls.rb') ? prog.gsub('/colorls.rb', '') : '.' - yaml = YAML.safe_load(File.read("#{path}/#{filename}")).symbolize_keys + def load_from_yaml(filename, aliase=false) + filepath = File.join(File.dirname(__FILE__),"yaml/#{filename}") + yaml = YAML.safe_load(File.read(filepath)).symbolize_keys return yaml unless aliase yaml .to_a @@ -166,55 +165,3 @@ class ColorLS # rubocop:disable ClassLength [key, :green, :recognized_files] end end - -args = *ARGV -opts = {} - -opts[:report] = args.include?('-r') || args.include?('--report') -opts[:one_per_line] = args.include?('-1') - -show_dirs_only = args.include?('-d') || args.include?('--dirs') -show_files_only = args.include?('-f') || args.include?('--files') -sort_dirs_first = args.include?('-sd') || args.include?('--sort-dirs') -sort_files_first = args.include?('-sf') || args.include?('--sort-files') - -if sort_dirs_first && sort_files_first - STDERR.puts "\n Restrain from using -sd and -sf flags together." - .colorize(:red) - return -end - -if show_files_only && show_dirs_only - STDERR.puts "\n Restrain from using -d and -f flags together." - .colorize(:red) - return -end - -opts[:sort] = if sort_files_first - :files - elsif sort_dirs_first - :dirs - end - -opts[:show] = if show_files_only - :files - elsif show_dirs_only - :dirs - end - -args.keep_if { |arg| !arg.start_with?('-') } - -if args.empty? - ColorLS.new(opts).ls -else - args.each do |path| - if Dir.exist?(path) - ColorLS.new(path, opts).ls - else - next STDERR.puts "\n Specified directory '#{path}' doesn't exist." - .colorize(:red) - end - end -end - -true diff --git a/lib/colorls/version.rb b/lib/colorls/version.rb new file mode 100644 index 0000000..4b9892b --- /dev/null +++ b/lib/colorls/version.rb @@ -0,0 +1,3 @@ +module ColorLS + VERSION = '0.1.0'.freeze +end diff --git a/file_aliases.yaml b/lib/yaml/file_aliases.yaml similarity index 100% rename from file_aliases.yaml rename to lib/yaml/file_aliases.yaml diff --git a/files.yaml b/lib/yaml/files.yaml similarity index 100% rename from files.yaml rename to lib/yaml/files.yaml diff --git a/folder_aliases.yaml b/lib/yaml/folder_aliases.yaml similarity index 100% rename from folder_aliases.yaml rename to lib/yaml/folder_aliases.yaml diff --git a/folders.yaml b/lib/yaml/folders.yaml similarity index 100% rename from folders.yaml rename to lib/yaml/folders.yaml diff --git a/pkg/colorls-0.1.0.gem b/pkg/colorls-0.1.0.gem new file mode 100644 index 0000000..7547d6e Binary files /dev/null and b/pkg/colorls-0.1.0.gem differ diff --git a/spec/colorls_spec.rb b/spec/colorls_spec.rb new file mode 100644 index 0000000..ee548c1 --- /dev/null +++ b/spec/colorls_spec.rb @@ -0,0 +1,5 @@ +RSpec.describe ColorLS do + it 'has a version number' do + expect(ColorLS::VERSION).not_to be nil + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..7548193 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,14 @@ +require 'bundler/setup' +require 'colorls' + +RSpec.configure do |config| + # Enable flags like --only-failures and --next-failure + config.example_status_persistence_file_path = '.rspec_status' + + # Disable RSpec exposing methods globally on `Module` and `main` + config.disable_monkey_patching! + + config.expect_with :rspec do |c| + c.syntax = :expect + end +end