summaryrefslogtreecommitdiffstats
path: root/Doc/lib/libreadline.tex
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2004-05-23 19:06:41 (GMT)
committerSkip Montanaro <skip@pobox.com>2004-05-23 19:06:41 (GMT)
commitb98a8ba14db919d6bf6b277c59ab20da16ed19cd (patch)
tree424a60e4d6c058b581fb62965228691f515ace18 /Doc/lib/libreadline.tex
parent0dc23101a070fd6a5cceb66ee87eac70f94e7bd1 (diff)
downloadcpython-b98a8ba14db919d6bf6b277c59ab20da16ed19cd.zip
cpython-b98a8ba14db919d6bf6b277c59ab20da16ed19cd.tar.gz
cpython-b98a8ba14db919d6bf6b277c59ab20da16ed19cd.tar.bz2
Add example that uses readline.readline().
Diffstat (limited to 'Doc/lib/libreadline.tex')
-rw-r--r--Doc/lib/libreadline.tex33
1 files changed, 33 insertions, 0 deletions
diff --git a/Doc/lib/libreadline.tex b/Doc/lib/libreadline.tex
index cd2a80a..34d3a4c 100644
--- a/Doc/lib/libreadline.tex
+++ b/Doc/lib/libreadline.tex
@@ -159,3 +159,36 @@ atexit.register(readline.write_history_file, histfile)
del os, histfile
\end{verbatim}
+The following example extends the \class{code.InteractiveConsole} class to
+support command line editing and history save/restore.
+
+\begin{verbatim}
+import code
+import readline
+import atexit
+import os
+
+class HistoryConsole(code.InteractiveConsole):
+ def __init__(self, locals=None, filename="<console>",
+ histfile=os.path.expanduser("~/.console-history")):
+ code.InteractiveConsole.__init__(self)
+ self.init_history(histfile)
+
+ def init_history(self, histfile):
+ readline.parse_and_bind("tab: complete")
+ if hasattr(readline, "read_history_file"):
+ try:
+ readline.read_history_file(histfile)
+ except IOError:
+ pass
+ atexit.register(self.save_history, histfile)
+
+ def raw_input(self, prompt=""):
+ line = readline.readline(prompt)
+ if line:
+ readline.add_history(line)
+ return line
+
+ def save_history(self, histfile):
+ readline.write_history_file(histfile)
+\end{verbatim}