Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Last active April 15, 2018 11:17
Show Gist options
  • Save pathikrit/f2644500d5b392795e2e61263f112bb4 to your computer and use it in GitHub Desktop.
Save pathikrit/f2644500d5b392795e2e61263f112bb4 to your computer and use it in GitHub Desktop.
Scala Script to print Git PunchCard
/**
* Quick and dirty Scala app to print git commit punch-card e.g.
*
* ┃08┃09┃10┃11┃12┃13┃14┃15┃16┃17┃18┃19┃20┃21┃22┃23┃00┃01┃02┃03┃04┃05┃06┃07┃
* Sun┃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Mon┃▁▁▁▄▄▄▅▅▅▅▅▅▄▄▄▆▆▆▇▇▇▇▇▇███▆▆▆▅▅▅▄▄▄▃▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Tue┃▁▁▁▃▃▃▆▆▆▅▅▅▅▅▅▅▅▅▆▆▆▇▇▇▆▆▆▆▆▆▅▅▅▅▅▅▄▄▄▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Wed┃▁▁▁▄▄▄▅▅▅▇▇▇▅▅▅▅▅▅███▇▇▇▅▅▅▆▆▆▇▇▇▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Thu┃▁▁▁▂▂▂▄▄▄▆▆▆▅▅▅▆▆▆▇▇▇▇▇▇▆▆▆▇▇▇▅▅▅▄▄▄▃▃▃▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Fri┃▁▁▁▂▂▂▄▄▄▅▅▅▅▅▅▄▄▄▄▄▄▅▅▅▅▅▅▃▃▃▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
* Sat┃▁▁▁▁▁▁▁▁▁▃▃▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
*/
object GitPunchCard extends App {
import java.time._
/********************************[Config]*************************************/
val startHour = 8 // Starting hour [0,23]
val startDay = DayOfWeek.SUNDAY // Starting week day
val gitLogFilter = "" // Specify author or date filter for git log
val dir = Option.empty[String] // If specified run in particular git directory
val locale = java.util.Locale.getDefault
/******************************************************************************/
val commits = {
import scala.sys.process._
val cmd = s"git ${dir.map("-C " + _).getOrElse("")} log --format=%cI $gitLogFilter"
cmd.!!.split("\n").map(ZonedDateTime.parse)
}
val table = commits.groupBy(t => (t.getDayOfWeek, t.getHour)).mapValues(_.length).withDefaultValue(0)
val bars = "▁▂▃▄▅▆▇█"
def sparkLine(x: Int, max: Int = table.values.max) = bars(((bars.length*(x-1))/max) max 0)
def makeLine(prefix: String, f: Int => String) = s"$prefix┃" + (0 until 24).map(h => f((h + startHour)%24)).mkString
def shortDay(d: DayOfWeek) = d.getDisplayName(format.TextStyle.SHORT, locale)
val lines = makeLine(" " * shortDay(startDay).length, h => f"$h%02d┃") +:
DayOfWeek.values()
.map(_.plus(startDay.getValue.toLong - 1))
.map(day => makeLine(shortDay(day), h => sparkLine(table(day -> h)).toString * 3))
lines.foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment