mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
Adds custom config coloring support for faster git-status
This commit is contained in:
parent
a88598e10e
commit
0984a529ac
6 changed files with 98 additions and 26 deletions
|
@ -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'
|
||||
|
||||
|
|
|
@ -8,3 +8,4 @@ require 'colorls/core'
|
|||
require 'colorls/flags'
|
||||
require 'colorls/load_from_yaml'
|
||||
require 'colorls/monkeys'
|
||||
require 'colorls/git'
|
|
@ -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)
|
||||
|
|
33
lib/colorls/git.rb
Normal file
33
lib/colorls/git.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
Loading…
Reference in a new issue