diff --git a/babi.py b/babi.py index 7373118..830e994 100644 --- a/babi.py +++ b/babi.py @@ -174,6 +174,8 @@ class Status: elif isinstance(key.wch, str) and key.wch.isprintable(): buf = buf[:pos] + key.wch + buf[pos:] pos += 1 + elif key.keyname == b'^C': + return '' elif key.key == ord('\r'): return buf @@ -610,11 +612,11 @@ def _edit(screen: Screen) -> EditResult: return EditResult.EXIT elif response == ':w': screen.file.save(screen.status, screen.margin) + elif response == '': # noop / cancel + screen.status.update('', screen.margin) else: - screen.status.update( - f'{response} is not a valid command.', - screen.margin, - ) + msg = f'invalid command: {response}' + screen.status.update(msg, screen.margin) elif key.keyname == b'^S': screen.file.save(screen.status, screen.margin) elif key.keyname == b'^X': diff --git a/tests/babi_test.py b/tests/babi_test.py index b69f836..1fa0884 100644 --- a/tests/babi_test.py +++ b/tests/babi_test.py @@ -949,3 +949,31 @@ def test_resizing_and_scrolling_in_command_mode(): h.await_text(f'\n{"b" * 15}\n') h.press('Enter') + + +def test_invalid_command(): + with run() as h, and_exit(h): + trigger_command_mode(h) + h.press_and_enter(':fake') + h.await_text('invalid command: :fake') + + +def test_empty_command_is_noop(): + with run() as h, and_exit(h): + h.press('hello ') + trigger_command_mode(h) + h.press('Enter') + h.press('world') + h.await_text('hello world') + h.await_text_missing('invalid command') + + +def test_cancel_command_mode(): + with run() as h, and_exit(h): + h.press('hello ') + trigger_command_mode(h) + h.press(':q') + h.press('^C') + h.press('world') + h.await_text('hello world') + h.await_text_missing('invalid command')