更改 julia 提示以包含评估编号

Change julia promt to include evalutation numbers

在 REPL 中调试或 运行 julia 代码时,我通常会看到显示 ... at ./REPL[161]:12 [inlined]... 的错误消息。我猜数字 161 表示 REPL 中的 161-th 评估。所以我的问题是我们可以在 julia 的提示中显示这个数字,即 julia [161]> 而不是 julia>?

Julia 的优势之一是其超强的灵活性。这在 Julia 0.7(夜间版)中非常容易。

julia> repl = Base.active_repl.interface.modes[1]
"Prompt(\"julia> \",...)"

julia> repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
#1 (generic function with 1 method)

julia[3] >

julia[3] >2
2

julia[4] >f = () -> error("e")
#3 (generic function with 1 method)

julia[5] >f()
ERROR: e
Stacktrace:
 [1] error at .\error.jl:33 [inlined]
 [2] (::getfield(, Symbol("##3#4")))() at .\REPL[4]:1
 [3] top-level scope

您只需将前 2 行放在 ~/.juliarc 上即可享受~

由于 julia 0.7 之后的 REPL 有一些变化,这些代码在旧版本中不起作用。

编辑:好吧,实际上需要付出更多努力才能使其在 .juliarc.jl 中发挥作用。试试这个代码:

atreplinit() do repl
    repl.interface = Base.REPL.setup_interface(repl)
    repl = Base.active_repl.interface.modes[1]
    repl.prompt = () -> "julia[$(length(repl.hist.history) - repl.hist.start_idx + 1)] >"
end