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:
textwidth
instead of length
? Perhaps too much of a breaking change.textwidth
, e.g. with a kwarg?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
.
Most helpful comment
From triage: we can't think of a use case where you would want
length
.