I’m trying to spice up my Julia REPL prompt with ANSI color codes (not just one single color, but multiple colors in the same line). However, it messes up the cursor alignment, because the code in REPL.jl also counts the ANSI control sequences when it computes the length of the prompt. AFAICT, the “culprit” is Base.textwidth:
The \e is already ignored, since it’s invisible, and this could arguably be considered a bug, to not ignore what follows and is interpreted for it, and then get 10. Not too important since (assume a similar feature for underlining):
This is a standard library as of Julia 1.11. A version compatible with Julia 1.0 through to 1.10 has also been registered in General — allowing StyledStrings to be used anywhere without compromising compatibility.
Well, StyledString.jl might be backported to earlier Julia versions, but the support is limited. First and foremost, the REPL prompt doesn’t expect AnnotatedStrings, and it throws a typeasset.
TypeError: in typeassert, expected String, got a value of type StyledStrings.AnnotatedStrings.AnnotatedString{String}
Stacktrace:
[1] write_prompt(terminal::IO, s::Union{AbstractString, Function}, color::Bool)
@ REPL.LineEdit ~/.local/share/mise/installs/julia/1.10.5/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:1525
(On a related note, in Julia 1.10, concatenating StyledStrings seems to be a PITA, regular join and * return plain Strings. The documentation says otherwise. I also can’t get it to load my faces.toml file on startup. Odd.)
julia> using StyledStrings
julia> styled"{my_custom_face:test test}"
"test test" # this is not visibly styled
julia> StyledStrings.load_customisations!(force=true)
julia> styled"{my_custom_face:test test}"
"test test" # this is visibly styled
This happens both with 1.10.5 and 1.11.0-rc4. Using --startup-file=no doesn’t help, either, so its not my local setup. But we are digressing, perhaps this could be continued in an issue reported to the StyledStrings.jl repo.
I tried this on Julia v1.11-rc4. Sadly, it seems that this issue, i.e., that the REPL prompt doesn’t accept AnnotatedStrings, holds for v1.11 as well, which makes such strings unusable in the prompt:
Unhandled Task ERROR: TypeError: in typeassert, expected String, got a value of type Base.AnnotatedString{String}
Stacktrace:
[1] write_prompt(terminal::IO, s::Union{AbstractString, Function}, color::Bool)
@ REPL.LineEdit ~/.local/share/mise/installs/julia/1.11.0-rc4/share/julia/stdlib/v1.11/REPL/src/LineEdit.jl:1618
It seems that the type spec on line 1618 is too strong:
promptstr = prompt_string(s)::String
Too bad. Can I hope that this gets fixed before the release of v1.11?
The fix I mentioned earlier will have to come in 1.11.1, since StyledStrings wasn’t bumped between rc4 and 1.11.0, unfortunately. It’s been suggested to me I should be more willing to merge my own PRs about AnnotatedStrings/StyledStrings, but it feels dodgy to me .