Skip to main content

Add my gitstats script

ID
23d991a
date
2022-03-20 20:27:46+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
3fd21c9
message
Add my gitstats script
changed files
1 file, 51 additions

Changed files

gitstats (0) → gitstats (1194)

diff --git a/gitstats b/gitstats
new file mode 100755
index 0000000..33c9aa6
--- /dev/null
+++ b/gitstats
@@ -0,0 +1,51 @@
+#!/usr/bin/env ruby
+# This script gives me a brief summary of my current Git changes, by
+# telling me the number of lines I'm adding/deleting, e.g.
+#
+#     +++ 455 additions
+#     ---   9 deletions
+#
+# It's just for my own amusement, and shouldn't be relied on for
+# anything serious.
+
+deletions = 0
+additions = 0
+
+`git diff`.each_line do |line|
+  if line.start_with? "-" and not line.start_with? "--- "
+    deletions += 1
+  elsif line.start_with? "+" and not line.start_with? "+++ "
+    additions += 1
+  end
+end
+
+`git diff --cached`.each_line do |line|
+  if line.start_with? "-" and not line.start_with? "--- "
+    deletions += 1
+  elsif line.start_with? "+" and not line.start_with? "+++ "
+    additions += 1
+  end
+end
+
+# https://stackoverflow.com/q/1489183/1558022
+class String
+  def colorize(color_code)
+    "\e[#{color_code}m#{self}\e[0m"
+  end
+
+  def red
+    colorize(31)
+  end
+
+  def green
+    colorize(32)
+  end
+end
+
+additions = additions.to_s
+deletions = deletions.to_s
+
+max_len = [additions.size, deletions.size].max
+
+puts "+++ #{additions.rjust(max_len)} addition#{additions == "1" ? "" : "s"}".green
+puts "--- #{deletions.rjust(max_len)} deletion#{deletions == "1" ? "" : "s"}".red