:i bar bar :: Lens' (Foo a0) Int -- Defined at NewType_makeLenses.hs:7:1 >-6ren">
gpt4 book ai didi

haskell - 增加ghci的 "width"

转载 作者:行者123 更新时间:2023-12-02 04:32:34 25 4
gpt4 key购买 nike

当 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

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

最佳答案

共有三个选项控制 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 包含 printForUser,它使用 pprCol dflags 以及 Pretty< 中的 printDoc/printDoc 定义为

printDoc :: Mode -> Int -> Handle -> Doc -> IO ()
-- printDoc adds a newline to the end
printDoc mode cols hdl doc = printDoc_ mode cols hdl (doc $$ text "")

pprColcompiler/main/DynFlags.hs中定义,它对应于-dppr-cols。您只需 grep 即可通过 GHC :)。

关于haskell - 增加ghci的 "width",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com