From 9b9e15423b3c39a9bdbf337fc18ee325ce936295 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Mon, 25 Aug 2008 18:40:11 +0100 Subject: [PATCH] History file support added. Defaults to ~/.pythonhist but can be configured in rc (see sample-rc) --- CHANGELOG | 5 +++++ bpython.py | 16 ++++++++++++++-- sample-rc | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d2f39c3..794f0af 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,8 @@ +v0.7.1 +====== +Added support for a history file, defaults to ~/.pythonhist and 100 lines but +is configurable from the rc file (see sample-rc). + v0.7.0 ====== C-d behaviour changed so it no longer exits if the current line isn't empty. diff --git a/bpython.py b/bpython.py index 8ff5267..b774dd6 100644 --- a/bpython.py +++ b/bpython.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# bpython 0.7.0::fancy curses interface to the Python repl::Bob Farrell 2008 +# bpython 0.7.1::fancy curses interface to the Python repl::Bob Farrell 2008 # # The MIT License # @@ -101,6 +101,8 @@ OPTS.tab_length = 4 OPTS.auto_display_list = True OPTS.syntax = True OPTS.arg_spec = True +OPTS.hist_file = '~/.pythonhist' +OPTS.hist_length = 100 # TODO: # @@ -246,6 +248,10 @@ class Repl( object ): if not OPTS.arg_spec: return + pythonhist = os.path.expanduser('~/.pythonhist') + if os.path.exists(pythonhist): + self.rl_hist = open(pythonhist, 'r').readlines() + pexp = Forward() chars = printables.replace('(', '') chars = chars.replace(')', '') @@ -829,7 +835,8 @@ class Repl( object ): self.s_hist[-1] += self.f_string self.stdout_hist += inp + '\n' # Keep two copies so you can go up and down in the hist: - self.rl_hist.append( inp ) + if inp: + self.rl_hist.append( inp + '\n' ) more = self.push( inp ) def size( self ): @@ -1566,6 +1573,11 @@ def main( scr ): repl.repl() + if OPTS.hist_length: + f = open(os.path.expanduser('~/.pythonhist'), 'w') + f.writelines(repl.rl_hist[-OPTS.hist_length:]) + f.close() + return repl.getstdout() if __name__ == '__main__': diff --git a/sample-rc b/sample-rc index 3769b1e..841cd8c 100644 --- a/sample-rc +++ b/sample-rc @@ -13,3 +13,13 @@ syntax = on # Display the arg spec (list of arguments) for callables, # when possible (default: on). arg_spec = true + +# History file: +hist_file = ~/.pythonhist + +# Number of lines to store in history (set to 0 to disable): +hist_len = 100 + +# Soft tab size: +tab_length = 4 +