comparison sh-replay @ 9:39800c8f0532

Add initial session replay
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 08 Mar 2014 19:35:00 -0500
parents
children 82176b2a61da
comparison
equal deleted inserted replaced
8:504206897586 9:39800c8f0532
1 #!/usr/bin/env python
2
3 import os,sys,subprocess
4 from colors import yellow, blue, magenta
5 from sh import hg, cd
6 import sh
7
8 curr_cmd = ""
9
10 def getch():
11 import sys, tty, termios
12 fd = sys.stdin.fileno()
13 old_settings = termios.tcgetattr(fd)
14 try:
15 tty.setraw(sys.stdin.fileno())
16 ch = sys.stdin.read(1)
17 finally:
18 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
19 if ord(ch) == 3:
20 sys.exit(0)
21 if ord(ch) == 12:
22 subprocess.call("reset")
23 print_prompt()
24 sys.stdout.write(curr_cmd)
25 sys.stdout.flush()
26
27 return ch
28
29 def get_hg_id():
30 try:
31 book = hg.id(B=True).strip()
32 except:
33 book = False
34
35 if book:
36 return book
37
38 try:
39 branch = hg.branch().strip()
40 except:
41 return ""
42
43 return branch
44
45 def print_prompt():
46 cwd = os.getcwd()
47 if cwd == "/home/jordi":
48 cwd = "~"
49 else:
50 cwd = os.path.split(cwd)[-1]
51
52 hg_id = get_hg_id()
53
54 sys.stdout.write(yellow("jordi@REVSETS", style="bold") + ":"
55 + blue(cwd, style="bold") + " "
56 + magenta(hg_id, style="bold") + "$ ")
57
58 def main():
59 subprocess.call("reset")
60 with open(sys.argv[1]) as f:
61 cmds = [cmd.strip() for cmd in f.readlines()
62 if cmd.strip() and not cmd.strip().startswith("#")]
63
64 global curr_cmd
65 for cmd in cmds:
66 print_prompt()
67 curr_cmd = ""
68 for c in cmd:
69 key = getch()
70 sys.stdout.write(c)
71 sys.stdout.flush()
72 curr_cmd += c
73
74 while True:
75 key = getch()
76 if ord(key) == 13:
77 sys.stdout.write("\n")
78 sys.stdout.flush()
79 break
80
81 if cmd.startswith("cd "):
82 cd(cmd[3:])
83 else:
84 with open("/tmp/cmd", "w") as f:
85 f.write("shopt -s expand_aliases\n")
86 f.write(cmd + "\n")
87 f.flush()
88
89 subprocess.call(["/bin/bash", "-l", "/tmp/cmd"])
90
91 while True:
92 key = getch()
93 if ord(key) == 13:
94 sys.stdout.write("\n")
95 sys.stdout.flush()
96 break
97
98 print
99 print
100 print
101 print
102 print
103 print
104 print
105 print
106 print yellow(" TTTTTT HH HH A NN NN KK KK SSS ", style="bold")
107 print yellow(" TT HH HH A A NNNN NN KK KK SS ", style="bold")
108 print yellow(" TT HHHHH AAAAA NN NNNN KKKKK SS ", style="bold")
109 print yellow(" TT HH HH AA AA NN NNN KK KK SS ", style="bold")
110 print yellow(" TT HH HH AA AA NN NN KK KK SSS ", style="bold")
111 print
112 print
113 print
114 print
115 print
116 print
117 print
118 print
119
120
121 if __name__ == "__main__":
122 main()
123