From ad726bc083fa1b9c2bf49df48ffead86b83c8c82 Mon Sep 17 00:00:00 2001 From: Athitya Date: Thu, 29 Jun 2017 22:08:38 +0530 Subject: [PATCH] Inits repository --- .gitignore | 1 + Gemfile | 4 ++++ aliases.yaml | 54 ++++++++++++++++++++++++++++++++++++++++++++++ colorls.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ formats.yaml | 33 +++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 aliases.yaml create mode 100644 colorls.rb create mode 100644 formats.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68feb7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Gemfile.lock \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..af2af3d --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem 'colorize' +gem 'facets' diff --git a/aliases.yaml b/aliases.yaml new file mode 100644 index 0000000..0c2a20b --- /dev/null +++ b/aliases.yaml @@ -0,0 +1,54 @@ +apk: android +ds_store: apple +localized: apple +mp3: audio +scss: css +docx: doc +gdoc: doc +mobi: ebook +eot: font +otf: font +ttf: font +woff: font +editorconfig: git +gitconfig: git +gitignore: git +gitignore_global: git +jpeg: image +jpg: image +png: image +svg: image +markdown: md +mkd: md +rdoc: md +readme: md +license: md +pptx: ppt +gslides: ppt +pyc: py +rdata: r +rds: r +gemfile: rb +gemspec: rb +guardfile: rb +lock: rb +procfile: rb +rakefile: rb +rspec: rb +rspec_status: rb +rspec_parallel: rb +ru: rb +bash: sh +bashrc: sh +bash_history: sh +bash_profile: sh +zsh: sh +zsh-theme: sh +zshrc: sh +mp4: video +csv: xls +gsheet: xls +xlsx: xls +yaml: yml +rar: zip +tar: zip \ No newline at end of file diff --git a/colorls.rb b/colorls.rb new file mode 100644 index 0000000..333a122 --- /dev/null +++ b/colorls.rb @@ -0,0 +1,60 @@ +require 'colorize' +require 'yaml' +require 'facets' + +# Source for icons unicode: http://nerdfonts.com/ +class ColorLS + def initialize(input) + @input = input || Dir.pwd + @contents = Dir.entries(@input) - ['.', '..'] + @count = { folders: 0, recognized_files: 0, unrecognized_files: 0 } + @formats = load_from_yaml('formats.yaml').symbolize_keys + @aliases = load_from_yaml('aliases.yaml') + .to_a + .map! { |k, v| [k.to_sym, v.to_sym] } + .to_h + @format_keys = @formats.keys + @aliase_keys = @aliases.keys + end + + def ls + @contents.each { |content| print fetch_string(content, *options(content)) } + + puts "\n\n\tFound #{@contents.length} contents in this directory"\ + "\n\t#{File.expand_path(@input)}."\ + "\n\n\t\tFolders\t\t\t: #{@count[:folders]}"\ + "\n\t\tRecognized files\t: #{@count[:recognized_files]}"\ + "\n\t\tUnrecognized files\t: #{@count[:unrecognized_files]}" + .colorize(:white) + end + + private + + def fetch_string(content, key, color, increment) + @count[increment] += 1 + + value = @formats[key] + logo = value.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') } + "\n\t#{logo} #{content}".colorize(color) + end + + def load_from_yaml(filename) + path = $PROGRAM_NAME.gsub('/colorls.rb', '') + YAML.safe_load(File.read("#{path}/#{filename}")) + end + + def options(content) + return %i[folder blue folders] if Dir.exist?("#{@input}/#{content}") + + all_keys = @format_keys + @aliase_keys + key = content.split('.').last.downcase.to_sym + + return %i[file yellow unrecognized_files] unless all_keys.include?(key) + + key = @aliases[key] unless @format_keys.include?(key) + [key, :green, :recognized_files] + end +end + +ColorLS.new(ARGV[0]).ls +true diff --git a/formats.yaml b/formats.yaml new file mode 100644 index 0000000..06fba01 --- /dev/null +++ b/formats.yaml @@ -0,0 +1,33 @@ +android: "\uf17b" +apple: "\uf179" +audio: "\uf001" +avro: "\ue60b" +c: "\ue61e" +cpp: "\ue61d" +css: "\ue749" +doc: "\uf1c2" +ebook: "\ue28b" +file: "\uf15b" +folder: "\uf115" +font: "\uf031" +gform: "\uf298" +git: "\uf1d3" +html: "\uf13b" +image: "\uf1c5" +js: "\ue74e" +json: "\ue60b" +less: "\ue758" +md: "\uf48a" +ppt: "\uf1c4" +pdf: "\uf1c1" +py: "\ue606" +r: "\uf25d" +rb: "\ue21e" +rdb: "\ue76d" +sh: "\uf489" +txt: "\uf15c" +video: "\uf03d" +xls: "\uf1c3" +xml: "\ue619" +yml: "\uf481" +zip: "\uf410" \ No newline at end of file