开发者

How do I set the 'page width' in Haskell using ansi-wl-pprint library?

I am currently using the ansi-wl-pprint library. 开发者_如何学PythonThe width function is undocumented, but I need to set it so that the width of the page is 80 characters instead of the default 100.

How do I do this? Function width accepts a Doc as its first argument and then a function from Int -> Doc as the second argument. There are no instructions on what this function is meant to do.


Provided ansi-wl-pprint follows wl-pprint, you want to use the renderPretty function to render the Doc to a different width and possibly use a different ribbon-fraction (here 0.7), something like this should work:

show100 :: Doc -> String
show100 d = displayS (renderPretty 0.7 100 d) ""

See the last section of the manual on Daan Leijen's old website:

http://legacy.cs.uu.nl/daan/download/pprint/pprint.pdf


Looking at the definition of the fill and width functions, it seems the second argument to width gets called with the width of its first arg and produces a document accordingly.

fill :: Int -> Doc -> Doc
fill f d = width d (\w ->
                  if (w >= f) then empty
                              else text (spaces (f - w)))

width :: Doc -> (Int -> Doc) -> Doc
width d f = column (\k1 -> d <> column (\k2 -> f (k2 - k1)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜