Skip to content

Commit

Permalink
Add improved feedback messages
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Feb 23, 2024
1 parent 24a4179 commit d0878a8
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions circfirm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import shutil
import sys
import time
from typing import Optional
from typing import Any, Callable, Dict, Iterable, Optional

import click

Expand All @@ -28,6 +28,22 @@ def cli() -> None:
circfirm.startup.ensure_app_setup()


def announce_and_await(
msg: str,
func: Callable,
args: Iterable = (),
kwargs: Optional[Dict[str, Any]] = None,
) -> Any:
"""Announce an action to be performed, do it, then announce its completion."""
if kwargs is None:
kwargs = {}
fmt_msg = f"{msg}..."
click.echo(fmt_msg, nl=False)
resp = func(*args, **kwargs)
click.echo(" done")
return resp


@cli.command()
@click.argument("version")
@click.option("-l", "--language", default="en_US", help="CircuitPython language/locale")
Expand Down Expand Up @@ -61,13 +77,20 @@ def install(version: str, language: str, board: Optional[str]) -> None:
sys.exit(2)

if not circfirm.backend.is_downloaded(board, version, language):
click.echo("Downloading UF2...")
circfirm.backend.download_uf2(board, version, language)
announce_and_await(
"Downloading UF2",
circfirm.backend.download_uf2,
args=(board, version, language),
)
else:
click.echo(f"Using cached firmware file")

uf2file = circfirm.backend.get_uf2_filepath(board, version, language)
uf2filename = os.path.basename(uf2file)
shutil.copyfile(uf2file, os.path.join(bootloader, uf2filename))
click.echo("UF2 file copied to device!")
uf2_path = os.path.join(bootloader, uf2filename)
announce_and_await(
f"Copying UF2 to {board}", shutil.copyfile, args=(uf2file, uf2_path)
)
click.echo("Device should reboot momentarily.")


Expand Down Expand Up @@ -138,6 +161,10 @@ def cache_list(board: Optional[str]) -> None:
def cache_save(board: str, version: str, language: str) -> None:
"""Install a version of CircuitPython to cache."""
try:
circfirm.backend.download_uf2(board, version, language)
announce_and_await(
f"Caching firmware version {version} for {board}",
circfirm.backend.download_uf2,
args=(board, version, language),
)
except ConnectionError as err:
raise click.exceptions.ClickException(err.args[0])

0 comments on commit d0878a8

Please sign in to comment.