daemon mode

This commit is contained in:
Alexander Popov 2022-09-25 00:23:45 +03:00
parent b3f6b2b808
commit 56373a75ee
Signed by: iiiypuk
GPG Key ID: D8C9B59A9F04A70C
4 changed files with 97 additions and 57 deletions

View File

@ -1,5 +1,5 @@
name: git-ahead-check
version: 0.1.0
version: 0.2.0
authors:
- Alexander Popov <iiiypuk@fastmail.fm>
@ -13,3 +13,6 @@ targets:
git-ahead-check:
main: src/git-ahead-check.cr
dependencies:
tallboy:
github: epoch/tallboy

27
src/argv.cr Normal file
View File

@ -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

31
src/conf.cr Normal file
View File

@ -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"

View File

@ -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