Skip to content

Commit e3781f8

Browse files
committed
Fix behavior when closing with unsaved changes
"n" and "esc" were previously handled the same as "go back to editing", instead of "n" meaning exit editor but don't save changes. Signed-off-by: Martin Prpič <martin.prpic@gmail.com>
1 parent ed31a76 commit e3781f8

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/commit_editor/app.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,21 @@ def check_action(self, action: str, parameters: tuple) -> bool | None:
304304
"""Disable editor actions when in prompt mode."""
305305
if self._prompt_mode is not None:
306306
# Allow only prompt-related actions
307-
if action in ("confirm_quit", "cancel_quit"):
307+
if action in ("confirm_quit", "discard_quit", "cancel_quit"):
308308
return True
309309
return False
310310
return True
311311

312312
def action_confirm_quit(self) -> None:
313-
"""Confirm quit when prompted."""
313+
"""Save and quit when prompted."""
314+
if self._prompt_mode == "quit_confirm":
315+
self._prompt_mode = None
316+
self.query_one("#message", MessageBar).clear()
317+
self.action_save()
318+
self.exit()
319+
320+
def action_discard_quit(self) -> None:
321+
"""Quit without saving when prompted."""
314322
if self._prompt_mode == "quit_confirm":
315323
self._prompt_mode = None
316324
self.query_one("#message", MessageBar).clear()
@@ -332,7 +340,11 @@ def on_key(self, event) -> None:
332340
event.prevent_default()
333341
event.stop()
334342
self.action_confirm_quit()
335-
elif event.key in ("n", "escape"):
343+
elif event.key == "n":
344+
event.prevent_default()
345+
event.stop()
346+
self.action_discard_quit()
347+
elif event.key == "escape":
336348
event.prevent_default()
337349
event.stop()
338350
self.action_cancel_quit()
@@ -398,7 +410,7 @@ def action_quit_app(self) -> None:
398410
editor = self.query_one("#editor", CommitTextArea)
399411
editor.read_only = True
400412
message_bar = self.query_one("#message", MessageBar)
401-
message_bar.show_prompt("Unsaved changes. Quit anyway? (y,n,esc)")
413+
message_bar.show_prompt("Save changes? (y/n/esc)")
402414
else:
403415
self.exit()
404416

tests/test_app.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ async def test_ctrl_q_dirty_shows_prompt(self, temp_file):
8888
assert app._prompt_mode == "quit_confirm"
8989
message_bar = app.query_one("#message", MessageBar)
9090
message_content = message_bar.message
91-
assert "Quit anyway?" in message_content
91+
assert "Save changes?" in message_content
9292

93-
async def test_confirm_quit_with_y(self, temp_file):
94-
"""Pressing 'y' in confirmation prompt should quit."""
93+
async def test_confirm_quit_with_y_saves_and_exits(self, temp_file):
94+
"""Pressing 'y' in confirmation prompt should save and quit."""
9595
temp_file.write_text("Original")
9696
app = CommitEditorApp(temp_file)
9797

@@ -104,9 +104,11 @@ async def test_confirm_quit_with_y(self, temp_file):
104104
await pilot.press("y")
105105

106106
assert app._exit is True
107+
# File should have been saved with the modified content
108+
assert temp_file.read_text() == "Modified\n"
107109

108-
async def test_cancel_quit_with_n(self, temp_file):
109-
"""Pressing 'n' in confirmation prompt should cancel quit."""
110+
async def test_discard_quit_with_n(self, temp_file):
111+
"""Pressing 'n' in confirmation prompt should quit without saving."""
110112
temp_file.write_text("Original")
111113
app = CommitEditorApp(temp_file)
112114

@@ -118,13 +120,9 @@ async def test_cancel_quit_with_n(self, temp_file):
118120
await pilot.press("ctrl+q")
119121
await pilot.press("n")
120122

121-
# Should not have exited, prompt mode should be cleared
122-
assert app._exit is False
123-
assert app._prompt_mode is None
124-
message_bar = app.query_one("#message", MessageBar)
125-
# Message bar should be cleared (empty)
126-
message_content = message_bar.message
127-
assert message_content == ""
123+
# Should have exited without saving
124+
assert app._exit is True
125+
assert temp_file.read_text() == "Original"
128126

129127
async def test_cancel_quit_with_escape(self, temp_file):
130128
"""Pressing 'escape' in confirmation prompt should cancel quit."""

0 commit comments

Comments
 (0)