-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
125 lines (108 loc) · 4.77 KB
/
main.py
File metadata and controls
125 lines (108 loc) · 4.77 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import click
from src.models.habit import Habit, Period
from src.storage.database import Database
# Initialize the database
db = Database()
@click.group()
def cli():
"""Main entry point for the CLI application."""
pass
@click.command()
@click.option('--name', prompt='Habit name', help='The name of the habit you want to complete.')
def complete_habit(name):
"""Complete a specific habit."""
# Retrieve all habits from the database
habits = db.get_all_habits()
# Search for the habit by name
habit = next((h for h in habits if h.name.lower() == name.lower()), None)
if habit:
# Mark the habit as completed
habit.complete_task()
db.save_habit(habit)
click.echo(f"Habit '{habit.name}' has been marked as completed.")
else:
click.echo(f"Habit '{name}' not found.")
@click.command()
@click.option('--name', prompt='Habit name', help='The name of the habit.')
@click.option('--description', prompt='Habit description', help='A brief description of the habit.')
@click.option('--period', prompt='Habit period (daily/weekly)', type=click.Choice(['daily', 'weekly'], case_sensitive=False), help='The period of the habit.')
def create_habit(name, description, period):
"""Create a new habit."""
# Convert the period string to the Period enum
period_enum = Period.DAILY if period == 'daily' else Period.WEEKLY
# Create a new habit instance
habit = Habit(name, description, period_enum)
# Save the new habit to the database
db.save_habit(habit)
click.echo(f"Habit '{habit.name}' created successfully.")
@click.command()
def list_habits():
"""List all habits."""
# Retrieve all habits from the database
habits = db.get_all_habits()
if habits:
# Print each habit
for habit in habits:
click.echo(f"{habit.name} ({habit.period.value}): {habit.description}")
else:
click.echo("No habits found.")
@click.command()
@click.option('--period', prompt='Period (daily/weekly)', type=click.Choice(['daily', 'weekly'], case_sensitive=False), help='The period of the habits to list.')
def list_habits_by_period(period):
"""List all habits of a specific period."""
habits = db.get_all_habits()
period_enum = Period.DAILY if period == 'daily' else Period.WEEKLY
filtered_habits = [habit for habit in habits if habit.period == period_enum]
if filtered_habits:
for habit in filtered_habits:
click.echo(f"{habit.name} ({habit.period.value}): {habit.description}")
else:
click.echo(f"No {period} habits found.")
@click.command()
@click.option('--habit_name', prompt='Habit name', help='The name of the habit.')
def longest_streak(habit_name):
"""Get the longest streak for a specific habit."""
habits = db.get_all_habits()
habit = next((h for h in habits if h.name.lower() == habit_name.lower()), None)
if habit:
streak = habit.get_longest_streak()
click.echo(f"The longest streak for habit '{habit.name}' is {streak} days.")
else:
click.echo(f"Habit '{habit_name}' not found.")
@click.command()
def longest_overall_streak():
"""Get the longest streak among all habits."""
habits = db.get_all_habits()
if habits:
streaks = [habit.get_longest_streak() for habit in habits]
overall_streak = max(streaks)
click.echo(f"The longest overall streak across all habits is {overall_streak} days.")
else:
click.echo("No habits found.")
@click.command()
@click.option('--name', prompt='Habit name', help='The name of the habit to delete.')
def delete_habit(name):
"""Delete a habit."""
db.delete_habit(name)
click.echo(f"Habit '{name}' has been deleted.")
@click.command()
@click.option('--old_name', prompt='Old habit name', help='The old name of the habit you want to edit.')
@click.option('--new_name', prompt='New habit name', help='The new name for the habit.')
@click.option('--new_description', prompt='New description', help='The new description of the habit.')
@click.option('--new_period', prompt='New period (daily/weekly)', type=click.Choice(['daily', 'weekly'], case_sensitive=False), help='The new period of the habit.')
def edit_habit(old_name, new_name, new_description, new_period):
"""Edit an existing habit."""
period_enum = Period.DAILY if new_period == 'daily' else Period.WEEKLY
db.edit_habit(old_name, new_name, new_description, period_enum)
click.echo(f"Habit '{old_name}' has been updated to '{new_name}'.")
# Register the commands
cli.add_command(complete_habit)
cli.add_command(create_habit)
cli.add_command(list_habits)
cli.add_command(list_habits_by_period)
cli.add_command(longest_streak)
cli.add_command(longest_overall_streak)
cli.add_command(delete_habit)
cli.add_command(edit_habit)
if __name__ == '__main__':
cli()