How to hack GHCi (or Hugs) so that it prints Unicode chars unescaped?

前端 未结 7 1908
情话喂你
情话喂你 2020-12-05 00:26

Look at the problem: Normally, in the interactive Haskell environment, non-Latin Unicode characters (that make a part of the results) are printed escaped, even if the locale

7条回答
  •  半阙折子戏
    2020-12-05 01:11

    One way to hack this is to wrap GHCi into a shell wrapper that reads its stdout and unescapes Unicode characters. This is not the Haskell way of course, but it does the job :)

    For example, this is a wrapper ghci-esc that uses sh and python3 (3 is important here):

    #!/bin/sh
    
    ghci "$@" | python3 -c '
    import sys
    import re
    
    def tr(match):
        s = match.group(1)
        try:
            return chr(int(s))
        except ValueError:
            return s
    
    for line in sys.stdin:
        sys.stdout.write(re.sub(r"\\([0-9]{4})", tr, line))
    '
    

    Usage of ghci-esc:

    $ ./ghci-esc
    GHCi, version 7.0.2: http://www.haskell.org/ghc/  :? for help
    > "hello"
    "hello"
    > "привет"
    "привет"
    > 'Я'
    'Я'
    > show 'Я'
    "'\Я'"
    > :q
    Leaving GHCi.
    

    Note that not all unescaping above is done correctly, but this is a fast way to show Unicode output to your audience.

提交回复
热议问题