-
Notifications
You must be signed in to change notification settings - Fork 2
/
hghci
executable file
·46 lines (38 loc) · 1.09 KB
/
hghci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
import subprocess
import tempfile
import os
import sys
def confirm(prompt=None, resp=False):
if prompt is None:
prompt = 'Confirm'
if resp:
prompt = '%s [%s]|%s: ' % (prompt, 'y', 'n')
else:
prompt = '%s [%s]|%s: ' % (prompt, 'n', 'y')
while True:
ans = raw_input(prompt)
if not ans:
return resp
if ans not in ['y', 'Y', 'n', 'N']:
print 'please enter y or n.'
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False
if confirm("reload history?"):
pass
else:
os.execvp("ghci", sys.argv)
hist = open(os.path.expanduser("~/.ghc/ghci_history")).readlines()
hist2 = reversed(map((lambda l : 'send {' + l[:-1] + '}\nsend "\r"'), hist))
prompt = 'expect ">"'
hist3 = "spawn ghci " + ' '.join(sys.argv[1:]) + "\n" + prompt + "\n" + ("\n"+prompt+"\n").join(hist2) + "\ninteract"
(_,f) = tempfile.mkstemp()
temp = open(f,'w')
temp.write(hist3)
temp.close()
#print hist3
subprocess.call(["expect", f])
os.remove(f)