File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -159,3 +159,36 @@ \subsection{Example \label{readline-example}}
159159del os, histfile
160160\end {verbatim }
161161
162+ The following example extends the \class {code.InteractiveConsole} class to
163+ support command line editing and history save/restore.
164+
165+ \begin {verbatim }
166+ import code
167+ import readline
168+ import atexit
169+ import os
170+
171+ class HistoryConsole(code.InteractiveConsole):
172+ def __init__(self, locals=None, filename="<console>",
173+ histfile=os.path.expanduser("~/.console-history")):
174+ code.InteractiveConsole.__init__(self)
175+ self.init_history(histfile)
176+
177+ def init_history(self, histfile):
178+ readline.parse_and_bind("tab: complete")
179+ if hasattr(readline, "read_history_file"):
180+ try:
181+ readline.read_history_file(histfile)
182+ except IOError:
183+ pass
184+ atexit.register(self.save_history, histfile)
185+
186+ def raw_input(self, prompt=""):
187+ line = readline.readline(prompt)
188+ if line:
189+ readline.add_history(line)
190+ return line
191+
192+ def save_history(self, histfile):
193+ readline.write_history_file(histfile)
194+ \end {verbatim }
You can’t perform that action at this time.
0 commit comments