Fix multi-byte IME input loss in poll_event#7
Open
twokidsCarl wants to merge 1 commit intomarcoroth:mainfrom
Open
Fix multi-byte IME input loss in poll_event#7twokidsCarl wants to merge 1 commit intomarcoroth:mainfrom
twokidsCarl wants to merge 1 commit intomarcoroth:mainfrom
Conversation
When typing CJK characters via IME (e.g. "你好"), the terminal delivers multiple UTF-8 characters in a single read(). Previously, poll_event() called tea_parse_input_with_consumed() once, which parsed only the first character and discarded the remaining bytes. This caused only the first character to appear (e.g. "你" instead of "你好"). Fix: Add an internal byte buffer to bubbletea_program_t. After parsing, any unconsumed bytes are saved to the buffer. On the next poll_event() call, buffered bytes are consumed before reading from stdin. This ensures all characters from a single read are eventually delivered as separate events. The fix is transparent to the Ruby layer — poll_event() still returns a single Hash per call, and the run_loop naturally processes buffered events on subsequent iterations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When typing CJK characters via IME (Input Method Editor), such as Chinese "你好", only the first character appears in the TextArea. The second character is silently dropped.
Root cause: The terminal delivers multiple UTF-8 characters in a single
read()call.poll_event()callstea_parse_input_with_consumed()once, which parses only the first UTF-8 character and returnsconsumed = 3(bytes). The remaining bytes (the second character) are discarded because theconsumedvalue is never used to process the rest of the buffer.Fix
Add an internal byte buffer (
pending_buf/pending_len) tobubbletea_program_t. After parsing, any unconsumed bytes (bytes_avail - consumed) are saved to this buffer. On the nextpoll_event()call, buffered bytes are consumed before reading from stdin.This ensures all characters from a single
read()are eventually delivered as separate events across multiplepoll_event()calls.Changes
extension.h: Addedpending_buf[256]andpending_lenfields tobubbletea_program_tprogram.c: Modifiedpoll_eventto check/use buffer before reading stdin, save remaining bytes after parseBehavior
poll_event()still returns a singleHashper callrun_loopnaturally processes buffered events on subsequent iterationsTest
Typed "你好" (6 bytes:
E4 BD A0 E5 A5 BD) in a Bubbletea TUI with TextArea.Before: Only "你" appears
After: "你好" appears correctly
Compilation
Verified: compiles cleanly on macOS arm64 with Ruby 4.0.0.