diff --git a/colorls.gemspec b/colorls.gemspec index d3da2cf..6a6eda9 100644 --- a/colorls.gemspec +++ b/colorls.gemspec @@ -36,7 +36,6 @@ Gem::Specification.new do |spec| spec.post_install_message = ColorLS::POST_INSTALL_MESSAGE spec.add_runtime_dependency 'filesize' - spec.add_runtime_dependency 'git' spec.add_runtime_dependency 'rainbow' spec.add_runtime_dependency 'rake' diff --git a/lib/colorls.rb b/lib/colorls.rb index a42d160..4eebe85 100644 --- a/lib/colorls.rb +++ b/lib/colorls.rb @@ -8,3 +8,4 @@ require 'colorls/core' require 'colorls/flags' require 'colorls/load_from_yaml' require 'colorls/monkeys' +require 'colorls/git' \ No newline at end of file diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index feb13e2..b985761 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -2,7 +2,7 @@ module ColorLS class Core def initialize(input=nil, all: false, report: false, sort: false, show: false, one_per_line: false, git_status: false,long: false, almost_all: false, tree: false, colors: []) - @input = input || Dir.pwd + @input = init_input_path(input) @count = {folders: 0, recognized_files: 0, unrecognized_files: 0} @all = all @almost_all = almost_all @@ -12,7 +12,7 @@ module ColorLS @one_per_line = one_per_line @long = long @tree = tree - @git_status = git_status + process_git_status_details(git_status) @screen_width = `tput cols`.chomp.to_i @colors = colors @@ -39,6 +39,16 @@ module ColorLS private + def init_input_path(input) + return Dir.pwd unless input + + actual = Dir.pwd + Dir.chdir(input) + input = Dir.pwd + Dir.chdir(actual) + input + end + def init_contents(path) is_directory = Dir.exist?(path) @contents = if is_directory @@ -216,25 +226,49 @@ module ColorLS mtime.colorize(@colors[:no_modifier]) end - def git_info(path, content) - return '' unless @git_status + def process_git_status_details(git_status) + return false unless git_status + + actual_path = Dir.pwd + Dir.chdir(@input) until File.exist?('.git') # check whether the repository is git controlled - return '' if Dir.pwd=='/' + return false if Dir.pwd=='/' Dir.chdir('..') end - relative_path = path.remove(Dir.pwd+'/') - relative_path = relative_path==path ? '' : relative_path+'/' + @git_root_path = Dir.pwd + Dir.chdir(actual_path) - status = Git.open('.').status - git_info_of_file("#{relative_path}#{content}", status) + @git_status = Git.status(@git_root_path) end - def git_info_of_file(path, status) - return '(A)'.colorize(@colors[:added]) if status.added.keys.any? { |a| a.include?(path) } - return '(?)'.colorize(@colors[:untracked]) if status.untracked.keys.any? { |u| u.include?(path) } - return '(C)'.colorize(@colors[:changed]) if status.changed.keys.any? { |c| c.include?(path) } - ' '.colorize(@colors[:unchanged]) + def git_info(path, content) + return '' unless @git_status + + # puts "\n\n" + + Dir.chdir(@git_root_path) + relative_path = path.remove(@git_root_path+'/') + relative_path = relative_path==path ? '' : relative_path+'/' + content_path = "#{relative_path}#{content}" + content_type = Dir.exist?("#{@git_root_path}/#{content_path}") ? :dir : :file + + if content_type == :file then git_file_info(content_path) + else git_dir_info(content_path) + end + # puts "\n\n" + end + + def git_file_info(path) + return ' ✓ '.colorize(@colors[:unchanged]) unless @git_status[path] + Git.colored_status_symbols(@git_status[path], @colors) + end + + def git_dir_info(path) + modes = @git_status.select { |file, mode| file.start_with?(path) }.values + + return ' ✓ '.colorize(@colors[:unchanged]) if modes.empty? + Git.colored_status_symbols(modes.join.uniq, @colors) end def long_info(path, content) diff --git a/lib/colorls/git.rb b/lib/colorls/git.rb new file mode 100644 index 0000000..3753d1e --- /dev/null +++ b/lib/colorls/git.rb @@ -0,0 +1,33 @@ +module ColorLS + class Git < Core + def self.status(repo_path) + actual = Dir.pwd + Dir.chdir(repo_path) + + @git_status = {} + + `git status --short`.split("\n").map { |x| x.split(" ") }.each do |mode, file| + @git_status[file] = mode + end + + Dir.chdir(actual) + return(@git_status) + end + + def self.colored_status_symbols(modes, colors) + modes = + case modes.length + when 1 then " #{modes} " + when 2 then " #{modes} " + when 3 then "#{modes} " + when 4 then modes + end + + modes + .gsub('?', '?'.colorize(colors[:untracked])) + .gsub('A', 'A'.colorize(colors[:addition])) + .gsub('M', 'M'.colorize(colors[:modification])) + .gsub('D', 'D'.colorize(colors[:deletion])) + end + end +end diff --git a/lib/colorls/monkeys.rb b/lib/colorls/monkeys.rb index d0a5718..a1421da 100644 --- a/lib/colorls/monkeys.rb +++ b/lib/colorls/monkeys.rb @@ -4,22 +4,26 @@ class String end def remove(pattern) - self.gsub(pattern, '') + gsub(pattern, '') + end + + def uniq + split('').uniq.join end end class Hash def symbolize_keys - keys.each do |key| - new_key = (key.to_sym rescue key.to_s.to_sym) - self[new_key] = delete(key) + new_hash = {} + each_key do |key| + new_hash[key.to_sym] = delete(key) end - self + new_hash end end class Array def sum - self.inject(:+) + inject(:+) end -end \ No newline at end of file +end diff --git a/lib/yaml/dark_colors.yaml b/lib/yaml/dark_colors.yaml index abac80a..e8c2440 100644 --- a/lib/yaml/dark_colors.yaml +++ b/lib/yaml/dark_colors.yaml @@ -32,7 +32,8 @@ error: red normal: darkkhaki # Git -unchanged: forestgreen -added: chartreuse -changed: darkorange -untracked: darkred +addition: chartreuse +modification: darkkhaki +deletion: darkred +untracked: darkorange +unchanged: forestgreen \ No newline at end of file