6363 cast ,
6464)
6565
66+ import prompt_toolkit as pt
6667import rich .box
6768from rich .console import Group , RenderableType
6869from rich .highlighter import ReprHighlighter
143144from prompt_toolkit .completion import Completer , DummyCompleter
144145from prompt_toolkit .formatted_text import ANSI
145146from prompt_toolkit .history import InMemoryHistory
146- from prompt_toolkit .input import DummyInput
147- from prompt_toolkit .output import DummyOutput
148147from prompt_toolkit .patch_stdout import patch_stdout
149148from prompt_toolkit .shortcuts import PromptSession , set_title
150149
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-
166150from .pt_utils import (
167151 Cmd2Completer ,
168152 Cmd2History ,
@@ -437,22 +421,10 @@ def __init__(
437421 # Initialize prompt-toolkit PromptSession
438422 self .history_adapter = Cmd2History (self )
439423 self .completer = Cmd2Completer (self )
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- )
424+ self .session : PromptSession [str ] = PromptSession (
425+ history = self .history_adapter ,
426+ completer = self .completer ,
427+ )
456428
457429 # Commands to exclude from the history command
458430 self .exclude_from_history = ['eof' , 'history' ]
@@ -3254,16 +3226,12 @@ def get_prompt() -> Any:
32543226 history_to_use = InMemoryHistory ()
32553227 for item in history :
32563228 history_to_use .append_string (item )
3229+ from prompt_toolkit import prompt as pt_prompt
32573230
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 (
3231+ return pt_prompt (
32653232 prompt_to_use ,
32663233 completer = completer_to_use ,
3234+ history = history_to_use ,
32673235 bottom_toolbar = self ._bottom_toolbar ,
32683236 )
32693237
@@ -3276,26 +3244,13 @@ def get_prompt() -> Any:
32763244 # Otherwise read from self.stdin
32773245 elif self .stdin .isatty ():
32783246 # on a tty, print the prompt first, then read the line
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- )
3247+ line = pt .prompt (prompt , bottom_toolbar = self ._bottom_toolbar )
32873248 if len (line ) == 0 :
32883249 raise EOFError
32893250 return line .rstrip ('\n ' )
32903251 else :
32913252 # not a tty, just read the line
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- )
3253+ line = pt .prompt (bottom_toolbar = self ._bottom_toolbar )
32993254 if len (line ) == 0 :
33003255 raise EOFError
33013256 line = line .rstrip ('\n ' )
0 commit comments