diff --git a/CHANGELOG.md b/CHANGELOG.md index 20886d0..df04f60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Added the `Change Target File` option to the `--stay-connected` menu. + ([pybricksdev#123]) + +[pybricksdev#123]: https://github.com/pybricks/pybricksdev/pull/123 + ## [2.1.1] - 2025-09-13 ### Fixed diff --git a/pybricksdev/cli/__init__.py b/pybricksdev/cli/__init__.py index a499bd3..0abe13b 100644 --- a/pybricksdev/cli/__init__.py +++ b/pybricksdev/cli/__init__.py @@ -10,6 +10,7 @@ import os import sys from abc import ABC, abstractmethod +from enum import IntEnum from os import PathLike, path from tempfile import NamedTemporaryFile from typing import ContextManager, TextIO @@ -239,6 +240,12 @@ def is_pybricks_usb(dev): if not args.stay_connected: return + class ResponseOptions(IntEnum): + RECOMPILE_RUN = 0 + RECOMPILE_DOWNLOAD = 1 + CHANGE_TARGET_FILE = 2 + EXIT = 3 + async def reconnect_hub(): if not await questionary.confirm( "\nThe hub has been disconnected. Would you like to re-connect?" @@ -264,6 +271,7 @@ async def reconnect_hub(): response_options = [ "Recompile and Run", "Recompile and Download", + "Change Target File", "Exit", ] while True: @@ -280,22 +288,50 @@ async def reconnect_hub(): response = await hub.race_disconnect( hub.race_power_button_press( questionary.select( - "Would you like to re-compile your code?", + f"Would you like to re-compile {os.path.basename(args.file.name)}?", response_options, default=( - response_options[0] + response_options[ResponseOptions.RECOMPILE_RUN] if args.start - else response_options[1] + else response_options[ + ResponseOptions.RECOMPILE_DOWNLOAD + ] ), ).ask_async() ) ) - with _get_script_path(args.file) as script_path: - if response == response_options[0]: - await hub.run(script_path, wait=True) - elif response == response_options[1]: - await hub.download(script_path) - else: + + match response_options.index(response): + + case ResponseOptions.RECOMPILE_RUN: + with _get_script_path(args.file) as script_path: + await hub.run(script_path, wait=True) + + case ResponseOptions.RECOMPILE_DOWNLOAD: + with _get_script_path(args.file) as script_path: + await hub.download(script_path) + + case ResponseOptions.CHANGE_TARGET_FILE: + args.file.close() + while True: + try: + args.file = open( + await hub.race_disconnect( + hub.race_power_button_press( + questionary.path( + "What file would you like to use?" + ).ask_async() + ) + ) + ) + break + except FileNotFoundError: + print("The file was not found. Please try again.") + # send the new target file to the hub + with _get_script_path(args.file) as script_path: + await hub.download(script_path) + + case _: return except HubPowerButtonPressedError: