From 263f3dd45b7656e8ba0aa4614234e5350b869158 Mon Sep 17 00:00:00 2001 From: Randy Lai Date: Mon, 27 Nov 2023 22:56:59 -0800 Subject: [PATCH] Require prompt-toolkit 3.0.41 fix #446 --- radian/lineedit/prompt.py | 6 ++-- radian/prompt_session.py | 64 +++++++++++++++++++-------------------- setup.py | 2 +- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/radian/lineedit/prompt.py b/radian/lineedit/prompt.py index 6f40b78..ebaf878 100644 --- a/radian/lineedit/prompt.py +++ b/radian/lineedit/prompt.py @@ -49,6 +49,7 @@ class ModalPromptSession(PromptSession): _current_mode = None _default_settings = {} _specs = OrderedDict() + _inputhook = None # new settings add_history = True @@ -66,7 +67,8 @@ def _filter_args(self, kwargs): setattr(self, key, kwargs[key]) del kwargs[key] - def __init__(self, *args, **kwargs): + def __init__(self, inputhook=None, *args, **kwargs): + self._inputhook = inputhook self._check_args(kwargs) self._filter_args(kwargs) super().__init__(*args, **kwargs) @@ -223,7 +225,7 @@ def prompt(self, *args, **kwargs): orig_mode = self.current_mode try: - result = super().prompt(**kwargs) + result = super().prompt(inputhook=self._inputhook, **kwargs) except KeyboardInterrupt: self._default_settings = backup.copy() self.activate_mode(orig_mode, force=True) diff --git a/radian/prompt_session.py b/radian/prompt_session.py index 1b89232..ad2fa70 100644 --- a/radian/prompt_session.py +++ b/radian/prompt_session.py @@ -13,7 +13,6 @@ from prompt_toolkit.utils import is_windows, get_term_environment_variable from prompt_toolkit.validation import Validator from prompt_toolkit.auto_suggest import AutoSuggestFromHistory -from prompt_toolkit.eventloop.inputhook import set_eventloop_with_inputhook from pygments.styles import get_style_by_name @@ -150,6 +149,36 @@ def message(): else: editing_mode = EditingMode.EMACS + def get_inputhook(): + # make testing more robust + if "RADIAN_NO_INPUTHOOK" in os.environ: + return None + + terminal_width = [None] + + def _(context): + output_width = session.app.output.get_size().columns + if output_width and terminal_width[0] != output_width: + terminal_width[0] = output_width + setoption("width", max(terminal_width[0], 20)) + + while True: + if context.input_is_ready(): + break + try: + if peek_event(): + with session.app.input.detach(): + with session.app.input.rare_mode(): + process_events() + else: + polled_events() + + except Exception: + pass + time.sleep(1.0 / 30) + + return _ + session = RadianPromptSession( message=message, style=style_from_pygments_cls(get_style_by_name(settings.color_scheme)), @@ -161,7 +190,8 @@ def message(): enable_suspend=True, input=CustomInput(sys.stdin), output=output, - auto_suggest=AutoSuggestFromHistory() if settings.auto_suggest else None + auto_suggest=AutoSuggestFromHistory() if settings.auto_suggest else None, + inputhook=get_inputhook() ) input_processors = [] @@ -254,36 +284,6 @@ def shell_process_text(session): input_processors=[] ) - def get_inputhook(): - terminal_width = [None] - - def _(context): - output_width = session.app.output.get_size().columns - if output_width and terminal_width[0] != output_width: - terminal_width[0] = output_width - setoption("width", max(terminal_width[0], 20)) - - while True: - if context.input_is_ready(): - break - try: - if peek_event(): - with session.app.input.detach(): - with session.app.input.rare_mode(): - process_events() - else: - polled_events() - - except Exception: - pass - time.sleep(1.0 / 30) - - return _ - - # make testing more robust - if "RADIAN_NO_INPUTHOOK" not in os.environ: - set_eventloop_with_inputhook(get_inputhook()) - apply_settings(session, settings) return session diff --git a/setup.py b/setup.py index ccab43c..201db4b 100644 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ def get_version(package): install_requires=[ # 'rchitect@git+https://github.com/randy3k/rchitect', 'rchitect>=0.4.3,<0.5.0', - 'prompt_toolkit>=3.0.15,<3.1', + 'prompt_toolkit>=3.0.41,<3.1', 'pygments>=2.5.0' ], entry_points={