From 687a4d03a04de2e4fe61de5fba6ccd034827aea9 Mon Sep 17 00:00:00 2001 From: mecaneer23 Date: Thu, 2 May 2024 12:52:23 -0500 Subject: [PATCH] refactor: use created_new_file bool rather than keeping track of edits --- todo.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/todo.py b/todo.py index 6cb8e42..5974575 100755 --- a/todo.py +++ b/todo.py @@ -245,10 +245,10 @@ def remove_file(filename: Path) -> int: return 0 -def quit_program(todos: Todos, edits: int, prev_time: float) -> int: +def quit_program(todos: Todos, created_new_file: bool, prev_time: float) -> int: """Exit the program, writing to disk first""" todos, _ = update_modified_time(prev_time, todos) - if edits < 1: + if created_new_file and len(todos) == 0: return remove_file(FILENAME) return update_file(FILENAME, todos) @@ -490,8 +490,8 @@ def main(stdscr: curses.window) -> int: color and indentation level. Used for copy/paste operations. - edits: - int, used to indicate whether the file was newly + created_new_file: + bool, used to indicate whether the file was newly created and modified or not, determines whether to delete file in quit_program() @@ -506,7 +506,7 @@ def main(stdscr: curses.window) -> int: history = UndoRedo() single_line_state = SingleLineModeImpl(SingleLineMode.ON) copied_todo = Todo() - edits = len(todos) + created_new_file = len(todos) == 0 file_modified_time = get_file_modified_time(FILENAME) # if adding a new feature that updates `todos`, # make sure it also calls update_file() @@ -581,7 +581,6 @@ def main(stdscr: curses.window) -> int: _print_history(history) while True: - edits += 1 todos, file_modified_time = update_modified_time(file_modified_time, todos) set_header(stdscr, f"{HEADER}:") sublist_top = print_todos(stdscr, todos, selected, sublist_top) @@ -618,14 +617,13 @@ def main(stdscr: curses.window) -> int: }, ) if isinstance(next_step, Todos): - return quit_program(next_step, edits, file_modified_time) + return quit_program(next_step, created_new_file, file_modified_time) if next_step not in ( Key.ctrl_r, Key.u, ): # redo/undo history.add(todos, selected) _print_history(history) - edits -= 1 if __name__ == "__main__":