From a66607159d206f376ce4722805918a7011527071 Mon Sep 17 00:00:00 2001 From: SusInMotion Date: Thu, 11 Sep 2014 17:17:37 -0400 Subject: [PATCH] pressing prints the last word of the last command to the command line Added test for get last word --- bpython/curtsiesfrontend/repl.py | 25 ++++++++++++++++++++++++- bpython/test/test_curtsies_repl.py | 9 +++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bpython/curtsiesfrontend/repl.py b/bpython/curtsiesfrontend/repl.py index 419c9fce1..fd22d9802 100644 --- a/bpython/curtsiesfrontend/repl.py +++ b/bpython/curtsiesfrontend/repl.py @@ -46,6 +46,7 @@ #TODO other autocomplete modes (also fix in other bpython implementations) + from curtsies.configfile_keynames import keymap as key_dispatch logger = logging.getLogger(__name__) @@ -87,6 +88,7 @@ def __init__(self, coderunner, repl, configured_edit_keys=None): def process_event(self, e): assert self.has_focus + logger.debug('fake input processing event %r', e) if isinstance(e, events.PasteEvent): for ee in e.events: @@ -100,6 +102,9 @@ def process_event(self, e): self.current_line = '' self.cursor_offset = 0 self.repl.run_code_and_maybe_finish() + elif e in ("",): + self.get_last_word() + elif e in [""]: pass elif e in ['']: @@ -469,6 +474,8 @@ def process_key_event(self, e): self.down_one_line() elif e in ("",): self.on_control_d() + elif e in ("",): + self.get_last_word() elif e in ("",): self.incremental_search(reverse=True) elif e in ("",): @@ -524,6 +531,21 @@ def process_key_event(self, e): else: self.add_normal_character(e) + def get_last_word(self): + + def last_word(line): + if not line: + return '' + return line.split().pop() + + previous_word = last_word(self.rl_history.entry) + word = last_word(self.rl_history.back()) + line=self.current_line + self._set_current_line(line[:len(line)-len(previous_word)]+word, reset_rl_history=False) + + self._set_cursor_offset(self.cursor_offset-len(previous_word)+len(word), reset_rl_history=False) + + def incremental_search(self, reverse=False, include_current=False): if self.special_mode == None: self.rl_history.enter(self.current_line) @@ -808,7 +830,8 @@ def run_code_and_maybe_finish(self, for_code=None): if err: indent = 0 - #TODO This should be printed ABOVE the error that just happened instead + + #TODO This should be printed ABOVE the error that just happened instead # or maybe just thrown away and not shown if self.current_stdouterr_line: self.display_lines.extend(paint.display_linize(self.current_stdouterr_line, self.width)) diff --git a/bpython/test/test_curtsies_repl.py b/bpython/test/test_curtsies_repl.py index b0c8a4c8d..21a2b3f57 100644 --- a/bpython/test/test_curtsies_repl.py +++ b/bpython/test/test_curtsies_repl.py @@ -51,6 +51,15 @@ def test_external_communication(self): self.repl.send_current_block_to_external_editor() self.repl.send_session_to_external_editor() + def test_get_last_word(self): + self.repl.rl_history.entries=['1','2 3','4 5 6'] + self.repl._set_current_line('abcde') + self.repl.get_last_word() + self.assertEqual(self.repl.current_line,'abcde6') + self.repl.get_last_word() + self.assertEqual(self.repl.current_line,'abcde3') + + @contextmanager # from http://stackoverflow.com/a/17981937/398212 - thanks @rkennedy def captured_output(): new_out, new_err = StringIO(), StringIO()