8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ghciでマルチバイト文字をエスケープせず表示させる

Posted at

ghci 8.0.1の話。
マルチバイト文字が"\12411\12370"のようにエスケープされて表示されるが、

ghci> "ほげ"
"\12411\12370"

これを

ghci> "ほげ"
"ほげ"

と表示させるようにしたい。

まず~/MyPrint.hsを作成:

MyPrint.hs
module MyPrint (myPrint, myShow) where
-- preparing for the 7.6.1
myPrint :: Show a => a -> IO ()
myPrint = putStrLn . myShow

myShow :: Show a => a -> String
myShow x = con (show x) where
  con :: String -> String
  con [] = []
  con li@(x:xs) | x == '\"' = '\"':str++"\""++(con rest)
                | x == '\'' = '\'':char:'\'':(con rest')
                | otherwise = x:con xs where
                  (str,rest):_ = reads li
                  (char,rest'):_ = reads li

シェルでaliasを設定:
.ghciで設定する方がベターだと思うのだが、Haskell超初心者なのでその書き方も分からなかった)

alias ghci="ghci -interactive-print=MyPrint.myPrint MyPrint -i$HOME"

これで一応はできたのだが、:load:reloadを使うと元に戻ってしまう。
これは-interactive-printの仕様らしい:

The function can reside in any loaded module or any registered package, but only when it resides in a registered package will it survive a :cd, :add, :load, :reload or, :set.
https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghci.html#using-a-custom-interactive-printing-function

ghciをカラー化するツールもあるので、それをベースにエスケープされた文字列をアンエスケープする方法もある…らしい。
https://wiki.haskell.org/GHCi_in_colour

参考

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?