DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / python / mal_readline.py
1 import os, sys, readline as pyreadline
2
3 history_loaded = False
4 histfile = os.path.expanduser("~/.mal-history")
5 if sys.version_info[0] >= 3:
6 rl = input
7 else:
8 rl = raw_input
9
10 def readline(prompt="user> "):
11 global history_loaded
12 if not history_loaded:
13 history_loaded = True
14 try:
15 with open(histfile, "r") as hf:
16 for line in hf.readlines():
17 pyreadline.add_history(line.rstrip("\r\n"))
18 pass
19 except IOError:
20 #print("Could not open %s" % histfile)
21 pass
22
23 try:
24 line = rl(prompt)
25 pyreadline.add_history(line)
26 with open(histfile, "a") as hf:
27 hf.write(line + "\n")
28 except IOError:
29 pass
30 except EOFError:
31 return None
32 return line