After reading your reply to my comment, I think your situation is that you have some data structure, maybe with type [(String,String)]
, and you'd like to output it for debugging purposes. Using show
would be convienent, but it escapes non-ASCII characters.
The problem here isn't with the unicode, you need a function that will properly format your data for display. I don't think show
is the right choice, in part because of the problems with escaping some characters. What you need is a type class like Show
, but one that displays data for reading instead of escaping characters. That is, you need a pretty-printer, which is a library that provides functions to format data for display. There are several pretty-printers available on Hackage, I'd look at uulib or wl-pprint to start. I think either would be suitable without too much work.
Here's an example with the uulib tools. The Pretty
type class is used instead of Show, the library comes with many useful instances.
import UU.PPrint
-- | Write each item to StdOut
logger :: Pretty a => a -> IO ()
logger x = putDoc $ pretty x <+> line
running this in ghci:
Prelude UU.PPrint> logger 'Д'
Д
Prelude UU.PPrint> logger ('Д', "other text", 54)
(Д,other text,54)
Prelude UU.PPrint>
If you want to output to a file instead of the console, you can use the hPutDoc
function to output to a handle. You could also call renderSimple
to produce a SimpleDoc
, then pattern match on the constructors to process output, but that's probably more trouble. Whatever you do, avoid show
:
Prelude UU.PPrint> show $ pretty 'Д'
"\1044"
You could also write your own type class similar to show but formatted as you like it. The Text.Printf
module can be helpful if you go this route.