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

Commit

Permalink
Polishing + cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
vkostyanetsky committed Aug 14, 2023
1 parent 3bdead2 commit d08ba18
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 268 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* Added an option to cancel an active fast.
* Information about an active fast now displays in the main menu.
* Implemented an interface allows a user to have a look at previous fasts.
* Added more precise description for each achievement & a page with [the full list](Achievements.md) of possible awards.
* Added more precise description for each achievement & a page with [the full list](ACHIEVEMENTS.md) of possible awards.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Press [Esc] to return to the main menu.

### Statistics

The `Statistics` command shows you statistical data and earned [achievements](Achievements.md).
The `Statistics` command shows you statistical data and earned [achievements](ACHIEVEMENTS.md).

For instance:

Expand Down
29 changes: 26 additions & 3 deletions fastimer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import click

from fastimer import constants
from fastimer.commands import command_show, command_start
from fastimer.commands import command_cancel, command_show, command_start, command_stop


def __get_path(path: str | None) -> str:
Expand Down Expand Up @@ -44,14 +44,15 @@ def cli():


@cli.command(help="Start a new fast.")
@click.argument("length", type=click.INT, default=16)
@click.option("-p", "--path", type=__path_type(), help=__path_help())
def start(path: str | None) -> None:
def start(path: str | None, length: int) -> None:
"""
Creates a new fast entry in the data file.
"""

path = __get_path(path)
command_start.main(path)
command_start.main(path, length)


@cli.command(help="Show fasts by date.")
Expand All @@ -65,5 +66,27 @@ def show(path: str | None) -> None:
command_show.main(path)


@cli.command(help="Stop ongoing fast.")
@click.option("-p", "--path", type=__path_type(), help=__path_help())
def stop(path: str | None) -> None:
"""
Stops ongoing fast.
"""

path = __get_path(path)
command_stop.main(path)


@cli.command(help="Cancel ongoing fast.")
@click.option("-p", "--path", type=__path_type(), help=__path_help())
def cancel(path: str | None) -> None:
"""
Cancels ongoing fast.
"""

path = __get_path(path)
command_cancel.main(path)


if __name__ == "__main__":
cli()
37 changes: 16 additions & 21 deletions fastimer/commands/command_cancel.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
#!/usr/bin/env python3

"""
Implementation of a command to cancel an ongoing fast.
"""
"""Implementation of a command to cancel an ongoing fast."""

def cancel_fast() -> None:
import click

from fastimer import datafile, utils


def main(path: str) -> 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()
fasts = datafile.read_fasts(path)
fast = utils.get_active_fast(fasts)

if fast is not None:
fasts.remove(fast)
datafile.write_fasts(path, fasts)

else:
click.echo("No active fast to cancel!")
184 changes: 4 additions & 180 deletions fastimer/commands/command_show.py
Original file line number Diff line number Diff line change
@@ -1,192 +1,16 @@
#!/usr/bin/env python3

"""
Implementation of a command to output a fast.
"""

import datetime
import typing

import click
"""Implementation of a command to output a fast."""

from fastimer import datafile, utils


def main(path: str) -> None:
"""
Generates a detailed view of a fast.
Outputs a detailed view of a fast.
"""

fasts = datafile.read_fasts(path)
index = len(fasts) - 1
fast = fasts[index]

time = fast["stopped"] if utils.is_fast_stopped(fast) else datetime.datetime.now()

goal = fast["started"] + datetime.timedelta(hours=fast["length"])

__echo_fast_title(fast)
click.echo()

__echo_fast_from(fast)
__echo_fast_goal(fast, goal)
click.echo()

__echo_fasting_zones(fast, time)
click.echo()

__echo_fast_progress_bar(fast, goal, time)
click.echo()

__echo_fast_elapsed_time(fast, time)

if time <= goal:
__echo_fast_remaining_time(time, goal)
else:
__echo_fast_extra_time(time, goal)


def __echo_fast_title(fast: dict[str, typing.Any]) -> None:
if utils.is_fast_stopped(fast):
if utils.is_fast_completed(fast):
click.echo(click.style(text="COMPLETED FAST", fg="green"))
else:
click.echo(click.style(text="FAILED FAST", fg="red"))
else:
click.echo(click.style(text="ACTIVE FAST", fg="yellow"))


def __echo_fast_from(fast: dict[str, typing.Any]) -> None:
value = __get_day(fast["started"])
value = utils.title_and_value("From", value, 5)

click.echo(value)


def __echo_fast_goal(fast: dict[str, typing.Any], goal: datetime.datetime) -> None:
length = fast["length"]
goal_string = __get_day(goal)
goal_string = f"{goal_string} ({length} hours)"
goal_string = utils.title_and_value("Goal", goal_string, 5)

click.echo(goal_string)


def __echo_fast_elapsed_time(
fast: dict[str, typing.Any], time: datetime.datetime
) -> None:
date1 = fast["started"]
date2 = time if fast.get("stopped") is None else fast["stopped"]

value = __get_time_difference(date1, date2)
value = utils.title_and_value("Elapsed time", value, 15)

click.echo(str(value))


def __echo_fast_extra_time(time: datetime.datetime, goal: datetime.datetime) -> None:
value = __get_time_difference(goal, time) if time >= goal else None
value = utils.title_and_value("Extra time", value, 15)

click.echo(str(value))


def __echo_fast_remaining_time(
time: datetime.datetime, goal: datetime.datetime
) -> None:
value = (
__get_time_difference(time - datetime.timedelta(minutes=1), goal)
if time < goal
else None
)

value = utils.title_and_value("Remaining", value, 15)

click.echo(str(value))


def __line_for_zone(
note: str,
time: datetime.datetime,
title: str,
start_time: datetime.datetime,
end_time: datetime.datetime | None,
) -> str:
zone_from = f"from {__get_day(start_time)}"

if end_time is None:
zone_note = note if start_time <= time else ""
else:
zone_note = note if start_time <= time < end_time else ""

zone_note = utils.title_and_value(f"- {title}", f"{zone_from}{zone_note}")

return str(zone_note)


def __echo_fasting_zones(fast: dict[str, typing.Any], time: datetime.datetime) -> None:
note = " <-- you were here" if utils.is_fast_stopped(fast) else " <-- you are here"

anabolic_zone = fast["started"]
catabolic_zone = anabolic_zone + datetime.timedelta(hours=4)
fat_burning_zone = catabolic_zone + datetime.timedelta(hours=12)
ketosis_zone = fat_burning_zone + datetime.timedelta(hours=8)
deep_ketosis_zone = ketosis_zone + datetime.timedelta(hours=48)

click.echo("Fasting zones:")
click.echo("")

line = __line_for_zone(note, time, "Anabolic", anabolic_zone, catabolic_zone)
click.echo(line)

line = __line_for_zone(note, time, "Catabolic", catabolic_zone, fat_burning_zone)
click.echo(line)

line = __line_for_zone(note, time, "Fat burning", fat_burning_zone, ketosis_zone)
click.echo(line)

line = __line_for_zone(note, time, "Ketosis", ketosis_zone, deep_ketosis_zone)
click.echo(line)

line = __line_for_zone(note, time, "Anabolic", deep_ketosis_zone, None)
click.echo(line)


def __echo_fast_progress_bar(
fast: dict[str, typing.Any], goal: datetime.datetime, time: datetime.datetime
) -> None:
seconds_now = (time - fast["started"]).total_seconds()
seconds_all = (goal - fast["started"]).total_seconds()

percent = round(seconds_now / seconds_all * 100, 1)

done_len = int(percent // 2.5)
done_len = min(done_len, 40)

left_len = int(40 - done_len)

left = "-" * left_len
done = "#" * done_len
tail = str(percent)

click.echo(f"{done}{left} {tail}%")


def __get_time_difference(
start_date: datetime.datetime, end_date: datetime.datetime
) -> str:
hours, minutes = utils.get_time_difference(start_date, end_date)

hours_string = str(hours).zfill(2)
minutes_string = str(minutes).zfill(2)

return f"{hours_string}h {minutes_string}m"


def __get_day(date: datetime.datetime) -> str:
if (datetime.datetime.now() - date).days > 7:
fmt = "%Y-%m-%d %H:%M"
else:
fmt = "%a, %H:%M"
fast = fasts[len(fasts) - 1]

return date.strftime(fmt)
utils.echo(fast)
64 changes: 27 additions & 37 deletions fastimer/commands/command_start.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
#!/usr/bin/env python3

"""
Implementation of a command to start a new fast.
"""
"""Implementation of a command to start a new fast."""

import datetime

def main(path: str) -> None:
import click

from fastimer import datafile, utils


def main(path: str, length: int) -> None:
"""
Starts a new fast.
"""

pass


#
# fasts = datafile.read_fasts()
# fast = utils.get_active_fast(fasts)
#
# if fast is not None:
# print("Fast is already on.")
# print()
#
# cliutils.ask_for_enter()
#
# else:
# length = None
#
# 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(),
# }
#
# fasts.append(fast)
#
# datafile.write_fasts(fasts)
#
# else:
# print("Please enter a valid number.")
# print()
fasts = datafile.read_fasts(path)
fast = utils.get_active_fast(fasts)

if fast is not None:
click.echo("Fast is already on.")

else:
fast = {
"length": length,
"started": datetime.datetime.now(),
}

fasts.append(fast)

datafile.write_fasts(path, fasts)

fast = fasts[len(fasts) - 1]

utils.echo(fast)
4 changes: 1 addition & 3 deletions fastimer/commands/command_stat.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/usr/bin/env python3

"""
Implementation of a command to output fasting statistics.
"""
"""Implementation of a command to output fasting statistics."""


def main(path: str) -> None:
Expand Down
Loading

0 comments on commit d08ba18

Please sign in to comment.