ronisbr / TerminalPager.jl

Pure Julia implementation of the command less
MIT License
116 stars 8 forks source link

interfacing with `AbbreviatedStackTraces` #33

Open haakon-e opened 11 months ago

haakon-e commented 11 months ago

AbbreviatedStackTraces.jl is a package which essentially shortens stack traces to "your own" code, relying on the (often correct) assumption that you are doing something wrong, not a bunch of julia internals.

Taking the example from their docs, if I am using AbbreviatedStackTraces and type sum([]), I get an easily readable stack trace:

julia> using AbbreviatedStackTraces
[ Info: Precompiling AbbreviatedStackTraces [ac637c84-cc71-43bf-9c33-c1b4316be3d4]
[ Info: Skipping precompilation since __precompile__(false). Importing AbbreviatedStackTraces [ac637c84-cc71-43bf-9c33-c1b4316be3d4].

julia> sum([])
ERROR: MethodError: no method matching zero(::Type{Any})

Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T
   @ Base missing.jl:105
  zero(::Union{Type{P}, P}) where P<:Period
   @ Dates ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/Dates/src/periods.jl:51
  zero(::FillArrays.Zeros{T, N}) where {T, N}
   @ FillArrays ~/.julia/packages/FillArrays/FwMoZ/src/FillArrays.jl:591
  ...

Stacktrace:
  [1-12] ⋮ internal
       @ Base, Unknown
    [13] sum(a::Vector{Any})
       @ Base ./reducedim.jl:994
    [14] top-level scope
       @ REPL[137]:1
Use `err` to retrieve the full stack trace.

If I am instead in pager> mode (or, indeed, if I am not using AbbreviatedStackTraces) I get a really long error message!

expand really long message!! ```julia pager> sum([]) ERROR: MethodError: no method matching zero(::Type{Any}) Closest candidates are: zero(::Type{Union{Missing, T}}) where T @ Base missing.jl:105 zero(::Union{Type{P}, P}) where P<:Period @ Dates ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/Dates/src/periods.jl:51 zero(::FillArrays.Zeros{T, N}) where {T, N} @ FillArrays ~/.julia/packages/FillArrays/FwMoZ/src/FillArrays.jl:591 ... Stacktrace: [1] zero(#unused#::Type{Any}) @ Base ./missing.jl:106 [2] reduce_empty(#unused#::typeof(+), #unused#::Type{Any}) @ Base ./reduce.jl:338 [3] reduce_empty(#unused#::typeof(Base.add_sum), #unused#::Type{Any}) @ Base ./reduce.jl:347 [4] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type) @ Base ./reduce.jl:367 [5] reduce_empty(op::Base.MappingRF{typeof(identity), typeof(Base.add_sum)}, #unused#::Type{Any}) @ Base ./reduce.jl:356 [6] reduce_empty_iter @ ./reduce.jl:379 [inlined] [7] mapreduce_empty_iter(f::Function, op::Function, itr::Vector{Any}, ItrEltype::Base.HasEltype) @ Base ./reduce.jl:375 [8] _mapreduce(f::typeof(identity), op::typeof(Base.add_sum), #unused#::IndexLinear, A::Vector{Any}) @ Base ./reduce.jl:427 [9] _mapreduce_dim @ ./reducedim.jl:365 [inlined] [10] mapreduce @ ./reducedim.jl:357 [inlined] [11] _sum @ ./reducedim.jl:999 [inlined] [12] _sum @ ./reducedim.jl:998 [inlined] [13] sum(a::Vector{Any}) @ Base ./reducedim.jl:994 [14] top-level scope @ none:1 [15] eval @ ./boot.jl:370 [inlined] [16] _tp_mode_do_cmd(repl::REPL.LineEditREPL, input::String) @ TerminalPager ~/.julia/packages/TerminalPager/MpAXn/src/repl.jl:243 [17] (::TerminalPager.var"#11#14"{REPL.LineEditREPL, REPL.LineEdit.Prompt})(s::REPL.LineEdit.MIState, buf::IOBuffer, ok::Bool) @ TerminalPager ~/.julia/packages/TerminalPager/MpAXn/src/repl.jl:41 [18] #invokelatest#2 @ ./essentials.jl:819 [inlined] [19] invokelatest @ ./essentials.jl:816 [inlined] [20] run_interface(terminal::REPL.Terminals.TextTerminal, m::REPL.LineEdit.ModalInterface, s::REPL.LineEdit.MIState) @ REPL.LineEdit ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/LineEdit.jl:2647 [21] run_frontend(repl::REPL.LineEditREPL, backend::REPL.REPLBackendRef) @ REPL ~/.julia/juliaup/julia-1.9.3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/REPL/src/REPL.jl:1300 [22] (::REPL.var"#62#68"{REPL.LineEditREPL, REPL.REPLBackendRef})() @ REPL ./task.jl:514 ```
ronisbr commented 11 months ago

Hum, this will be a difficult one. AbbreviatedStackTraces.jl overrides a lot of functions in REPL.jl. However, we cannot rely on those functions because we need to render them for a pager. It will take either a full rewrite of the REPL mode in TerminalPager.jl or modification in AbbreviatedStackTraces.jl to add an extension to support TerminalPager.jl. I am not sure what is the best approach yet.

haakon-e commented 11 months ago

I suspected this may be a bigger change. Apart from the substantial amount of time/effort needed for the change, is this something you think is useful and worth working towards (at some point)? Would it help to loop in the maintainer for AbbreviatedStackTraces.jl to hear their view?

ronisbr commented 11 months ago

Yes, sure! It would be awesome to add this support in TerminalPager.jl!