Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GHCi on Windows: how to read Unicode input from console?

How to make the following code work with Unicode (CJK) in Command Prompt/LameShell?

main = getLine >>= putStrLn

(Windows 8.1, Haskell Platform 2014)

No, chcp 65001 doesn't work, nor chcp 10000.

No, I don't want to change System Local (Control Panel/Clock, Language, and Region/Language/Change date, time, or number formats/Administrative) from English(United States) to others.

Yes, I have set font to Lucida Console.

I've tried ConEmu/Cmder/Console2, they all said "Exception: : hGetLine: end of file". I also tried Emacs, (I use Vim), it didn't crash, but I always get spaces, no matter what I input.

Patch GHC and recompile it is acceptable, if there is no other way.

Any idea?

Thanks.

like image 703
Not an ID Avatar asked Feb 28 '15 09:02

Not an ID


People also ask

How do I input unicode characters into a console application?

For input: allow HEX input of Unicode. One more gotcha with “Pasting” into a console application (very technical): HEX input delivers a character on KeyUpof Alt; allthe other ways to deliver a character happen on KeyDown; so many applications are not ready to see a character on KeyUp.

Does Windows Console support Unicode?

Windows’ console has A LOT of support for Unicode — but it is not perfect (just “good enough”; see below). chcp 65001 is very dangerous. Unless a program was specially designed to work around defects in the Windows’ API (or uses a C runtime library which has these workarounds), it would not work reliably.

Is it possible to use Unicode characters in CMD?

CMD.exe is a just one of programs which are ready to “work inside” a console (“console applications”). AFAIK, CMD has perfect support for Unicode; you can enter/output all Unicode chars when any codepage is active. Windows’ console has A LOT of support for Unicode — but it is not perfect (just “good enough”; see below).

Is it possible to pass Unicode arguments to console?

– phuclv May 28 '17 at 4:07 2 @LưuVĩnhPhúc - No, this is about passing unicode command line arguments, rather than displaying text in the console. Console might not get involved at all. – Vilx- May 28 '17 at 11:08


1 Answers

PowerShell supports UTF natively. Assuming I understand your example (main = getLine >>= putStrLn) properly:

$var = Read-Host
Write "$var"

Or simply:

Read-Host
like image 131
Maximilian Burszley Avatar answered Sep 28 '22 11:09

Maximilian Burszley