From 3c7f07170ef6068402f5a14116f5fe2751eb76d7 Mon Sep 17 00:00:00 2001 From: PythonPan <2292551082@qq.com> Date: Wed, 11 Jun 2025 23:50:20 +0800 Subject: [PATCH 1/2] use correct width of non-english character in `Button` --- src/prompt_toolkit/widgets/base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/prompt_toolkit/widgets/base.py b/src/prompt_toolkit/widgets/base.py index e2330e9e1..cd5365278 100644 --- a/src/prompt_toolkit/widgets/base.py +++ b/src/prompt_toolkit/widgets/base.py @@ -438,6 +438,8 @@ def get_style() -> str: def _get_text_fragments(self) -> StyleAndTextTuples: width = self.width - ( get_cwidth(self.left_symbol) + get_cwidth(self.right_symbol) + ) + ( + len(self.text) - get_cwidth(self.text) ) text = (f"{{:^{width}}}").format(self.text) From 5db92459eeab0639e1ec2cafea542170fdb6b9c6 Mon Sep 17 00:00:00 2001 From: PythonPan <2292551082@qq.com> Date: Thu, 12 Jun 2025 02:01:16 +0800 Subject: [PATCH 2/2] Avoid the width is negative --- src/prompt_toolkit/widgets/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prompt_toolkit/widgets/base.py b/src/prompt_toolkit/widgets/base.py index cd5365278..51cfaa27f 100644 --- a/src/prompt_toolkit/widgets/base.py +++ b/src/prompt_toolkit/widgets/base.py @@ -441,7 +441,7 @@ def _get_text_fragments(self) -> StyleAndTextTuples: ) + ( len(self.text) - get_cwidth(self.text) ) - text = (f"{{:^{width}}}").format(self.text) + text = (f"{{:^{max(0,width)}}}").format(self.text) def handler(mouse_event: MouseEvent) -> None: if (