Skip to content

Commit 550b0ab

Browse files
authored
cli: Add option to --stay connected menu to re-run stored program.
Add "Run Stored Program" menu item to run a program already on the hub without downloading it again.
1 parent 5e767e7 commit 550b0ab

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

99
### Added
10+
- Added the `Run Stored Program` option to the `--stay-connected` menu.
11+
([pybricksdev#125])
1012
- Added the `Change Target File` option to the `--stay-connected` menu.
1113
([pybricksdev#123])
1214

1315
[pybricksdev#123]: https://github.com/pybricks/pybricksdev/pull/123
1416

17+
### Changed
18+
- Changed the default option in the `--stay-connected` menu to be the last
19+
used option. ([pybricksdev#125])
20+
21+
[pybricksdev#125]: https://github.com/pybricks/pybricksdev/pull/125
22+
1523
## [2.1.1] - 2025-09-13
1624

1725
### Fixed

pybricksdev/cli/__init__.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import argcomplete
1919
import questionary
2020
from argcomplete.completers import FilesCompleter
21+
from packaging.version import Version
2122

2223
from pybricksdev import __name__ as MODULE_NAME
2324
from pybricksdev import __version__ as MODULE_VERSION
@@ -243,8 +244,9 @@ def is_pybricks_usb(dev):
243244
class ResponseOptions(IntEnum):
244245
RECOMPILE_RUN = 0
245246
RECOMPILE_DOWNLOAD = 1
246-
CHANGE_TARGET_FILE = 2
247-
EXIT = 3
247+
RUN_STORED = 2
248+
CHANGE_TARGET_FILE = 3
249+
EXIT = 4
248250

249251
async def reconnect_hub():
250252
if not await questionary.confirm(
@@ -271,9 +273,19 @@ async def reconnect_hub():
271273
response_options = [
272274
"Recompile and Run",
273275
"Recompile and Download",
276+
"Run Stored Program",
274277
"Change Target File",
275278
"Exit",
276279
]
280+
# the entry that is selected by default when the menu opens
281+
# this is overridden after the user picks an option
282+
# so that the default option is always the one that was last chosen
283+
default_response_option = (
284+
ResponseOptions.RECOMPILE_RUN
285+
if args.start
286+
else ResponseOptions.RECOMPILE_DOWNLOAD
287+
)
288+
277289
while True:
278290
try:
279291
if args.file is sys.stdin:
@@ -290,17 +302,13 @@ async def reconnect_hub():
290302
questionary.select(
291303
f"Would you like to re-compile {os.path.basename(args.file.name)}?",
292304
response_options,
293-
default=(
294-
response_options[ResponseOptions.RECOMPILE_RUN]
295-
if args.start
296-
else response_options[
297-
ResponseOptions.RECOMPILE_DOWNLOAD
298-
]
299-
),
305+
default=(response_options[default_response_option]),
300306
).ask_async()
301307
)
302308
)
303309

310+
default_response_option = response_options.index(response)
311+
304312
match response_options.index(response):
305313

306314
case ResponseOptions.RECOMPILE_RUN:
@@ -311,6 +319,15 @@ async def reconnect_hub():
311319
with _get_script_path(args.file) as script_path:
312320
await hub.download(script_path)
313321

322+
case ResponseOptions.RUN_STORED:
323+
if hub.fw_version < Version("3.2.0-beta.4"):
324+
print(
325+
"Running a stored program remotely is only supported in the hub firmware version >= v3.2.0."
326+
)
327+
else:
328+
await hub.start_user_program()
329+
await hub._wait_for_user_program_stop()
330+
314331
case ResponseOptions.CHANGE_TARGET_FILE:
315332
args.file.close()
316333
while True:

0 commit comments

Comments
 (0)