Compare commits

...

4 Commits

Author SHA1 Message Date
Alexander Popov f6ee1a16db
shard.lock 2022-09-25 02:23:44 +03:00
Alexander Popov 716d5634c3
set table render 2022-09-25 00:45:02 +03:00
Alexander Popov 2a7a225024
table render 2022-09-25 00:34:03 +03:00
Alexander Popov 56373a75ee
daemon mode 2022-09-25 00:23:45 +03:00
6 changed files with 132 additions and 57 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin/
lib/

6
shard.lock Normal file
View File

@ -0,0 +1,6 @@
version: 2.0
shards:
tallboy:
git: https://github.com/epoch/tallboy.git
version: 0.9.3

View File

@ -1,5 +1,5 @@
name: git-ahead-check
version: 0.1.0
version: 0.3.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

31
src/argv.cr Normal file
View File

@ -0,0 +1,31 @@
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 "-r RENDER", "Table render (unicode, ascii, markdown)" do |render|
ARGV_VARS.render = render
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

40
src/conf.cr Normal file
View File

@ -0,0 +1,40 @@
VERSION = {{ `shards version "#{__DIR__}"`.chomp.stringify }}
class CONFIG
property daemon : Bool
property short : Bool
def initialize(daemon : Bool, short : Bool)
@daemon = daemon
@short = short
@render = "unicode"
end
def daemon=(daemon : Bool)
@daemon = daemon
end
def short=(short : Bool)
@short = short
end
def render=(render : String)
@render = render
end
def daemon
return @daemon
end
def short
return @short
end
def render
return @render
end
end
ARGV_VARS = CONFIG.new(false, false)
CONFIG_PATH = "#{ENV["HOME"]}/.config/emilecok/git-ahead-check"

View File

@ -1,39 +1,58 @@
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
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")
@ -41,29 +60,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