@@ -674,3 +674,79 @@ loop. Prompting the user for input is as simple as calling
674674 The ``patch_stdout=True `` parameter is optional, but it's recommended, because
675675other coroutines could print to stdout. This option ensures that other output
676676won't destroy the prompt.
677+
678+
679+ Debugging on Windows
680+ --------------------
681+
682+ Debugging applications on windows under a debugger/ide can be difficult
683+ because the IDE may use its own 'console' which Prompt Toolkit is unable
684+ to use productively. In these cases it is possible to set an environment
685+ variable which allows Prompt Toolkit to create a console window and
686+ attach the appropriate handles to allow the program to use the console
687+ productively.
688+
689+ Two conditions need to be present for this to work. First, the code needs
690+ to be imported before anything else that may use the std file handles, or
691+ the console, because they may cache these handles, and thus wouldn't work
692+ as hoped.
693+
694+ :module: `~prompt_toolkit.shortcuts ` does this import with the following:
695+
696+ .. code :: python
697+
698+ if is_windows():
699+ from .terminal import win32_console_debug
700+ from .terminal.win32_output import Win32Output
701+ from .terminal.conemu_output import ConEmuOutput
702+ else :
703+
704+ If not using :module: `~prompt_toolkit.shortcuts ` this can be done with:
705+
706+ .. code :: python
707+
708+ from prompt_toolkit.terminal import win32_console_debug
709+
710+ The second condition needed is to set an environment variable::
711+
712+ SET USE_WIN_CONSOLE=Window Identifier
713+
714+ or alternatively, if managing the import directly, right after the import,
715+ call:
716+
717+ .. code :: python
718+
719+ win32_console_debug.ensure_console(title = " Window Identifier" )
720+
721+ or, if it is preferred to only invoke when the application is being
722+ 'debugged', set an environment variable::
723+
724+ SET USE_WIN_CONSOLE_DEBUG=Window Identifier
725+
726+ or right after the import, call:
727+
728+ .. code :: python
729+
730+ win32_console_debug.ensure_console(
731+ debugger_only = True , title = " Window Identifier" )
732+
733+ The console will be reused (based on the "Window Identifier" passed in)
734+ when restarting the application.
735+
736+
737+ Other Console Support (or starting in pre-existing terminals)
738+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
739+
740+ As an additional operating mode, :module: `~prompt_toolkit.shortcuts ` may be
741+ directly executed in a terminal::
742+
743+ c:\> python win_console_debug.py Window Identifier
744+
745+ When run as a script it will prep the terminal to allow it to be connected to.
746+ This allows other terminals to be supported by debugging with an environment
747+ variable of::
748+
749+ SET USE_EXISTING_WIN_CONSOLE=Window Identifier
750+
751+ USE_EXISTING_WIN_CONSOLE will not create a console if a console to connect
752+ to does not already exist.
0 commit comments