#!/usr/bin/env python
"""
Copyright (C) 2009  Matt Behrens <askedrelic@gmail.com> http://asktherelic.com

Script for creating a github style punchcard of your bash history.

Requires
-the HISTTIMEFORMAT variable to be set, which includes unix timestamps 
on all bash commands.

-pygooglechart
http://pygooglechart.slowchop.com/

Ideas and most code from:
http://dustin.github.com/2009/01/11/timecard.html
http://github.com/dustin/bindir/blob/master/gitaggregates.py
"""

import argparse
from collections import defaultdict
from itertools import groupby
from os.path import expanduser
import re
import sys
import time

class TimeHistory(object):

    def __init__(self, is12h=True):
        self.h = defaultdict(lambda: 0)
        self.is12h = is12h

    def add_logs(self):
        #Find users default bash history file
        with open(expanduser('~/.bash_history'), 'r') as histfile:
            R = re.compile(r'#\d+$');
            lines = [time.strftime("%w %H", time.localtime(float(line[1:]))) \
                     for line in histfile if R.match(line)]
            for k, g in groupby(sorted(lines)):
                self.h[k] += sum(1 for _ in g)

    def dump(self):
        for h in range(24):
            for d in range(7):
                sys.stderr.write("%02d %d - %s\n"
                                 % (h, d, self.h["%d %02d" % (d, h)]))

    def to_gchart(self):
        from pygooglechart import ScatterChart
        chart = ScatterChart(800, 300, x_range=(-1, 24), y_range=(-1, 7))

        chart.add_data([(h % 24) for h in range(24 * 8)])

        d=[]
        for i in range(8):
            d.extend([i] * 24)
        chart.add_data(d)

        day_names = "Sun Mon Tue Wed Thu Fri Sat".split(" ")
        days = (0, 6, 5, 4, 3, 2, 1)

        sizes=[]
        for d in days:
            sizes.extend([self.h["%d %02d" % (d, h)] for h in range(24)])
        sizes.extend([0] * 24)
        chart.add_data(sizes)

        if self.is12h:
          xlabels = ('|12am|1|2|3|4|5|6|7|8|9|10|11|'
                     '12pm|1|2|3|4|5|6|7|8|9|10|11|')
        else:
          xlabels = ('|0|1|2|3|4|5|6|7|8|9|10|11|'
                     '12|13|14|15|16|17|18|19|20|21|22|23|')
        chart.set_axis_labels('x', [xlabels])
        chart.set_axis_labels('y', [''] + [day_names[n] for n in days] + [''])

        chart.add_marker(1, 1.0, 'o', '333333', 25)
        chart.download('historychart.png')
        #return chart.get_url()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-2', '--24', action='store_false',
                        help='24-hour clock', dest='is12h')
    args = parser.parse_args()

    th = TimeHistory(is12h=args.is12h)
    th.add_logs()
    #th.dump()
    th.to_gchart()

@@ -661,16 +661,39 @@
 __(self,
+ width=800, height=300,
  is12h=T
@@ -698,16 +698,16 @@
 =True):%0A
-
         
@@ -738,16 +738,72 @@
 bda: 0)%0A
+        self.width = width%0A        self.height = height%0A
         
@@ -1538,17 +1538,61 @@
 art(
-800, 300,
+self.width, self.height,%0A                            
  x_r
@@ -2633,16 +2633,280 @@
 arser()%0A
+    parser.add_argument('-W', '--width', default=800, type=int,%0A                        help='chart width (default: %25(default)d)')%0A    parser.add_argument('-H', '--height', default=300, type=int,%0A                        help='chart height (default: %25(default)d)')%0A
     pars
@@ -3049,16 +3049,16 @@
 args()%0A%0A
-
     th =
@@ -3070,16 +3070,54 @@
 History(
+width=args.width, height=args.height, 
 is12h=ar

make width, height as options