Julia: Should lpad/rpad be defined in terms of textwidth instead of code units or have the option to do so?

Created on 1 Nov 2020  ·  3Comments  ·  Source: JuliaLang/julia

One of the main use cases of lpad and rpad is, at least for me personally, to align things in the terminal. However, lpad and rpad are defined (and documented) to work in terms of code units (i.e. they call length on the input string) which means that when length and textwidth disagree, you get unaligned output:

julia> s1 = "⟨k|H₁|k̃⟩"
"⟨k|H₁|k̃⟩"
julia> s2 = "⟨k|H₁|k⟩"
"⟨k|H₁|k⟩"
julia> textwidth(s1), textwidth(s2)
(8, 8)
julia> rpad(s1, 12) |> textwidth
11
julia> rpad(s2, 12) |> textwidth
12

This can cause bugs like https://github.com/KristofferC/TimerOutputs.jl/issues/94.

Two points of discussions:

  • Should the default be changed to use textwidth instead of length? Perhaps too much of a breaking change.
  • Should there be an option to use textwidth, e.g. with a kwarg?
minor change strings

Most helpful comment

From triage: we can't think of a use case where you would want length.

All 3 comments

I have seen functions like mylpad in a package to workaround this behavior... So yeah, at least providing an option to use textwidth would be great.

Does someone know the usecase for using length instead of textwidth ?

Triage said textwidth sounds best here

From triage: we can't think of a use case where you would want length.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TotalVerb picture TotalVerb  ·  3Comments

m-j-w picture m-j-w  ·  3Comments

ararslan picture ararslan  ·  3Comments

felixrehren picture felixrehren  ·  3Comments

Keno picture Keno  ·  3Comments