Skip to content

Commit a438949

Browse files
committed
Create a first implementation of keybinding to the reverse-i search mode.
1 parent 54c8c46 commit a438949

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

bpython/cli.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,15 @@ def fwd(self):
575575
self.s = self.rl_history.forward()
576576
self.print_line(self.s, clr=True)
577577

578+
def search(self):
579+
"""Search with the partial matches from the history object."""
580+
581+
self.cpo = 0
582+
self.clear_wrapped_lines()
583+
self.rl_history.enter(self.s)
584+
self.s = self.rl_history.back(start=False, search=True)
585+
self.print_line(self.s, clr=True)
586+
578587
def get_key(self):
579588
key = ''
580589
while True:
@@ -848,6 +857,10 @@ def p_key(self, key):
848857
self.undo()
849858
return ''
850859

860+
elif key in key_dispatch[config.search_key]:
861+
self.search()
862+
return ''
863+
851864
elif key in ('KEY_UP', ) + key_dispatch[config.up_one_line_key]:
852865
# Cursor Up/C-p
853866
self.back()

bpython/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def loadini(struct, configfile):
8484
'show_source': 'F2',
8585
'suspend': 'C-z',
8686
'undo': 'C-r',
87+
'search': 'C-o',
8788
'up_one_line': 'C-p',
8889
'yank_from_buffer': 'C-y'},
8990
'cli': {
@@ -115,6 +116,7 @@ def loadini(struct, configfile):
115116
struct.flush_output = config.getboolean('general', 'flush_output')
116117
struct.pastebin_key = config.get('keyboard', 'pastebin')
117118
struct.save_key = config.get('keyboard', 'save')
119+
struct.search_key = config.get('keyboard', 'search')
118120
struct.show_source_key = config.get('keyboard', 'show_source')
119121
struct.suspend_key = config.get('keyboard', 'suspend')
120122
struct.undo_key = config.get('keyboard', 'undo')

bpython/repl.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ def first(self):
173173
self.index = len(self.entries)
174174
return self.entries[-self.index]
175175

176-
def back(self, match=True):
176+
def back(self, start=True, search=False):
177177
"""Move one step back in the history."""
178178
if not self.is_at_end:
179-
if match:
179+
if search:
180+
self.index += self.find_partial_match_backward(self.saved_line)
181+
elif match:
180182
self.index += self.find_match_backward(self.saved_line)
181183
else:
182184
self.index += 1
@@ -189,10 +191,20 @@ def find_match_backward(self, search_term):
189191
return idx + 1
190192
return 0
191193

192-
def forward(self, match = True):
194+
def find_partial_match_backward(self, search_term):
195+
filtered_list_len = len(self.entries) - self.index
196+
for idx, val in enumerate(reversed(self.entries[:filtered_list_len])):
197+
if search_term in val:
198+
return idx + 1
199+
return 0
200+
201+
202+
def forward(self, start=True, search=False):
193203
"""Move one step forward in the history."""
194204
if self.index > 1:
195-
if match:
205+
if search:
206+
self.index -= self.find_partial_match_forward(self.saved_line)
207+
elif match:
196208
self.index -= self.find_match_forward(self.saved_line)
197209
else:
198210
self.index -= 1
@@ -208,6 +220,15 @@ def find_match_forward(self, search_term):
208220
return idx + 1
209221
return self.index
210222

223+
def find_partial_match_forward(self, search_term):
224+
filtered_list_len = len(self.entries) - self.index + 1
225+
for idx, val in enumerate(self.entries[filtered_list_len:]):
226+
if search_term in val:
227+
return idx + 1
228+
return self.index
229+
230+
231+
211232
def last(self):
212233
"""Move forward to the end of the history."""
213234
if not self.is_at_start:

0 commit comments

Comments
 (0)