-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (55 loc) · 2.1 KB
/
Copy pathutils.py
File metadata and controls
65 lines (55 loc) · 2.1 KB
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
import csv
import sys
import termios
import tty
def load_tasks(file_path="tasks.csv"):
"""Load tasks from a CSV file and return a list of dictionaries."""
tasks = []
try:
with open(file_path, "r", newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
# Convert string "True"/"False" to boolean
row["is_complete"] = row["is_complete"] == "True"
tasks.append(row)
except FileNotFoundError:
# File doesn't exist yet - that's okay! Return empty list
print(f"Task file not found. Starting with empty task list.")
return []
return tasks
def save_tasks(tasks, file_path="tasks.csv"):
"""Save tasks to a CSV file."""
with open(file_path, "w", newline="", encoding="utf-8") as file:
fieldnames = ["label", "is_complete", "priority", "location"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for task in tasks:
# Convert boolean back to string for CSV
task_copy = task.copy()
task_copy["is_complete"] = str(task["is_complete"])
writer.writerow(task_copy)
print(f"Saved {len(tasks)} tasks to {file_path}.")
def get_key():
"""Capture a single keypress, including arrow keys."""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
# Arrow keys send escape sequences: \x1b[A (up), \x1b[B (down), etc.
if ch == '\x1b' and sys.stdin.read(1) == '[':
arrow_keys = {'A': 'up', 'B': 'down', 'C': 'right', 'D': 'left'}
return arrow_keys.get(sys.stdin.read(1), ch)
# Special keys
if ch in ('\r', '\n'):
return 'enter'
if ch in ('\x7f', '\x08'):
return 'delete'
if ch == '\x03':
return 'exit'
return ch
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def clear_screen():
"""Clear the terminal screen."""
print("\033[2J\033[H", end="")