diff options
Diffstat (limited to 'screens/weight.py')
| -rw-r--r-- | screens/weight.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/screens/weight.py b/screens/weight.py new file mode 100644 index 0000000..5669f07 --- /dev/null +++ b/screens/weight.py @@ -0,0 +1,72 @@ +import sqlite3 + +import models +import ui + + +def log_weight(conn: sqlite3.Connection) -> None: + ui.clear_screen() + ui.print_header("Log Weight") + date_time = ui.prompt_datetime("Date/Time") + weight = ui.prompt_float("Weight (kg): ", min_val=0.1) + assert weight is not None + note = ui.prompt_str("Note (optional): ", required=False) + models.add_weight_log(conn, date_time, weight, note) + print(f"Logged {weight:.1f}kg.") + ui.pause() + + +def _fmt_diff(current: float, previous: float) -> str: + diff = round(current - previous, 1) + if diff > 0: + return f"+{diff:.1f}" + elif diff < 0: + return f"{diff:.1f}" + return "0.0" + + +def view_weight_logs(conn: sqlite3.Connection) -> None: + while True: + ui.clear_screen() + logs = models.list_weight_logs(conn) + if not logs: + print("\nNo weight logs recorded yet.") + ui.pause() + return + ui.print_header("Weight Logs") + + # Logs are newest-first; diff compares to the next row (previous in time) + rows = [] + for i, log in enumerate(logs): + weight = log["weight"] + if i < len(logs) - 1: + diff = _fmt_diff(weight, logs[i + 1]["weight"]) + else: + diff = "" + rows.append( + [ + str(i + 1), + log["date_time"], + f"{weight:.1f}", + diff, + log["note"] or "", + ] + ) + + ui.print_table(["#", "Date", "Weight (kg)", "Diff", "Note"], rows) + + choice = input("\nSelect # to delete ('b' = back): ").strip() + if choice.lower() == "b": + break + try: + idx = int(choice) - 1 + if idx < 0 or idx >= len(logs): + print("Invalid selection.") + continue + except ValueError: + print("Invalid input.") + continue + if ui.confirm(f"Delete log from {logs[idx]['date_time']}?"): + models.delete_weight_log(conn, logs[idx]["id"]) + print("Log deleted.") + ui.pause() |
