2014-04-17 07:42:17 +04:00
|
|
|
import os, sys, readline as pyreadline
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
history_loaded = False
|
|
|
|
histfile = os.path.expanduser("~/.mal-history")
|
2014-04-17 07:42:17 +04:00
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
rl = input
|
|
|
|
else:
|
|
|
|
rl = raw_input
|
2014-03-25 01:32:24 +04:00
|
|
|
|
|
|
|
def readline(prompt="user> "):
|
2014-04-18 07:23:07 +04:00
|
|
|
global history_loaded
|
2014-03-25 01:32:24 +04:00
|
|
|
if not history_loaded:
|
2014-04-18 07:23:07 +04:00
|
|
|
history_loaded = True
|
2014-03-25 01:32:24 +04:00
|
|
|
try:
|
|
|
|
with open(histfile, "r") as hf:
|
|
|
|
for line in hf.readlines():
|
|
|
|
pyreadline.add_history(line.rstrip("\r\n"))
|
|
|
|
pass
|
|
|
|
except IOError:
|
2015-03-15 01:14:32 +03:00
|
|
|
#print("Could not open %s" % histfile)
|
2014-03-25 01:32:24 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2014-04-17 07:42:17 +04:00
|
|
|
line = rl(prompt)
|
2014-03-25 01:32:24 +04:00
|
|
|
pyreadline.add_history(line)
|
|
|
|
with open(histfile, "a") as hf:
|
|
|
|
hf.write(line + "\n")
|
2015-03-12 06:22:35 +03:00
|
|
|
except IOError:
|
|
|
|
pass
|
2014-03-25 01:32:24 +04:00
|
|
|
except EOFError:
|
|
|
|
return None
|
2015-03-12 06:22:35 +03:00
|
|
|
return line
|