Skip to content

Commit fa8e0b4

Browse files
author
Sebastian Ramacher
committed
Add option to remove duplicates from history. (Closes: #135)
1 parent 3e02455 commit fa8e0b4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def loadini(struct, configfile):
6161
'highlight_show_source': True,
6262
'hist_file': '~/.pythonhist',
6363
'hist_length': 100,
64+
'hist_duplicates': True,
6465
'paste_time': 0.02,
6566
'syntax': True,
6667
'tab_length': 4,
@@ -110,6 +111,7 @@ def loadini(struct, configfile):
110111
'highlight_show_source')
111112
struct.hist_file = config.get('general', 'hist_file')
112113
struct.hist_length = config.getint('general', 'hist_length')
114+
struct.hist_duplicates = config.getboolean('general', 'hist_duplicates')
113115
struct.flush_output = config.getboolean('general', 'flush_output')
114116
struct.pastebin_key = config.get('keyboard', 'pastebin')
115117
struct.save_key = config.get('keyboard', 'save')

bpython/repl.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,25 @@ def writetb(self, lines):
146146

147147
class History(object):
148148

149-
def __init__(self, entries=None):
149+
def __init__(self, entries=None, duplicates=False):
150150
if entries is None:
151151
self.entries = ['']
152152
else:
153153
self.entries = list(entries)
154154
self.index = 0
155155
self.saved_line = ''
156+
self.duplicates = duplicates
156157

157158
def append(self, line):
158159
line = line.rstrip('\n')
159160
if line:
161+
if not self.duplicates:
162+
# remove duplicates
163+
try:
164+
while True:
165+
self.entries.remove(line)
166+
except ValueError:
167+
pass
160168
self.entries.append(line)
161169

162170
def first(self):
@@ -340,7 +348,7 @@ def __init__(self, interp, config):
340348
self.interp = interp
341349
self.interp.syntaxerror_callback = self.clear_current_line
342350
self.match = False
343-
self.rl_history = History()
351+
self.rl_history = History(duplicates=config.hist_duplicates)
344352
self.s_hist = []
345353
self.history = []
346354
self.evaluating = False

0 commit comments

Comments
 (0)