Weird output with REPL escape codes

I’m trying to work with the raw output from a Julia process but I don’t understand even a simple version of it.

I tried julia --banner=no | tee julia.txt, then typed 1+2 and enter. The file julia.txt then looks like this (I replaced escape characters with \e here:

\e[?2004h
\e[0Kjulia> 
\e[7C
\e[7C1+2
\e[0Kjulia> 
\e[7C1+2
\e[10C
\e[?2004l3


\e[0K
\e[0Kjulia> 
\e[7C
\e[7C\e[?2004h
\e[0Kjulia> 
\e[7C
\e[7C
\e[?2004l

However, the visual output is

julia> 1+2
3

julia>

I don’t get how the ANSI escape codes lead to that result. \e[0K means “delete from cursor to end of line” and \e[7C means "move cursor forward 7 steps. The output has julia> two times, but in the raw output it’s 4 times. However, the cursor only ever moves forward, and only ever deletes to the end of the line. How does it work out to the final result then?

One more peculiarity, when I do cat julia.txt I get the correct output. However, when I do echo 'paste the content of the file here' I get:

julia> 

       1+2
julia> 
       1+2

3



julia> 


julia> 

And this is the output I would expect if I trace the ansi escape code commands in there. Where’s the problem in my logic?

Those are ansi color escape sequences. Try julia --banner=no --color=no

They are not color sequences, they are cursor movements. The color sequences end in m, Julia already disables color by default if you use it like this.

Didn’t you also replace cariage returns with new lines?
What I get looks like this (with escape characters replaced by \e and carriage returns replaced by \r):

\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C1+2\r\e[0Kjulia> \r\e[7C1+2\r\e[10C
\e[?2004l3

\r\e[0K\r\e[0Kjulia> \r\e[7C\r\e[7C\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C
\e[?2004l

This makes more sense, because now the CRs make the cursor move backward to the beginning of the line. The echo output also agrees with cat:

shell$ echo -e '\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C1+2\r\e[0Kjulia> \r\e[7C1+2\r\e[10C
> \e[?2004l3
> 
> \r\e[0K\r\e[0Kjulia> \r\e[7C\r\e[7C\e[?2004h\r\e[0Kjulia> \r\e[7C\r\e[7C
> \e[?2004l'
julia> 1+2
3

julia> 

However, I don’t know why the prompt is repeated twice (but erased right after it’s printed for the first time). An other peculiarity is how the bracketed paste mode sequences (\e?2004h and \e?2004l) encompass both prompts in the first line, but only one in the second line.

Ha, it turns out I didn’t but VSCode did. I had the file open to copy stuff from it, and VSCode replaced every \r with \r\n. Thanks for the hint!