ANSIColoredPrinters.jl also has a PlainTextPrinter that removes ANSI escape codes, though slightly less convenient to use.
StringManipulations accepts a normal string and returns a string, so it just requires println(msg) to become println(remove_decorations(msg)).
function git_pull(throw=false)
result = 0
if throw
msg = readlines(`git pull --rebase`)
for line in msg
println(remove_decorations(line))
end
else
try
msg = readlines(`git pull --rebase`)
for line in msg
println(remove_decorations(line))
end
catch e
result = 1
end
end
result
end
But the result is the same:
julia> git_pull()
["Already up to date."]
←[0m0
Perhaps this is not an ANSI escape sequence?
What else can I try?
The problem is that this brakes the terminal menu that I am using…
and show the result? I’m fairly certain by now that the escape sequence is not actually part of the string, but is an artifact of printing. This would help make sure that’s true.
Do you mean that the output in the other Windows computer, in VS Code REPL, doesn’t print these extra characters? If so, could you compare the startup.jl files in these two computers? (eg. redefining println to use printstyled could cause this.)
hex(n) = string(n, base=16, pad=3)
function git_pull2()
result = 0
try
msg = readlines(`git pull --ff`)
for line in msg
clean_line = remove_decorations(line)
for char in clean_line
print(hex(Int64(char)), " ")
end
println()
end
catch e
result = 1
end
result
end
Ah, that’s pretty odd, to print the reset sequence even if it hasn’t actually printed anything else to stderr. So would readlines(pipeline(`git pull --ff`, stderr = devnull)) get rid of the issue?
Yeah, any error messages from git would be siphoned off into the void. You can instead change devnull to be a file instead, or if you just want to print it, something like
All working fine now. Thanks everybody for the good support!
Final code:
function exec(command)
io = IOBuffer()
msg = readlines(pipeline(command, stderr = io))
err = remove_decorations(String(take!(io)))
msg, err
end
function git_pull(throw=false)
function pull()
result = 0
msg, git_err = exec(`git pull --ff`)
for line in msg
println(remove_decorations(line))
end
if length(git_err) > 0
result = 1
println(git_err)
end
result
end
result = 0
if throw
result = pull()
else
try
result = pull()
catch e
result = 1
end
end
result
end