git-ahead-check/src/git-ahead-check.cr

63 lines
1.6 KiB
Crystal

require "colorize"
require "tallboy"
require "./conf.cr"
require "./argv.cr"
if File.exists?(CONFIG_PATH)
repository_dirs = File.read_lines(CONFIG_PATH)
repositoryes = Array({name: String, path: String, ahead: Int32}).new
repository_dirs.each { |repo_path|
if Dir.exists?("#{repo_path}/.git".sub("$HOME", ENV["HOME"]))
repo_name = repo_path.split("/")[-1]
ahead_count = `git -C #{repo_path} status | grep -i "Your branch" | grep -Eo "[0-9]"`.chomp
if !ahead_count.empty?
repositoryes << {name: repo_name, path: repo_path, ahead: ahead_count.to_i}
end
end
}
if ARGV_VARS.daemon
puts "Daemon mode"
sleep(600)
`notify-send --app-name=git-ahead-check --icon=flag-red --urgency=normal GIT-AHEAD "Unpushed repos: #{repositoryes.size}"`
else
repos_table = Tallboy.table do
header [
"Ahead".colorize.mode(:bold),
"Repo name".colorize.mode(:bold),
if !ARGV_VARS.short
"Repo path".colorize.mode(:bold)
end,
]
repositoryes.each { |repo|
row [
repo[:ahead].colorize(:red).mode(:bold),
repo[:name].colorize(:yellow),
if !ARGV_VARS.short
repo[:path].sub("$HOME", ENV["HOME"]).colorize(:light_magenta)
end,
]
}
end
if ARGV_VARS.render == "ascii"
puts repos_table.render(:ascii)
elsif ARGV_VARS.render == "markdown"
puts repos_table.render(:markdown)
else
puts repos_table.render(:unicode)
end
end
else
Dir.mkdir_p(File.dirname(CONFIG_PATH))
File.new(CONFIG_PATH, "w")
print File.dirname(CONFIG_PATH)
puts " directory created.\n"
end