From 56373a75ee73debb275e8e2b7582750cc1d71472 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sun, 25 Sep 2022 00:23:45 +0300 Subject: [PATCH] daemon mode --- shard.yml | 5 ++- src/argv.cr | 27 +++++++++++++ src/conf.cr | 31 ++++++++++++++ src/git-ahead-check.cr | 91 ++++++++++++++++-------------------------- 4 files changed, 97 insertions(+), 57 deletions(-) create mode 100644 src/argv.cr create mode 100644 src/conf.cr diff --git a/shard.yml b/shard.yml index da11884..557298b 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: git-ahead-check -version: 0.1.0 +version: 0.2.0 authors: - Alexander Popov @@ -13,3 +13,6 @@ targets: git-ahead-check: main: src/git-ahead-check.cr +dependencies: + tallboy: + github: epoch/tallboy diff --git a/src/argv.cr b/src/argv.cr new file mode 100644 index 0000000..d12e3d8 --- /dev/null +++ b/src/argv.cr @@ -0,0 +1,27 @@ +require "option_parser" + +begin + OptionParser.parse do |parser| + parser.banner = "The very simple password manager for humans\n" + + parser.on "-h", "--help", "Show help" do + puts parser + exit(0) + end + + parser.on "-s", "Short output" do + ARGV_VARS.short = true + end + + parser.on "-d", "Daemon" do + ARGV_VARS.daemon = true + end + + parser.on "-v", "Show version" do + puts VERSION + exit(0) + end + end +rescue ex + puts ex.message, "" +end diff --git a/src/conf.cr b/src/conf.cr new file mode 100644 index 0000000..f54b69d --- /dev/null +++ b/src/conf.cr @@ -0,0 +1,31 @@ +VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }} + +class CONFIG + property daemon : Bool + property short : Bool + + def initialize(daemon : Bool, short : Bool) + @daemon = daemon + @short = short + end + + def daemon=(daemon : Bool) + @daemon = daemon + end + + def short=(short : Bool) + @short = short + end + + def daemon(daemon : Bool) + return @daemon + end + + def short(short : Bool) + return @short + end +end + +ARGV_VARS = CONFIG.new(false, false) + +CONFIG_PATH = "#{ENV["HOME"]}/.config/emilecok/git-ahead-check" diff --git a/src/git-ahead-check.cr b/src/git-ahead-check.cr index 9ad98fa..e64a90d 100644 --- a/src/git-ahead-check.cr +++ b/src/git-ahead-check.cr @@ -1,39 +1,44 @@ require "colorize" -require "option_parser" +require "tallboy" -VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }} -short_output = false - -begin - OptionParser.parse do |parser| - parser.banner = "The very simple password manager for humans\n" - - parser.on "-h", "--help", "Show help" do - puts parser - exit(0) - end - - parser.on "-s", "Short output" do - short_output = true - end - - parser.on "-v", "Show version" do - puts VERSION - exit(0) - end - end -rescue ex - puts ex.message, "" -end - -CONFIG_PATH = "#{ENV["HOME"]}/.config/emilecok/git-ahead-check" +require "./conf.cr" +require "./argv.cr" if File.exists?(CONFIG_PATH) - a = File.read_lines(CONFIG_PATH) + repository_dirs = File.read_lines(CONFIG_PATH) - repos_dirs = File.read_lines(CONFIG_PATH) + repositoryes = Array({name: String, path: String, ahead: Int32}).new - repos_dirs.each { |x| check_repo(x, short_output) } + 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 + repositoryes.each { |repo| + print "* #{repo[:ahead]}\t".colorize(:red).mode(:bold) + print repo[:name].colorize(:yellow) + + if !ARGV_VARS.short + print " [" + print repo[:path].colorize(:light_magenta) + print "]" + end + } + + puts + end else Dir.mkdir_p(File.dirname(CONFIG_PATH)) File.new(CONFIG_PATH, "w") @@ -41,29 +46,3 @@ else print File.dirname(CONFIG_PATH) puts " directory created.\n" end - -def check_repo(repo_path : String, show_path) - if !Dir.exists?(repo_path.sub("$HOME", ENV["HOME"])) - return 0 - end - - if !Dir.exists?("#{repo_path}/.git") - return 0 - end - - 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? - print "* #{ahead_count}\t".colorize(:red).mode(:bold) - print repo_name.colorize(:yellow) - - if !show_path - print " [" - print repo_path.colorize(:light_magenta) - puts "]" - else - puts - end - end -end