6363 cast ,
6464)
6565
66- import prompt_toolkit as pt
6766import rich .box
6867from rich .console import Group , RenderableType
6968from rich .highlighter import ReprHighlighter
144143from prompt_toolkit .completion import Completer , DummyCompleter
145144from prompt_toolkit .formatted_text import ANSI
146145from prompt_toolkit .history import InMemoryHistory
146+ from prompt_toolkit .input import DummyInput
147+ from prompt_toolkit .output import DummyOutput
147148from prompt_toolkit .patch_stdout import patch_stdout
148149from prompt_toolkit .shortcuts import PromptSession , set_title
149150
151+ try :
152+ if sys .platform == "win32" :
153+ from prompt_toolkit .output .win32 import NoConsoleScreenBufferError # type: ignore[attr-defined]
154+ else :
155+
156+ class NoConsoleScreenBufferError (Exception ): # type: ignore[no-redef]
157+ """Dummy exception to use when prompt_toolkit.output.win32.NoConsoleScreenBufferError is not available."""
158+
159+
160+ except ImportError :
161+
162+ class NoConsoleScreenBufferError (Exception ): # type: ignore[no-redef]
163+ """Dummy exception to use when prompt_toolkit.output.win32.NoConsoleScreenBufferError is not available."""
164+
165+
150166from .pt_utils import (
151167 Cmd2Completer ,
152168 Cmd2History ,
@@ -421,10 +437,22 @@ def __init__(
421437 # Initialize prompt-toolkit PromptSession
422438 self .history_adapter = Cmd2History (self )
423439 self .completer = Cmd2Completer (self )
424- self .session : PromptSession [str ] = PromptSession (
425- history = self .history_adapter ,
426- completer = self .completer ,
427- )
440+
441+ try :
442+ self .session : PromptSession [str ] = PromptSession (
443+ history = self .history_adapter ,
444+ completer = self .completer ,
445+ )
446+ except (NoConsoleScreenBufferError , AttributeError , ValueError ):
447+ # Fallback to dummy input/output if PromptSession initialization fails.
448+ # This can happen in some CI environments (like GitHub Actions on Windows)
449+ # where isatty() is True but there is no real console.
450+ self .session = PromptSession (
451+ history = self .history_adapter ,
452+ completer = self .completer ,
453+ input = DummyInput (),
454+ output = DummyOutput (),
455+ )
428456
429457 # Commands to exclude from the history command
430458 self .exclude_from_history = ['eof' , 'history' ]
@@ -3226,12 +3254,16 @@ def get_prompt() -> Any:
32263254 history_to_use = InMemoryHistory ()
32273255 for item in history :
32283256 history_to_use .append_string (item )
3229- from prompt_toolkit import prompt as pt_prompt
32303257
3231- return pt_prompt (
3258+ temp_session1 : PromptSession [str ] = PromptSession (
3259+ history = history_to_use ,
3260+ input = self .session .input ,
3261+ output = self .session .output ,
3262+ )
3263+
3264+ return temp_session1 .prompt (
32323265 prompt_to_use ,
32333266 completer = completer_to_use ,
3234- history = history_to_use ,
32353267 bottom_toolbar = self ._bottom_toolbar ,
32363268 )
32373269
@@ -3244,13 +3276,26 @@ def get_prompt() -> Any:
32443276 # Otherwise read from self.stdin
32453277 elif self .stdin .isatty ():
32463278 # on a tty, print the prompt first, then read the line
3247- line = pt .prompt (prompt , bottom_toolbar = self ._bottom_toolbar )
3279+ temp_session2 : PromptSession [str ] = PromptSession (
3280+ input = self .session .input ,
3281+ output = self .session .output ,
3282+ )
3283+ line = temp_session2 .prompt (
3284+ prompt ,
3285+ bottom_toolbar = self ._bottom_toolbar ,
3286+ )
32483287 if len (line ) == 0 :
32493288 raise EOFError
32503289 return line .rstrip ('\n ' )
32513290 else :
32523291 # not a tty, just read the line
3253- line = pt .prompt (bottom_toolbar = self ._bottom_toolbar )
3292+ temp_session3 : PromptSession [str ] = PromptSession (
3293+ input = self .session .input ,
3294+ output = self .session .output ,
3295+ )
3296+ line = temp_session3 .prompt (
3297+ bottom_toolbar = self ._bottom_toolbar ,
3298+ )
32543299 if len (line ) == 0 :
32553300 raise EOFError
32563301 line = line .rstrip ('\n ' )
0 commit comments