From 35d510d0d81d2eaa31c42a5bb7b622788169c2cb Mon Sep 17 00:00:00 2001 From: Jonathan Slenders Date: Thu, 19 Dec 2019 22:18:28 +0100 Subject: [PATCH] Support dumb terminals in PromptSession. --- prompt_toolkit/shortcuts/prompt.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/prompt_toolkit/shortcuts/prompt.py b/prompt_toolkit/shortcuts/prompt.py index 4df5cb947..39b7b4c4d 100644 --- a/prompt_toolkit/shortcuts/prompt.py +++ b/prompt_toolkit/shortcuts/prompt.py @@ -24,6 +24,7 @@ s = PromptSession() result = s.prompt('Say something: ') """ +import os from asyncio import get_event_loop from enum import Enum from functools import partial @@ -983,6 +984,11 @@ class itself. For these, passing in ``None`` will keep the current ) self.app.refresh_interval = self.refresh_interval # This is not reactive. + # If we are using the default output, and we have a dumb terminal, use + # the normal input function, instead of a prompt_toolkit prompt. + if self._output is None and os.environ.get("TERM") in ["dumb", "unknown"]: + return input(fragment_list_to_text(to_formatted_text(self.message))) + return self.app.run() async def prompt_async( @@ -1110,6 +1116,13 @@ async def prompt_async( ) self.app.refresh_interval = self.refresh_interval # This is not reactive. + # If we are using the default output, and we have a dumb terminal, use + # the normal input function, instead of a prompt_toolkit prompt. + if self._output is None and os.environ.get("TERM") in ["dumb", "unknown"]: + return await get_event_loop().run_until_complete( + lambda: input(fragment_list_to_text(to_formatted_text(self.message))) + ) + return await self.app.run_async() def _add_pre_run_callables(