Skip to content

Commit 5e767e7

Browse files
authored
cli: Allow changing the current file within the --stay-connected menu.
Add a "Change Target File" menu item that prompts the user to select a new file. This way the user can change projects without having to disconnect and reconnect. Once the file is selected, the program is compiled and downloaded, but it doesn't start.
1 parent 149b24a commit 5e767e7

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- Added the `Change Target File` option to the `--stay-connected` menu.
11+
([pybricksdev#123])
12+
13+
[pybricksdev#123]: https://github.com/pybricks/pybricksdev/pull/123
14+
915
## [2.1.1] - 2025-09-13
1016

1117
### Fixed

pybricksdev/cli/__init__.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import sys
1212
from abc import ABC, abstractmethod
13+
from enum import IntEnum
1314
from os import PathLike, path
1415
from tempfile import NamedTemporaryFile
1516
from typing import ContextManager, TextIO
@@ -239,6 +240,12 @@ def is_pybricks_usb(dev):
239240
if not args.stay_connected:
240241
return
241242

243+
class ResponseOptions(IntEnum):
244+
RECOMPILE_RUN = 0
245+
RECOMPILE_DOWNLOAD = 1
246+
CHANGE_TARGET_FILE = 2
247+
EXIT = 3
248+
242249
async def reconnect_hub():
243250
if not await questionary.confirm(
244251
"\nThe hub has been disconnected. Would you like to re-connect?"
@@ -264,6 +271,7 @@ async def reconnect_hub():
264271
response_options = [
265272
"Recompile and Run",
266273
"Recompile and Download",
274+
"Change Target File",
267275
"Exit",
268276
]
269277
while True:
@@ -280,22 +288,50 @@ async def reconnect_hub():
280288
response = await hub.race_disconnect(
281289
hub.race_power_button_press(
282290
questionary.select(
283-
"Would you like to re-compile your code?",
291+
f"Would you like to re-compile {os.path.basename(args.file.name)}?",
284292
response_options,
285293
default=(
286-
response_options[0]
294+
response_options[ResponseOptions.RECOMPILE_RUN]
287295
if args.start
288-
else response_options[1]
296+
else response_options[
297+
ResponseOptions.RECOMPILE_DOWNLOAD
298+
]
289299
),
290300
).ask_async()
291301
)
292302
)
293-
with _get_script_path(args.file) as script_path:
294-
if response == response_options[0]:
295-
await hub.run(script_path, wait=True)
296-
elif response == response_options[1]:
297-
await hub.download(script_path)
298-
else:
303+
304+
match response_options.index(response):
305+
306+
case ResponseOptions.RECOMPILE_RUN:
307+
with _get_script_path(args.file) as script_path:
308+
await hub.run(script_path, wait=True)
309+
310+
case ResponseOptions.RECOMPILE_DOWNLOAD:
311+
with _get_script_path(args.file) as script_path:
312+
await hub.download(script_path)
313+
314+
case ResponseOptions.CHANGE_TARGET_FILE:
315+
args.file.close()
316+
while True:
317+
try:
318+
args.file = open(
319+
await hub.race_disconnect(
320+
hub.race_power_button_press(
321+
questionary.path(
322+
"What file would you like to use?"
323+
).ask_async()
324+
)
325+
)
326+
)
327+
break
328+
except FileNotFoundError:
329+
print("The file was not found. Please try again.")
330+
# send the new target file to the hub
331+
with _get_script_path(args.file) as script_path:
332+
await hub.download(script_path)
333+
334+
case _:
299335
return
300336

301337
except HubPowerButtonPressedError:

0 commit comments

Comments
 (0)