PSReadLine icon indicating copy to clipboard operation
PSReadLine copied to clipboard

Problem with unicode input (copy-paste) on Windows 10 + Terminal

Open RoguePointer80 opened this issue 6 years ago • 4 comments

I am using Windows Terminal + Powershell 7.0.3. I noticed that I could not copy-paste some unicode characters into it. Here is some basic repro steps: open up your favorite web browser to : https://emojipedia.org/green-heart/ Copy the line at the bottom that contains the character and its code point to the clipboard (ctrl-c) example : 💚 U+1F49A

Open up Windows Terminal with a Powershell Core tab. Try to type Write-Host '' and paste the clipboard (ctrl-v) between the two ''. Expected: Write-Host '💚 U+1F49A'

💚 U+1F49A

Actual: image

Textual version: Oops, something went wrong. Please report this bug with the details below. Report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new

Environment

PSReadLine: 2.0.0-rc2 PowerShell: 7.0.3 OS: Microsoft Windows 10.0.19041 Last 23 Keys

 W r i t e - H o s t Spacebar ' � Spacebar U + 1 F 4 9 A ' Enter

Exception

System.Text.EncoderFallbackException: Unable to translate Unicode character \\uD83D at index 12 to specified code page.   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
   at System.Text.EncoderFallbackBuffer.InternalFallback(ReadOnlySpan`1 chars, Int32& charsConsumed)
   at System.Text.Encoding.GetBytesWithFallback(ReadOnlySpan`1 chars, Int32 originalCharsLength, Span`1 bytes, Int32 originalBytesLength, EncoderNLS encoder)
   at System.Text.Encoding.GetBytesWithFallback(Char* pOriginalChars, Int32 originalCharCount, Byte* pOriginalBytes, Int32 originalByteCount, Int32 charsConsumedSoFar, Int32 bytesWrittenSoFar, EncoderNLS encoder)
   at System.Text.Encoding.GetBytes(Char* pChars, Int32 charCount, Byte* pBytes, Int32 byteCount, EncoderNLS encoder)
   at System.Text.EncoderNLS.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount, Boolean flush)
   at System.Text.EncoderNLS.GetBytes(Char[] chars, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex, Boolean flush)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Dispose(Boolean disposing)
   at System.IO.TextWriter.Dispose()
   at Microsoft.PowerShell.PSConsoleReadLine.<>c__DisplayClass83_0.<WriteHistoryRange>b__0()
   at Microsoft.PowerShell.PSConsoleReadLine.WithHistoryFileMutexDo(Int32 timeout, Action action)
   at Microsoft.PowerShell.PSConsoleReadLine.WriteHistoryRange(Int32 start, Int32 end, Func`2 fileOpener)
   at Microsoft.PowerShell.PSConsoleReadLine.IncrementalHistoryWrite()
   at Microsoft.PowerShell.PSConsoleReadLine.MaybeAddToHistory(String result, List`1 edits, Int32 undoEditIndex, Boolean fromDifferentSession, Boolean fromInitialRead)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken)

I actually tried to paste the same clipboard into VS Code and it worked perfectly. Also, just for fun, I tried to paste the equivalent into CMD.exe: image

to my suprise it nearly worked. My System Locale is English, but I have activated the beta feature "use UTF-8" that makes the system code page 65001.

RoguePointer80 avatar Jul 30 '20 18:07 RoguePointer80

This seems like a problem with UTF-16 surrogate pairs.

RoguePointer80 avatar Jul 30 '20 20:07 RoguePointer80

I am able to reproduce this

Environment

PSReadLine: 2.1.0-beta2 PowerShell: 5.1.20180.1000 OS: Microsoft Windows 10.0.20180 BufferWidth: 120 BufferHeight: 30 Last 24 Keys

 w r i t e - h o s t Spacebar ' ' LeftArrow � Spacebar U + 1 F 4 9 A Enter

Exception

System.Text.EncoderFallbackException: Unable to translate Unicode character \uD83D at index 12 to specified code page.
   at System.Text.EncoderExceptionFallbackBuffer.Fallback(Char charUnknown, Int32 index)
   at System.Text.EncoderFallbackBuffer.InternalFallback(Char ch, Char*& chars)
   at System.Text.UTF8Encoding.GetBytes(Char* chars, Int32 charCount, Byte* bytes, Int32 byteCount, EncoderNLS baseEncoder)
   at System.Text.EncoderNLS.GetBytes(Char[] chars, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex, Boolean flush)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Dispose(Boolean disposing)
   at System.IO.TextWriter.Dispose()
   at Microsoft.PowerShell.PSConsoleReadLine.<>c__DisplayClass83_0.<WriteHistoryRange>b__0()
   at Microsoft.PowerShell.PSConsoleReadLine.WithHistoryFileMutexDo(Int32 timeout, Action action)
   at Microsoft.PowerShell.PSConsoleReadLine.MaybeAddToHistory(String result, List`1 edits, Int32 undoEditIndex, Boolean fromDifferentSession, Boolean fromInitialRead)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken)
> chcp
Active code page: 437
cmd->

>echo '💚 U+1F49A'
'💚 U+1F49A'

syntroniks avatar Aug 17 '20 16:08 syntroniks

Looks like a duplicate of #949

syntroniks avatar Aug 17 '20 17:08 syntroniks

Closing as a dup of #949

daxian-dbw avatar Aug 28 '20 19:08 daxian-dbw