Skip to content

feat: make Shift+Enter insert a newline by enabling terminal key modes - #20

Merged
wowi42 merged 7 commits into
mainfrom
feat/shift-enter-newline
Sep 30, 2026
Merged

wowi42 merged 7 commits into
mainfrom
feat/shift-enter-newline

Conversation

@wowi42

@wowi42 wowi42 commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Shift+Enter arrived as plain Enter in terminals that require keyboard mode enablement, so it submitted the prompt instead of inserting a newline. Enabling those modes also changes other keys: the original PR made Kitty Ctrl-C insert [99;5u into the composer, and xterm level 1 did not report Shift+Enter distinctly.

Fix

  • Enable Kitty keyboard disambiguation and xterm modifyOtherKeys level 2. Query xterm's prior mode and restore its reported value on exit; fall back to its configured default if it does not answer. Skip non-TTY output, TERM=dumb, and Windows.
  • Map the modified Ctrl, Alt, Escape, and Shift keys into prompt_toolkit, including Unicode text, so existing shortcuts and ordinary input keep working.
  • Suspend lecode's keyboard modes while Ctrl-G opens an external editor. Keep draft and session cleanup guaranteed if terminal restoration fails.

Validation

  • Ruff check and format checks passed.
  • Focused TUI tests cover Shift+Enter, Ctrl-C, xterm Unicode Shift/Alt input, mode restoration, editor handoff, and cleanup.
  • tests/test_tui_streaming_pty.py: 16 passed.
  • The full test_tui_app.py run is not green: generated-prompt hook cases fail, and test_pipe_ctrl_c_exits_cleanly passes but hangs after the test. The latter hang also occurs on an untouched main snapshot.

The chatbox already maps the Kitty (ESC [ 13 ; 2 u) and modifyOtherKeys
(ESC [ 27 ; 2 ; 13 ~) Shift+Enter sequences to c-j/newline, but lecode
never asked the terminal to send them, so Shift+Enter submitted the
prompt on terminals that need explicit enablement. TuiApp.run() now
pushes the Kitty keyboard-protocol disambiguate flag (CSI > 1 u) and
xterm modifyOtherKeys level 1 (CSI > 4 ; 1 m) on start, and restores
both on exit. Skipped on non-tty stdout, TERM=dumb, and Windows;
terminals without support ignore the sequences.
@Mouhand-Kaddo

Copy link
Copy Markdown
Contributor

Confirmed by testing the installed PR commit 90506f0965f761e13676451b9e706d241f3e7742: enabling Kitty keyboard disambiguation breaks existing shortcuts.

Reproduction on a terminal supporting the Kitty protocol:

  1. Start lecode and type some text.
  2. Press Ctrl-C.
  3. [99;5u is inserted into the prompt instead of clearing it.

_ENABLE_KEY_MODES enables flag 1 (CSI > 1 u), which changes Ctrl/Alt/Escape key encodings. _register_shift_enter() only adds support for Shift-Enter. The pinned prompt_toolkit parser treats the other CSI-u sequences as Escape followed by literal text. Ctrl-D, Ctrl-J, Escape, and Alt-Enter are also affected in parser reproductions.

Please support the CSI-u encodings of the existing controls before enabling this mode, and add regression coverage for Ctrl-C cancellation/clearing and the other affected shortcuts. The current tests supply legacy shortcut bytes, so they do not catch this regression.

Relevant code:

_ENABLE_KEY_MODES = "\x1b[>1u\x1b[>4;1m"

Protocol reference: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#disambiguate-escape-codes

@wowi42
wowi42 merged commit a843e96 into main Sep 30, 2026
4 checks passed
@wowi42
wowi42 deleted the feat/shift-enter-newline branch September 30, 2026 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants