-
-
Save webgago/1170102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/ruby | |
module DocSorter | |
extend self | |
require 'ap' | |
DOC = %w{doc docx xls odt xlsx dwg txt cdr psd ppt eml csv log xslic} | |
CODE = %w{yml sql mht htm html php rb diff conf} | |
PDF = %w{pdf} | |
GRAPH = %w{tiff jpg jpeg bmp gif png} | |
ARH = %w{zip rar 7z gz tgz bz2 bz exe jnlp msi apk xpi gem ipsw} | |
EXE = %w{exe msi} | |
ISO = %w{iso deb} | |
MUZ = %w{mp3 wav flac rm} | |
TORRENTS = %w{torrent} | |
BOOKS = %w{azw mobi epub} | |
COLORS = [:gray, :red, :green, :yellow, :blue, | |
:purple, :cyan, :white, :black, :redish, | |
:greenish, :yellowish, :blueish, :purpleish, | |
:cyanish, :pale] | |
def process(dir='/home/achernikov/Downloads') | |
print *files_scan(dir) | |
end | |
def files_scan(dir='/home/achernikov/Downloads') | |
puts "Processing #{dir} directory" | |
files = Dir["#{dir}/*"].delete_if { |f| !File.file?(f) } | |
puts "We have #{files.count} files in target dir" | |
files_by_types = Hash.new { |hash, key| hash[key] = [] } | |
constants.each do |const| | |
const_get(const).each do |type| | |
files_by_types[const.to_s.downcase.to_sym] = files.select { |file| file =~ /#{type}$/i } | |
end | |
end | |
[files, files_by_types] | |
end | |
def print(files, files_by_types, options = {}) | |
options = { | |
:file_types => { | |
:color => {:string => COLORS.delete(":red") { COLORS }.sample} | |
}, | |
:files => { | |
:color => {:string => :red}, | |
:indent => 20 | |
}}.merge options | |
files_by_types.each do |ftf| | |
ftf.last.each { |x| files.delete(x) } | |
ap ftf, options[:file_types] | |
end | |
ap files, options[:files] | |
end | |
end | |
DocSorter.process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment