1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()
|