Skip to content

Commit

Permalink
refactor: use created_new_file bool rather than keeping track of edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed May 2, 2024
1 parent 1ab8929 commit 687a4d0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 687a4d0

Please sign in to comment.