增加ghci的“宽度”

时间:2016-08-19 14:41:55

标签: haskell ghci

当GHCI中的输出线太长时,它会断开:

> :i bar
bar :: Lens' (Foo a0) Int   -- Defined at NewType_makeLenses.hs:7:1
> :i baz
baz :: Lens (Foo a0) (Foo a1) a0 a1
    -- Defined at NewType_makeLenses.hs:7:1

有没有办法设置线条的最大长度?

1 个答案:

答案 0 :(得分:21)

有三个选项可以控制pretty printing:

-dppr-debug         Turn on debug printing (more verbose)
-dppr-user-length   Set the depth for printing expressions in error msgs    
-dppr-cols⟨N⟩       Set the width of debugging output. For example -dppr-cols200

您正在寻找-dppr-cols。其默认值为100。您可以在调用GHCi或:set时将其设置为任何其他值。

选项的比较

没有-dppr-cols

$ ghci NewType_makeLenses.hs
[1 of 1] Compiling Main             ( NewType_makeLenses.hs, interpreted )  
Ok, modules loaded: Main.                                                   
> :i bar                                                                    
bar :: Lens' (Foo a0) Int       -- Defined at NewType_makeLenses.hs:9:1     
> :i baz                                                                    
baz :: Lens (Foo a0) (Foo a1) a0 a1                                         
        -- Defined at NewType_makeLenses.hs:10:1   

使用-dppr-cols140

$ ghci -dppr-cols140 NewType_makeLenses.hs
[1 of 1] Compiling Main             ( NewType_makeLenses.hs, interpreted )
Ok, modules loaded: Main.
> :i bar
bar :: Lens' (Foo a0) Int       -- Defined at NewType_makeLenses.hs:9:1
> :i baz
baz :: Lens (Foo a0) (Foo a1) a0 a1     -- Defined at NewType_makeLenses.hs:10:1

使用:set -dppr-cols140

$ ghci NewType_makeLenses.hs
[1 of 1] Compiling Main             ( NewType_makeLenses.hs, interpreted )
Ok, modules loaded: Main.
> :set -dppr-cols140
> :i bar
bar :: Lens' (Foo a0) Int       -- Defined at NewType_makeLenses.hs:9:1
> :i baz
baz :: Lens (Foo a0) (Foo a1) a0 a1     -- Defined at NewType_makeLenses.hs:10:1

奖金:我怎么找到这个?

我没有看过旗帜,相反,我查看了GHC的源代码:

$ git clone --depth=1 https://github.com/ghc/ghc.git && cd ghc

接下来,我查找以"Defined开头的字符串:

$ grep -C2 "\"Defined" -r . --exclude-dir=testsuite
./compiler/basicTypes/Name.hs-ppr_z_occ_name occ = ztext (zEncodeFS (occNameFS occ))
./compiler/basicTypes/Name.hs-
./compiler/basicTypes/Name.hs:-- Prints (if mod information is available) "Defined at <loc>" or
./compiler/basicTypes/Name.hs:--  "Defined in <mod>" information for a Name.
./compiler/basicTypes/Name.hs-pprDefinedAt :: Name -> SDoc
./compiler/basicTypes/Name.hs:pprDefinedAt name = text "Defined" <+> pprNameDefnLoc name
./compiler/basicTypes/Name.hs-
./compiler/basicTypes/Name.hs-pprNameDefnLoc :: Name -> SDoc

SDoc似乎很有趣。

$ grep "data SDoc" -r . --exclude-dir=testsuite
./compiler/utils/Outputable.hs:data SDocContext = SDC
./compiler/utils/Outputable.hs-boot:data SDoc

Outputable.hs包含printForUserpprCol dflagsprintDoc Pretty一起使用printDocprintDoc :: Mode -> Int -> Handle -> Doc -> IO () -- printDoc adds a newline to the end printDoc mode cols hdl doc = printDoc_ mode cols hdl (doc $$ text "") 定义为

pprCol

compiler/main/DynFlags.hs-dppr-cols中定义,其中grep对应.first_letter_post::first-letter { float: left; padding-right: 20px; padding-top: 0px; margin-bottom: -15px; margin-top: -10px; font-size: 50px; font-weight: bold; text-transform: uppercase; }。您可以通过GHC <p class="first_letter_post">foobar</p>方式:)。