@@ -230,75 +230,77 @@ def is_pybricks_usb(dev):
230230 await hub .run (script_path , args .wait or args .stay_connected )
231231 else :
232232 if args .stay_connected :
233+ # if the user later starts the program by pressing the button on the hub,
234+ # we still want the hub stdout to print to Python's stdout
233235 hub .print_output = True
234236 hub ._enable_line_handler = True
235237 await hub .download (script_path )
236238
237- if args .stay_connected :
238-
239- async def reconnect_hub ():
240- if not await questionary .confirm (
241- "\n The hub has been disconnected. Would you like to re-connect?"
242- ).ask_async ():
243- exit ()
244-
245- if args .conntype == "ble" :
246- print (
247- f"Searching for { args .name or 'any hub with Pybricks service' } ..."
248- )
249- device_or_address = await find_ble (args .name )
250- hub = PybricksHubBLE (device_or_address )
251- elif args .conntype == "usb" :
252- device_or_address = find_usb (custom_match = is_pybricks_usb )
253- hub = PybricksHubUSB (device_or_address )
254-
255- await hub .connect ()
256- # re-enable echoing of the hub's stdout
257- hub .log_file = None
258- hub .output = []
259- hub ._stdout_buf .clear ()
260- hub ._stdout_line_queue = asyncio .Queue ()
261- hub .print_output = True
262- hub ._enable_line_handler = True
263- return hub
264-
265- response_options = [
266- "Recompile and Run" ,
267- "Recompile and Download" ,
268- "Exit" ,
269- ]
270- while True :
271- try :
272- response = await hub .race_power_button_press (
273- questionary .select (
274- "Would you like to re-compile your code?" ,
275- response_options ,
276- default = (
277- response_options [0 ]
278- if args .start
279- else response_options [1 ]
280- ),
281- ).ask_async ()
282- )
283- with _get_script_path (args .file ) as script_path :
284- if response == response_options [0 ]:
285- await hub .run (script_path , True )
286- elif response == response_options [1 ]:
287- await hub .download (script_path )
288- else :
289- exit ()
290-
291- except HubPowerButtonPressedError :
292- try :
293- await hub ._wait_for_user_program_stop (5 )
294- except HubDisconnectError :
295- hub = await reconnect_hub ()
239+ if not args .stay_connected :
240+ exit ()
241+
242+ async def reconnect_hub ():
243+ if not await questionary .confirm (
244+ "\n The hub has been disconnected. Would you like to re-connect?"
245+ ).ask_async ():
246+ exit ()
296247
248+ if args .conntype == "ble" :
249+ print (
250+ f"Searching for { args .name or 'any hub with Pybricks service' } ..."
251+ )
252+ device_or_address = await find_ble (args .name )
253+ hub = PybricksHubBLE (device_or_address )
254+ elif args .conntype == "usb" :
255+ device_or_address = find_usb (custom_match = is_pybricks_usb )
256+ hub = PybricksHubUSB (device_or_address )
257+
258+ await hub .connect ()
259+ # re-enable echoing of the hub's stdout
260+ hub ._enable_line_handler = True
261+ hub .print_output = True
262+ return hub
263+
264+ response_options = [
265+ "Recompile and Run" ,
266+ "Recompile and Download" ,
267+ "Exit" ,
268+ ]
269+ while True :
270+ try :
271+ response = await hub .race_power_button_press (
272+ questionary .select (
273+ "Would you like to re-compile your code?" ,
274+ response_options ,
275+ default = (
276+ response_options [0 ]
277+ if args .start
278+ else response_options [1 ]
279+ ),
280+ ).ask_async ()
281+ )
282+ with _get_script_path (args .file ) as script_path :
283+ if response == response_options [0 ]:
284+ await hub .run (script_path , wait = True )
285+ elif response == response_options [1 ]:
286+ await hub .download (script_path )
287+ else :
288+ exit ()
289+
290+ except HubPowerButtonPressedError :
291+ # This means the user pressed the button on the hub to re-start the
292+ # current program, so the menu was canceled and we are now printing
293+ # the hub stdout until the user program ends on the hub.
294+ try :
295+ await hub ._wait_for_user_program_stop (5 )
297296 except HubDisconnectError :
298- # let terminal cool off before making a new prompt
299- await asyncio .sleep (0.3 )
300297 hub = await reconnect_hub ()
301298
299+ except HubDisconnectError :
300+ # let terminal cool off before making a new prompt
301+ await asyncio .sleep (0.3 )
302+ hub = await reconnect_hub ()
303+
302304 finally :
303305 await hub .disconnect ()
304306
0 commit comments