# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1394325300 18000 # Node ID 39800c8f0532e7042571f8c4ca56b4be23e8c136 # Parent 5042068975861865b345ebb232bb4bf3a71413d0 Add initial session replay diff -r 504206897586 -r 39800c8f0532 basic-session --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basic-session Sat Mar 08 19:35:00 2014 -0500 @@ -0,0 +1,11 @@ + +# Let's grab the Python Code of Conduct and have a look + +hg clone http://hg.python.org/coc/ +cd coc +hg log -G + +# Now let's add a couple of bookmarks +hg book upstream +hg book jordi + diff -r 504206897586 -r 39800c8f0532 sh-replay --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sh-replay Sat Mar 08 19:35:00 2014 -0500 @@ -0,0 +1,123 @@ +#!/usr/bin/env python + +import os,sys,subprocess +from colors import yellow, blue, magenta +from sh import hg, cd +import sh + +curr_cmd = "" + +def getch(): + import sys, tty, termios + fd = sys.stdin.fileno() + old_settings = termios.tcgetattr(fd) + try: + tty.setraw(sys.stdin.fileno()) + ch = sys.stdin.read(1) + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) + if ord(ch) == 3: + sys.exit(0) + if ord(ch) == 12: + subprocess.call("reset") + print_prompt() + sys.stdout.write(curr_cmd) + sys.stdout.flush() + + return ch + +def get_hg_id(): + try: + book = hg.id(B=True).strip() + except: + book = False + + if book: + return book + + try: + branch = hg.branch().strip() + except: + return "" + + return branch + +def print_prompt(): + cwd = os.getcwd() + if cwd == "/home/jordi": + cwd = "~" + else: + cwd = os.path.split(cwd)[-1] + + hg_id = get_hg_id() + + sys.stdout.write(yellow("jordi@REVSETS", style="bold") + ":" + + blue(cwd, style="bold") + " " + + magenta(hg_id, style="bold") + "$ ") + +def main(): + subprocess.call("reset") + with open(sys.argv[1]) as f: + cmds = [cmd.strip() for cmd in f.readlines() + if cmd.strip() and not cmd.strip().startswith("#")] + + global curr_cmd + for cmd in cmds: + print_prompt() + curr_cmd = "" + for c in cmd: + key = getch() + sys.stdout.write(c) + sys.stdout.flush() + curr_cmd += c + + while True: + key = getch() + if ord(key) == 13: + sys.stdout.write("\n") + sys.stdout.flush() + break + + if cmd.startswith("cd "): + cd(cmd[3:]) + else: + with open("/tmp/cmd", "w") as f: + f.write("shopt -s expand_aliases\n") + f.write(cmd + "\n") + f.flush() + + subprocess.call(["/bin/bash", "-l", "/tmp/cmd"]) + + while True: + key = getch() + if ord(key) == 13: + sys.stdout.write("\n") + sys.stdout.flush() + break + + print + print + print + print + print + print + print + print + print yellow(" TTTTTT HH HH A NN NN KK KK SSS ", style="bold") + print yellow(" TT HH HH A A NNNN NN KK KK SS ", style="bold") + print yellow(" TT HHHHH AAAAA NN NNNN KKKKK SS ", style="bold") + print yellow(" TT HH HH AA AA NN NNN KK KK SS ", style="bold") + print yellow(" TT HH HH AA AA NN NN KK KK SSS ", style="bold") + print + print + print + print + print + print + print + print + + +if __name__ == "__main__": + main() +