Mark Karpov
Mark Karpov

Reputation: 7599

Windows console doesn't seem to like Unicode

I usually develop Haskell programs on Linux, and then build and test some of them on Windows. Say, I have some console program that outputs Unicode symbols. In particular, it happens to output copyright symbol ‘©’, like this:

Copyright © 2015 Boo

It works fine on Arch Linux, but on Windows 7 it prints something like:

Copyright program_name.EXE: <stdout>: commitAndReleaseBuffer: invalid
argument (invalid character)

I'm not sure, but I think it's should be feasible to output unicode symbols in Windows console without any additional magic.

Here are two questions:

  1. Is it Windows' or Haskell's fault?
  2. How can I fix it?

P.S. MinGHC has been used, since it has GHC 7.10.

Upvotes: 2

Views: 366

Answers (1)

Jeremy List
Jeremy List

Reputation: 1766

This function takes the current text encoding of a handle and makes it substitute safe characters like "?" when outputting characters that are not supported by the console (nb: it's the Windows console itself that doesn't support these characters, but most other languages with unicode support apply work-arounds by default)

makeSafe h = do
  e <- hGetEncoding h
  case e of
    Nothing -> return ()
    Just e1 -> do
      e' <- mkTextEncoding (show e1 ++ "//TRANSLIT")
      hSetEncoding h e'

It can be used at the beginning of the main function like this:

main = do
  mapM_ makeSafe [stdout,stderr,stdin]
  ...

Upvotes: 2

Related Questions