Skip to content

Commit 8deee0e

Browse files
committed
Jumps back/forward in history to match the partial string entered in the command line.
1 parent 3f61d8d commit 8deee0e

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

bpython/repl.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,41 @@ def first(self):
165165
self.index = len(self.entries)
166166
return self.entries[-self.index]
167167

168-
def back(self):
168+
def back(self, match=True):
169169
"""Move one step back in the history."""
170170
if not self.is_at_end:
171-
self.index += 1
172-
return self.entries[-self.index]
171+
if match:
172+
self.index += self.find_match_backward(self.saved_line)
173+
else:
174+
self.index += 1
175+
return self.entries[-self.index] if self.index else self.saved_line
176+
177+
def find_match_backward(self, search_term):
178+
filtered_list_len = len(self.entries) - self.index
179+
for idx, val in enumerate(reversed(self.entries[:filtered_list_len])):
180+
if val.startswith(search_term):
181+
return idx + 1
182+
return 0
173183

174-
def forward(self):
184+
def forward(self, match = True):
175185
"""Move one step forward in the history."""
176186
if self.index > 1:
177-
self.index -= 1
178-
return self.entries[-self.index]
187+
if match:
188+
self.index -= self.find_match_forward(self.saved_line)
189+
else:
190+
self.index -= 1
191+
return self.entries[-self.index] if self.index else self.saved_line
179192
else:
180193
self.index = 0
181194
return self.saved_line
182195

196+
def find_match_forward(self, search_term):
197+
filtered_list_len = len(self.entries) - self.index + 1
198+
for idx, val in enumerate(self.entries[filtered_list_len:]):
199+
if val.startswith(search_term):
200+
return idx + 1
201+
return self.index
202+
183203
def last(self):
184204
"""Move forward to the end of the history."""
185205
if not self.is_at_start:

0 commit comments

Comments
 (0)