#! /usr/bin/python

import sys, os, string, time
import locale
locale.setlocale(locale.LC_ALL,"")

#some default definitions
colours = {    
            'none'       :    "",
            'default'    :    "\033[0m",
            'bold'       :    "\033[1m",
            'underline'  :    "\033[4m",
            'blink'      :    "\033[5m",
            'reverse'    :    "\033[7m",
            'concealed'  :    "\033[8m",

            'black'      :    "\033[30m", 
            'red'        :    "\033[31m",
            'green'      :    "\033[32m",
            'yellow'     :    "\033[33m",
            'blue'       :    "\033[34m",
            'magenta'    :    "\033[35m",
            'cyan'       :    "\033[36m",
            'white'      :    "\033[37m",

            'on_black'   :    "\033[40m", 
            'on_red'     :    "\033[41m",
            'on_green'   :    "\033[42m",
            'on_yellow'  :    "\033[43m",
            'on_blue'    :    "\033[44m",
            'on_magenta' :    "\033[45m",
            'on_cyan'    :    "\033[46m",
            'on_white'   :    "\033[47m",

            'beep'       :    "\007"
            }


timeformat = "%a %b %d %H:%M:%S %Y"
delim1 = colours["bold"]+colours["red"] # before time string
delim2 = colours["default"]+colours["yellow"]+":" # after time string
delim3 = ": "+colours["default"] # before command
delim4 = colours["default"] # at the end of line
delim5 = colours["bold"]+colours["blue"] # before unrecognized line format


for i in [ os.environ['HOME']+"/.zsh/misc/zshistrc"]:
    if os.path.isfile(i):
        execfile(i)


if len(sys.argv) > 1:
    historyfile = sys.argv[1]
    if os.path.isdir(historyfile):
        historyfile = historyfile + "/.zsh/history"
else:
    historyfile = os.environ['HOME']+"/.zsh/history"

f = open(historyfile,"r")
l = f.readlines()
for i in l:
    try:
        s1 = string.split(i, ":",2)
        timestring = s1[1]
        s2 = string.split(s1[2], ";", 1)
        delaystring = s2[0]
        commandstring = s2[1][:-1]
        t = long(timestring) # seconds since the epoch
        print delim1+time.strftime(timeformat,time.localtime(t))+delim2+delaystring+delim3+commandstring+delim4
    except:
        print delim5+i[:-1]+delim4

