Peter Simons | 8 Mar 13:10 2013
Picon

How to avoid trailing whitespace generated by Text.PrettyPrint.HughesPJ?

Hi guys,

it seems to me like the HughesPJ pretty printer has trouble dealing with
empty lines in a nested environment. Consider the following example:

> import Text.PrettyPrint.HughesPJ
> test1 = putStrLn $ render $ nest 4 $ vcat $ map text [ "line 1", "", "line 2" ]

The 'test1' function prints the following output, where blanks have been
replaced by underscores to make them visible:

  *Main> test1
  ____line_1
  ____
  ____line_2

Now, I would like to avoid this kind of trailing whitespace in the
document I'm generated -- but how? My first thought was to replace
text "" by empty, but that change effectively removes the empty line
altogether:

> test2 = putStrLn $ render $ nest 4 $ vcat $ [ text "line 1", empty , text "line 2" ]

  *Main> test2
  ____line_1
  ____line_2

Is there another way to accomplish what I want?

Take care,
(Continue reading)

Ivan Lazar Miljenovic | 8 Mar 22:22 2013
Picon

Re: How to avoid trailing whitespace generated by Text.PrettyPrint.HughesPJ?

On 8 March 2013 23:10, Peter Simons <simons <at> cryp.to> wrote:
> Hi guys,
>
> it seems to me like the HughesPJ pretty printer has trouble dealing with
> empty lines in a nested environment. Consider the following example:
>
>> import Text.PrettyPrint.HughesPJ
>> test1 = putStrLn $ render $ nest 4 $ vcat $ map text [ "line 1", "", "line 2" ]
>
> The 'test1' function prints the following output, where blanks have been
> replaced by underscores to make them visible:
>
>   *Main> test1
>   ____line_1
>   ____
>   ____line_2
>
> Now, I would like to avoid this kind of trailing whitespace in the
> document I'm generated -- but how? My first thought was to replace
> text "" by empty, but that change effectively removes the empty line
> altogether:
>
>> test2 = putStrLn $ render $ nest 4 $ vcat $ [ text "line 1", empty , text "line 2" ]
>
>   *Main> test2
>   ____line_1
>   ____line_2
>
> Is there another way to accomplish what I want?

(Continue reading)

Henning Thielemann | 8 Mar 22:31 2013
Picon

Re: How to avoid trailing whitespace generated by Text.PrettyPrint.HughesPJ?


On Sat, 9 Mar 2013, Ivan Lazar Miljenovic wrote:

> The pretty-printer is doing what it _should_ be doing here (namely
> indenting everything in that block); to get rid of unneeded trailing
> whitespace the only thing I can think of is to post-process the output
> (e.g.: unlines . reverse . dropWhile isSpace .  reverse . lines).

You certainly mean

    unlines . map (reverse . dropWhile isSpace .  reverse) . lines

Btw. (dropWhileRev isSpace) would be lazier:

    http://hackage.haskell.org/packages/archive/utility-ht/0.0.8/doc/html/Data-List-HT.html#v:dropWhileRev
Peter Simons | 9 Mar 08:53 2013
Picon

Re: How to avoid trailing whitespace generated by Text.PrettyPrint.HughesPJ?

Hi Ivan,

 >>   *Main> test1
 >>   ____line_1
 >>   ____
 >>   ____line_2
 >
 > The pretty-printer is doing what it _should_ be doing here (namely
 > indenting everything in that block).

when you say "everything" you really mean "nothing", right? Notice that
the line is empty. There is nothing to indent and these blanks are quite
unnecessary. It's a bit of a stretch to say that the pretty printer
should be adding them despite the fact that they serve no purpose.

 > to get rid of unneeded trailing whitespace the only thing I can think
 > of is to post-process the output (e.g.: unlines . reverse . dropWhile
 > isSpace . reverse . lines).

Well, if there is no other choice, then I'll do that.

Take care,
Peter

Gmane