Skip to content

Commit

Permalink
Support dumb terminals in PromptSession.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanslenders committed Jan 21, 2020
1 parent 9a0d66d commit 35d510d
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions prompt_toolkit/shortcuts/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 35d510d

Please sign in to comment.