-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set console to UTF-8 encoding on Windows #3951
base: master
Are you sure you want to change the base?
Conversation
kelleyma49
commented
Aug 4, 2024
- Should resolve Can't input non ascii characters #3799
- Here's an example with my change:
Thank you very much for the patch. I've tested the patch on cmd.exe on my Windows VM, but when I start fzf with 2024-08-04.1.52.57.mov |
I did all of my testing in Windows Terminal, so let me test some more. It could be because of this:
https://learn.microsoft.com/en-us/windows/console/legacymode#api-differences |
I've finally had a chance to test this on Windows Terminal. It works, however, on Windows Terminal, I don't seem to have any problem entering non-ASCII characters even without the patch (binary built from master branch). |
Sorry, I was wrong. This does fix the problem on Windows Terminal. Before the patch, I had to manually run But one thing I noticed is that it permanently changes the |
I would advice to avoid changing the code page permanently as it may lead to side effects with other tools that expect the default code page. I think a simple solution is to query the initial code page with GetConsoleCP and restore it at the end.
It is a solution but users need to be aware of the meaning of changing chcp. Because of some issues I've had in the past, I think it is usually better to always restore the code page to whatever it was before. I even created a powershell script which only purpose is to run things with a code page change and revert it back once it is done. # With-UTF8.ps1
# Usage
# ```powershell
# With-UTF8 { command }
# ```
[CmdletBinding()]
Param (
[scriptblock] $block
)
try {
# Ref: https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell
# Save the current settings and temporarily switch to UTF-8.
$oldOutputEncoding = $OutputEncoding;
$oldConsoleEncoding = [Console]::OutputEncoding
$OutputEncoding = [Console]::OutputEncoding = New-Object System.Text.Utf8Encoding
# Execute block with utf-8 encoding
return & $block
} finally {
# Restore the previous settings.
$OutputEncoding = $oldOutputEncoding;
[Console]::OutputEncoding = $oldConsoleEncoding
} I use this script very frequently on some custom functions that use fzf to handle the encoding of file names correctly (for display rather than for input) and I've never had an issue with fzf itself, so I'd say that changing the code page internally temporarily should be fine. |