Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Now click is my best friend
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyanetsky committed Aug 14, 2023
1 parent 2c26422 commit 1deaae8
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 334 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@

### Added

- Support for the new PyYAML version.
- CLI interface.
- Support for the new minor PyYAML version.

### Changed

- Menu has been removed.

## 1.2.3 - 2023-02-11

Expand Down
193 changes: 56 additions & 137 deletions fastimer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,168 +5,87 @@
"""

import datetime
import os
import sys
from sys import stdout

import click
from vkostyanetsky import cliutils # type: ignore

from fastimer import datafile, statistics, utils
from fastimer import constants, datafile, statistics, utils
from fastimer.browser import FastsBrowser
from fastimer.commands import command_show, command_start
from fastimer.menu import FastimerMenu


def main() -> None:
def __get_path(path: str | None) -> str:
"""
Main entry point of the application. Displays the main menu by default.
Determines the path to a working directory. It is supposed to be the "Fastimer" folder
in user's home directory in case it's not specified via app's options.
"""

main_menu()
if path is None:
path = os.path.expanduser("~")
path = os.path.join(path, "Fastimer")

return path

def main_menu() -> None:
"""
Draws the main menu of the application.
"""

fasts = datafile.read_fasts()
active_fast = utils.get_active_fast(fasts)

menu = FastimerMenu(active_fast)

if active_fast is None:
menu.add_item("Start New Fast", start_fast)
else:
menu.add_item("Stop Active Fast", stop_fast)

menu.add_item("Fasts Browser", show_fasts_browser)
menu.add_item("Statistics", show_statistics)
menu.add_item("Exit", sys.exit)

menu.choose()


def start_fast() -> None:
"""
Starts a new fast.
"""

fasts = datafile.read_fasts()
fast = utils.get_active_fast(fasts)

if fast is not None:
print("Fast is already on.")
print()
def __path_help() -> str:
return "Set path to working directory."

cliutils.ask_for_enter()

else:
length = None
def __path_type() -> click.Path:
return click.Path(exists=True)

while length is None:
user_input = input("Enter fast duration in hours: ")

if user_input.isdigit():
length = int(user_input)
fast = {
"length": length,
"started": datetime.datetime.now(),
}
@click.group(help="CLI tool that helps with fasting.")
def cli():
stdout.reconfigure(encoding=constants.ENCODING)

fasts.append(fast)

datafile.write_fasts(fasts)
@cli.command(help="Start a new fast.")
@click.option("-p", "--path", type=__path_type(), help=__path_help())
def start(path: str | None) -> None:
path = __get_path(path)
command_start.main(path)

else:
print("Please enter a valid number.")
print()

main_menu()


def stop_fast() -> None:
"""
Stops the active fast.
"""

fasts = datafile.read_fasts()
active_fast = utils.get_active_fast(fasts)

menu = FastimerMenu(active_fast)

menu.add_item("Finish Fast", finish_fast)
menu.add_item("Cancel Fast", cancel_fast)
menu.add_item("Back", main_menu)

menu.choose()


def cancel_fast() -> None:
"""
Cancels the active fast.
"""

fasts = datafile.read_fasts()
active_fast = utils.get_active_fast(fasts)

if active_fast is not None:
cliutils.clear_terminal()

prompt = "Do you want to CANCEL the active fast? It cannot be undone."

if cliutils.ask_for_yes_or_no(prompt):
fasts.remove(active_fast)
datafile.write_fasts(fasts)

main_menu()


def finish_fast() -> None:
"""
Finishes the active fast.
"""

fasts = datafile.read_fasts()

cliutils.clear_terminal()

if cliutils.ask_for_yes_or_no("Do you want to end your ongoing fast?"):
fasts[-1]["stopped"] = datetime.datetime.now()
datafile.write_fasts(fasts)

main_menu()


def show_fasts_browser() -> None:
"""
Runs the fast browser to help a user to be proud of their efforts.
"""

fasts = datafile.read_fasts()
FastsBrowser(fasts).open()

main_menu()


def show_statistics() -> None:
"""
Draws fasting statistics accumulated so far.
"""
@cli.command(help="Show fasts by date.")
@click.option("-p", "--path", type=__path_type(), help=__path_help())
def show(path: str | None) -> None:
path = __get_path(path)
command_show.main(path)

fasts = datafile.read_fasts()

print("FASTING STATISTICS")
print()
# @cli.command(help="Check that data files have no mistakes.")
# @click.option("-p", "--path", type=__path_type(), help=__path_help())
# def test(path: str | None) -> None:
# path = __get_path(path)
# command_test.main(path)
#
#
# @cli.command(help="Set alarm according to notification settings.")
# @click.option("-p", "--path", type=__path_type(), help=__path_help())
# def beep(path: str | None):
# path = __get_path(path)
# command_beep.main(path)

statistics.print_completed_fasts(fasts)
statistics.print_total_fasting_time(fasts)
statistics.print_average_fast_length(fasts)
statistics.print_longest_fast_length(fasts)
statistics.print_longest_fasting_streak(fasts)
statistics.print_current_fasting_streak(fasts)
print()

statistics.print_achievements(fasts)
print()
# @cli.command(help="Display tasks for a given day (or days).")
# @click.argument(
# "period", default="today", type=click.Choice(["today", "last", "next", "date"])
# )
# @click.argument("value", default="")
# @click.option("-p", "--path", type=__path_type(), help=__path_help())
# @click.option(
# "-t", "--timesheet", is_flag=True, help="Show only tasks with time logged."
# )
# @click.option("-l", "--logs", is_flag=True, help="Show time logged for each task.")
# def show(path: str | None, timesheet: bool, logs: bool, period: str, value: str):
# path = __get_path(path)
# command_show.main(period, value, path, timesheet, logs)

cliutils.ask_for_enter()

main_menu()
if __name__ == "__main__":
cli()
22 changes: 22 additions & 0 deletions fastimer/commands/command_cancel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def cancel_fast() -> None:
"""
Cancels the active fast.
"""

pass


#
# fasts = datafile.read_fasts()
# active_fast = utils.get_active_fast(fasts)
#
# if active_fast is not None:
# cliutils.clear_terminal()
#
# prompt = "Do you want to CANCEL the active fast? It cannot be undone."
#
# if cliutils.ask_for_yes_or_no(prompt):
# fasts.remove(active_fast)
# datafile.write_fasts(fasts)
#
# main_menu()
Loading

0 comments on commit 1deaae8

Please sign in to comment.