From 39b7282c8a1566a0e91b9a46d12419304dfa686b Mon Sep 17 00:00:00 2001 From: Athitya Date: Fri, 7 Jul 2017 20:19:46 +0530 Subject: [PATCH 1/2] Updates to gem v0.1.3 --- colorls.gemspec | 4 +- exe/{lc => colorls} | 0 lib/colorls.rb | 163 +---------------------------------------- lib/colorls/core.rb | 161 ++++++++++++++++++++++++++++++++++++++++ lib/colorls/version.rb | 2 +- 5 files changed, 165 insertions(+), 165 deletions(-) rename exe/{lc => colorls} (100%) create mode 100644 lib/colorls/core.rb diff --git a/colorls.gemspec b/colorls.gemspec index 2e8619f..d3f0462 100644 --- a/colorls.gemspec +++ b/colorls.gemspec @@ -7,7 +7,7 @@ require 'colorls/version' Gem::Specification.new do |spec| spec.name = 'colorls' spec.version = ColorLS::VERSION - spec.authors = ['Athitya'] + spec.authors = ['Athitya Kumar'] spec.email = ['athityakumar@gmail.com'] spec.summary = "A Ruby CLI gem that beautifies the terminal's ls command, with color and font-awesome icons." @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| f.match(%r{^(test|spec|features)/}) end spec.bindir = 'exe' - spec.executables = 'lc' + spec.executables = 'colorls' spec.require_paths = ['lib'] spec.add_runtime_dependency 'colorize' diff --git a/exe/lc b/exe/colorls similarity index 100% rename from exe/lc rename to exe/colorls diff --git a/lib/colorls.rb b/lib/colorls.rb index f52c6f8..c1b5571 100644 --- a/lib/colorls.rb +++ b/lib/colorls.rb @@ -3,166 +3,5 @@ require 'yaml' require 'facets' require 'terminfo' +require 'colorls/core' require 'colorls/flags' -# Source for icons unicode: http://nerdfonts.com/ -module ColorLS - class Core - def initialize(input=nil, report: false, sort: false, show: false, one_per_line: false) - @input = input || Dir.pwd - @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 - - init_contents - @max_widths = @contents.map(&:length) - init_icons - end - - def ls - @contents = chunkify - @max_widths = @contents.transpose.map { |c| c.map(&:length).max } - @contents.each { |chunk| ls_line(chunk) } - print "\n" - display_report if @report - - true - end - - private - - def init_contents - @contents = Dir.entries(@input) - %w[. ..] - - filter_contents if @show - sort_contents if @sort - - @total_content_length = @contents.length - end - - def filter_contents - @contents.keep_if do |x| - next Dir.exist?("#{@input}/#{x}") if @show == :dirs - !Dir.exist?("#{@input}/#{x}") - end - end - - def sort_contents - @contents.sort! { |a, b| cmp_by_dirs(a, b) } - end - - def cmp_by_dirs(a, b) - is_a_dir = Dir.exist?("#{@input}/#{a}") - is_b_dir = Dir.exist?("#{@input}/#{b}") - - return cmp_by_alpha(a, b) unless is_a_dir ^ is_b_dir - - result = is_a_dir ? -1 : 1 - result *= -1 if @sort == :files - result - end - - def cmp_by_alpha(a, b) - a.downcase <=> b.downcase - end - - def init_icons - @files = load_from_yaml('files.yaml') - @file_aliases = load_from_yaml('file_aliases.yaml', true) - @folders = load_from_yaml('folders.yaml') - @folder_aliases = load_from_yaml('folder_aliases.yaml', true) - - @file_keys = @files.keys - @file_aliase_keys = @file_aliases.keys - @folder_keys = @folders.keys - @folder_aliase_keys = @folder_aliases.keys - - @all_files = @file_keys + @file_aliase_keys - @all_folders = @folder_keys + @folder_aliase_keys - end - - def chunkify - return @contents.zip if @one_per_line - - chunk_size = @contents.count - - until in_line(chunk_size) || chunk_size <= 1 - chunk_size -= 1 - chunk = get_chunk(chunk_size) - end - - chunk || [@contents] - end - - def get_chunk(chunk_size) - chunk = @contents.each_slice(chunk_size).to_a - chunk.last += [''] * (chunk_size - chunk.last.count) - @max_widths = chunk.transpose.map { |c| c.map(&:length).max } - chunk - end - - def in_line(chunk_size) - return false if @max_widths.sum + 6 * chunk_size > @screen_width - true - end - - def display_report - print "\n Found #{@total_content_length} contents in directory " - .colorize(:white) - - print File.expand_path(@input).to_s.colorize(:blue) - - puts "\n\n\tFolders\t\t\t: #{@count[:folders]}"\ - "\n\tRecognized files\t: #{@count[:recognized_files]}"\ - "\n\tUnrecognized files\t: #{@count[:unrecognized_files]}" - .colorize(:white) - end - - def fetch_string(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') } - - "#{logo} #{content}".colorize(color) - end - - 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 - .map! { |k, v| [k, v.to_sym] } - .to_h - end - - def ls_line(chunk) - print "\n" - chunk.each_with_index do |content, i| - break if content.empty? - - print " #{fetch_string(content, *options(content))}" - print Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' ' - print ' ' * (@max_widths[i] - content.length) - end - end - - def options(content) - if Dir.exist?("#{@input}/#{content}") - key = content.to_sym - return %i[folder blue folders] unless @all_folders.include?(key) - key = @folder_aliases[key] unless @folder_keys.include?(key) - return [key, :blue, :folders] - end - - key = content.split('.').last.downcase.to_sym - - return %i[file yellow unrecognized_files] unless @all_files.include?(key) - - key = @file_aliases[key] unless @file_keys.include?(key) - [key, :green, :recognized_files] - end - end -end diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb new file mode 100644 index 0000000..a37f423 --- /dev/null +++ b/lib/colorls/core.rb @@ -0,0 +1,161 @@ +module ColorLS + class Core + def initialize(input=nil, report: false, sort: false, show: false, one_per_line: false) + @input = input || Dir.pwd + @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 + + init_contents + @max_widths = @contents.map(&:length) + init_icons + end + + def ls + @contents = chunkify + @max_widths = @contents.transpose.map { |c| c.map(&:length).max } + @contents.each { |chunk| ls_line(chunk) } + print "\n" + display_report if @report + + true + end + + private + + def init_contents + @contents = Dir.entries(@input) - %w[. ..] + + filter_contents if @show + sort_contents if @sort + + @total_content_length = @contents.length + end + + def filter_contents + @contents.keep_if do |x| + next Dir.exist?("#{@input}/#{x}") if @show == :dirs + !Dir.exist?("#{@input}/#{x}") + end + end + + def sort_contents + @contents.sort! { |a, b| cmp_by_dirs(a, b) } + end + + def cmp_by_dirs(a, b) + is_a_dir = Dir.exist?("#{@input}/#{a}") + is_b_dir = Dir.exist?("#{@input}/#{b}") + + return cmp_by_alpha(a, b) unless is_a_dir ^ is_b_dir + + result = is_a_dir ? -1 : 1 + result *= -1 if @sort == :files + result + end + + def cmp_by_alpha(a, b) + a.downcase <=> b.downcase + end + + def init_icons + @files = load_from_yaml('files.yaml') + @file_aliases = load_from_yaml('file_aliases.yaml', true) + @folders = load_from_yaml('folders.yaml') + @folder_aliases = load_from_yaml('folder_aliases.yaml', true) + + @file_keys = @files.keys + @file_aliase_keys = @file_aliases.keys + @folder_keys = @folders.keys + @folder_aliase_keys = @folder_aliases.keys + + @all_files = @file_keys + @file_aliase_keys + @all_folders = @folder_keys + @folder_aliase_keys + end + + def chunkify + return @contents.zip if @one_per_line + + chunk_size = @contents.count + + until in_line(chunk_size) || chunk_size <= 1 + chunk_size -= 1 + chunk = get_chunk(chunk_size) + end + + chunk || [@contents] + end + + def get_chunk(chunk_size) + chunk = @contents.each_slice(chunk_size).to_a + chunk.last += [''] * (chunk_size - chunk.last.count) + @max_widths = chunk.transpose.map { |c| c.map(&:length).max } + chunk + end + + def in_line(chunk_size) + return false if @max_widths.sum + 6 * chunk_size > @screen_width + true + end + + def display_report + print "\n Found #{@total_content_length} contents in directory " + .colorize(:white) + + print File.expand_path(@input).to_s.colorize(:blue) + + puts "\n\n\tFolders\t\t\t: #{@count[:folders]}"\ + "\n\tRecognized files\t: #{@count[:recognized_files]}"\ + "\n\tUnrecognized files\t: #{@count[:unrecognized_files]}" + .colorize(:white) + end + + def fetch_string(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') } + + "#{logo} #{content}".colorize(color) + end + + 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 + .map! { |k, v| [k, v.to_sym] } + .to_h + end + + def ls_line(chunk) + print "\n" + chunk.each_with_index do |content, i| + break if content.empty? + + print " #{fetch_string(content, *options(content))}" + print Dir.exist?("#{@input}/#{content}") ? '/'.colorize(:blue) : ' ' + print ' ' * (@max_widths[i] - content.length) + end + end + + def options(content) + if Dir.exist?("#{@input}/#{content}") + key = content.to_sym + return %i[folder blue folders] unless @all_folders.include?(key) + key = @folder_aliases[key] unless @folder_keys.include?(key) + return [key, :blue, :folders] + end + + key = content.split('.').last.downcase.to_sym + + return %i[file yellow unrecognized_files] unless @all_files.include?(key) + + key = @file_aliases[key] unless @file_keys.include?(key) + [key, :green, :recognized_files] + end + end +end diff --git a/lib/colorls/version.rb b/lib/colorls/version.rb index c1f6a2e..c323136 100644 --- a/lib/colorls/version.rb +++ b/lib/colorls/version.rb @@ -1,3 +1,3 @@ module ColorLS - VERSION = '0.1.1'.freeze + VERSION = '0.1.3'.freeze end From 6479092635dc825b33e1944a41b64a7e0b15c609 Mon Sep 17 00:00:00 2001 From: Athitya Date: Fri, 7 Jul 2017 20:25:38 +0530 Subject: [PATCH 2/2] Updated markdown and travis files --- .travis.yml | 14 +++++++------- CONTRIBUTING.md | 4 ++-- README.md | 21 +++++++++++++++++---- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8a8336a..32866dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,13 +11,13 @@ script: - bundle exec rubocop - bundle exec rspec - rake install - - lc - - lc -d - - lc -f - - lc -sd - - lc -sf - - lc -1 - - lc -r + - colorls + - colorls -d + - colorls -f + - colorls -sd + - colorls -sf + - colorls -1 + - colorls -r install: - gem install bundler diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index de293ad..8369244 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,10 +25,10 @@ Please proceed with a Pull Request only after you're assigned. It'd be sad if yo - adding better icons to [YAML files](lib/yaml/) - adding more flag options to the ruby gem. -3. (Optional) To test whether `lc` is working properly, do +3. (Optional) To test whether `colorls` executable is working properly, do ```sh rake install - lc # start using lc + colorls # start using colorls ``` 4. (Required for YAML file changes) These are the specifications for the YAML files - diff --git a/README.md b/README.md index fa416c9..50efe02 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the - [Usage](#usage) - [Installatation](#installation) -- [Optional configurations](#optional-configurations) +- [Recommended configurations](#recommended-configurations) +- [Updating](#updating) - [Uninstallation](#uninstallation) - [Contributing](#contributing) - [License](#license) @@ -64,15 +65,15 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the rehash ``` -4. Start using `lc` :tada: +4. Start using `colorls` :tada: # Optional configurations [(Back to top)](#table-of-contents) -1. To add some flag options by default, add this to your shell configuration file (`~/.bashrc`, `~/.zshrc` or `~/.fishrc`) : +1. To add some short command (say, `lc`) with some flag options (say, `-r`)b y default, add this to your shell configuration file (`~/.bashrc`, `~/.zshrc` or `~/.fishrc`) : ```sh - alias lc='lc -r' + alias lc='colorls -r' ``` 2. For changing the icon(s) to other unicode icons of choice (select icons from [here](https://nerdfonts.com/)), change the YAML files in a text editor of your choice (say, `subl`) @@ -82,6 +83,18 @@ A Ruby script that colorizes the `ls` output with color and icons. Here are the _NOTE: If you're using iTerm2 on Mac, you may have to enable the nerd-font at iTerm2 > Preferences > Profiles > Text > Non-Ascii font > Knack Regular Nerd Font Complete_ + + +# Updating + +[(Back to top)](#table-of-contents) + +Want to update to the latest version of `colorls`? + +```sh +gem update colorls +``` + # Uninstallation [(Back to top)](#table-of-contents)