Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b98a8ba

Browse files
author
Skip Montanaro
committed
Add example that uses readline.readline().
1 parent 0dc2310 commit b98a8ba

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Doc/lib/libreadline.tex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,36 @@ \subsection{Example \label{readline-example}}
159159
del 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}

0 commit comments

Comments
 (0)