From be9ff957fd7ebaadabe9ab094067440e4ffc2937 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Fri, 27 Feb 2026 06:36:51 -0500 Subject: [PATCH] use transform_region() for prettify/unprettify This method is built in to prompt_toolkit, and simpler. The position of the cursor in the transformed text is still not precise, as expected. (It wasn't precise before.) --- changelog.md | 13 +++++++------ mycli/key_bindings.py | 20 ++++---------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/changelog.md b/changelog.md index 9708749d..25c1ac91 100644 --- a/changelog.md +++ b/changelog.md @@ -20,12 +20,6 @@ Bug Fixes * Ensure fullscreen in fuzzy history search. -Internal ---------- -* Better tests for `null_string` configuration option. -* Better cleanup of resources in the test suite. - - Documentation --------- * Add `help ` to TIPS. @@ -33,6 +27,13 @@ Documentation * Add `$VISUAL` environment variable hint to TIPS. +Internal +--------- +* Better tests for `null_string` configuration option. +* Better cleanup of resources in the test suite. +* Simplify prettify/unprettify handlers. + + 1.57.0 (2026/02/25) ============== diff --git a/mycli/key_bindings.py b/mycli/key_bindings.py index c7afaf45..9da02dac 100644 --- a/mycli/key_bindings.py +++ b/mycli/key_bindings.py @@ -166,14 +166,8 @@ def _(event: KeyPressEvent) -> None: _logger.debug("Detected /> key.") b = event.app.current_buffer - cursorpos_relative = b.cursor_position / max(1, len(b.text)) - pretty_text = mycli.handle_prettify_binding(b.text) - if len(pretty_text) > 0: - b.text = pretty_text - cursorpos_abs = int(round(cursorpos_relative * len(b.text))) - while 0 < cursorpos_abs < len(b.text) and b.text[cursorpos_abs] in (" ", "\n"): - cursorpos_abs -= 1 - b.cursor_position = min(cursorpos_abs, len(b.text)) + if b.text: + b.transform_region(0, len(b.text), mycli.handle_prettify_binding) @kb.add("c-x", "u", filter=emacs_mode) def _(event: KeyPressEvent) -> None: @@ -185,14 +179,8 @@ def _(event: KeyPressEvent) -> None: _logger.debug("Detected /< key.") b = event.app.current_buffer - cursorpos_relative = b.cursor_position / max(1, len(b.text)) - unpretty_text = mycli.handle_unprettify_binding(b.text) - if len(unpretty_text) > 0: - b.text = unpretty_text - cursorpos_abs = int(round(cursorpos_relative * len(b.text))) - while 0 < cursorpos_abs < len(b.text) and b.text[cursorpos_abs] in (" ", "\n"): - cursorpos_abs -= 1 - b.cursor_position = min(cursorpos_abs, len(b.text)) + if b.text: + b.transform_region(0, len(b.text), mycli.handle_unprettify_binding) @kb.add("c-o", "d", filter=emacs_mode) def _(event: KeyPressEvent) -> None: