fix(TextPrompt): support non-ASCII characters in editable default values - #2159
Open
danjourno-dev wants to merge 2 commits into
Open
fix(TextPrompt): support non-ASCII characters in editable default values#2159danjourno-dev wants to merge 2 commits into
danjourno-dev wants to merge 2 commits into
Conversation
ConsoleKeyInfo constructor throws ArgumentOutOfRangeException when the ConsoleKey value exceeds 255. Non-ASCII chars (e.g. Korean, CJK) have code points above 255, so casting directly to ConsoleKey was invalid. Also fixes two related wide-character display bugs: - Autocomplete erase used char count instead of cell width, leaving ghost characters on screen for double-width glyphs - Backspace in secret mode over-erased cells for wide chars; now erases one cell per mask character regardless of source char width - Backspace now handles surrogate pairs correctly instead of splitting them Closes spectreconsole#2132
Author
@microsoft-github-policy-service agree |
byte2pixel
reviewed
Jul 6, 2026
byte2pixel
approved these changes
Jul 6, 2026
patriksvensson
requested changes
Jul 12, 2026
| [Fact] | ||
| public void Should_Backspace_Remove_Surrogate_Pair_As_Single_Rune_Leaving_Preceding_Char() | ||
| { | ||
| // Given: "a😀" — 'a' then emoji (surrogate pair 😀) |
Contributor
There was a problem hiding this comment.
Suggested change
| // Given: "a😀" — 'a' then emoji (surrogate pair 😀) | |
| // Given |
| // When | ||
| var result = console.Prompt(new TextPrompt<string>("Enter:")); | ||
|
|
||
| // Then: both surrogate chars removed as one rune; 'a' remains |
Contributor
There was a problem hiding this comment.
Suggested change
| // Then: both surrogate chars removed as one rune; 'a' remains | |
| // Then |
| [Fact] | ||
| public void Should_Backspace_Remove_Lone_Emoji_Leaving_Empty_String() | ||
| { | ||
| // Given: only an emoji (surrogate pair); AllowEmpty so prompt accepts "" |
Contributor
There was a problem hiding this comment.
Suggested change
| // Given: only an emoji (surrogate pair); AllowEmpty so prompt accepts "" | |
| // Given |
| // When | ||
| var result = console.Prompt(new TextPrompt<string>("Enter:").AllowEmpty()); | ||
|
|
||
| // Then: both surrogate chars removed, nothing left |
Contributor
There was a problem hiding this comment.
Suggested change
| // Then: both surrogate chars removed, nothing left | |
| // Then |
| [Expectation("SecretValueBackspaceWideChar")] | ||
| public Task Should_Erase_One_Mask_Cell_Per_Backspace_For_Wide_Char_In_Secret_Mode() | ||
| { | ||
| // Given: type '한' (double-width CJK), backspace, enter — in secret mode |
Contributor
There was a problem hiding this comment.
Suggested change
| // Given: type '한' (double-width CJK), backspace, enter — in secret mode | |
| // Given |
| [Fact] | ||
| public void Should_Backspace_Through_Mixed_Width_Sequence_Peeling_One_Rune_At_A_Time() | ||
| { | ||
| // Given: "a한😀" — ASCII (1 cell) + CJK (1 char, 2 cells) + emoji (surrogate pair) |
Contributor
There was a problem hiding this comment.
Suggested change
| // Given: "a한😀" — ASCII (1 cell) + CJK (1 char, 2 cells) + emoji (surrogate pair) | |
| // Given |
| var console = new TestConsole(); | ||
| console.Input.PushKey(new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false)); | ||
| console.Input.PushKey(new ConsoleKeyInfo('한', ConsoleKey.Packet, false, false, false)); | ||
| // 😀 = U+1F600 = surrogate pair 😀 |
Contributor
There was a problem hiding this comment.
Suggested change
| // 😀 = U+1F600 = surrogate pair 😀 |
| // 😀 = U+1F600 = surrogate pair 😀 | ||
| console.Input.PushKey(new ConsoleKeyInfo('\uD83D', ConsoleKey.Packet, false, false, false)); | ||
| console.Input.PushKey(new ConsoleKeyInfo('\uDE00', ConsoleKey.Packet, false, false, false)); | ||
| console.Input.PushKey(ConsoleKey.Backspace); // removes emoji surrogate pair as one rune |
Contributor
There was a problem hiding this comment.
Suggested change
| console.Input.PushKey(ConsoleKey.Backspace); // removes emoji surrogate pair as one rune | |
| console.Input.PushKey(ConsoleKey.Backspace); |
| console.Input.PushKey(new ConsoleKeyInfo('\uD83D', ConsoleKey.Packet, false, false, false)); | ||
| console.Input.PushKey(new ConsoleKeyInfo('\uDE00', ConsoleKey.Packet, false, false, false)); | ||
| console.Input.PushKey(ConsoleKey.Backspace); // removes emoji surrogate pair as one rune | ||
| console.Input.PushKey(ConsoleKey.Backspace); // removes '한' |
Contributor
There was a problem hiding this comment.
Suggested change
| console.Input.PushKey(ConsoleKey.Backspace); // removes '한' | |
| console.Input.PushKey(ConsoleKey.Backspace); |
| // When | ||
| var result = console.Prompt(new TextPrompt<string>("Enter:")); | ||
|
|
||
| // Then: 'a' remains; emoji and CJK each removed as single runes |
Contributor
There was a problem hiding this comment.
Suggested change
| // Then: 'a' remains; emoji and CJK each removed as single runes | |
| // Then |
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.
Fixes #2132
Claude Code assisted with designing a proper fix. Once reviewed, I applied the fix and created a local console app to manually test the fix. The results of that test are...
Note: if you are testing this, you may need to add the following into your console app
Console.OutputEncoding = System.Text.Encoding.UTF8;Changes
ConsoleKeyInfo constructor throws ArgumentOutOfRangeException when the ConsoleKey value exceeds 255. Non-ASCII chars (e.g. Korean, CJK) have code points above 255, so casting directly to ConsoleKey was invalid.
Also fixes two related wide-character display bugs:
Please upvote 👍 this pull request if you are interested in it.