# Strange characters in windows terminal

**URL:** <https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2022, 10:03am UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355 "2022-10-27T10:03:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 27, 2022, 10:03am UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/1 "2022-10-27T10:03:47Z")

</div>

I have the following code:

```julia
function git_pull()
    result = 0
    try
        msg = readlines(`$git pull --rebase`)
        println(msg)
    catch e
        result = 1
    end
    result
end

```

If I call this function from the repl in vscode I get the following output:

```julia
julia> git_pull()
["Already up to date."]
←[0m0

```

This happens only on one of two Windows 10 computers I have.  
Output on Linux

```julia
julia> git_pull()   
["Current branch main is up to date."]
0

```

Where are these strange characters coming from?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 27, 2022, 10:13am UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/2 "2022-10-27T10:13:42Z")

</div>

Found a solution for Python, but not yet for Julia: [python - How to use the new support for ANSI escape sequences in the Windows 10 console? - Stack Overflow](https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console)

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 27, 2022, 12:01pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/3 "2022-10-27T12:01:25Z")

</div>

Perhaps a filter function that removes all ANSI escape sequences from a string would already help… But how could I implement it?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 27, 2022, 12:55pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/4 "2022-10-27T12:55:53Z")

</div>

StringManipulations.jl has a [remove\_decorations](https://github.com/ronisbr/StringManipulation.jl#remove-the-decorations) function that can remove ANSI escape sequences from a string.

```julia
julia> str = "Test 😅 \e[38;5;231;48;5;243mTest 😅 \e[38;5;201;48;5;243mTest\e[0m";

julia> remove_decorations(str)
"Test 😅 Test 😅 Test"

```

ANSIColoredPrinters.jl also has a [PlainTextPrinter](https://docs.juliahub.com/ANSIColoredPrinters/VW3io/0.0.1/output-formats/#plain_text) 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))`.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 27, 2022, 2:32pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/5 "2022-10-27T14:32:59Z")

</div>

I tried:

```julia
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
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…

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [October 27, 2022, 2:48pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/6 "2022-10-27T14:48:32Z")

</div>

Can you reproduce this outside of VS Code?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 28, 2022, 11:07am UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/7 "2022-10-28T11:07:10Z")

</div>

Could you print out the bytes of the result, like

```julia
line |> codeunits |> println

```

and

```julia
remove_decorations(line) |> codeunits |> println

```

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.

> [@ufechner7](#):
>
> This happens only on one of two Windows 10 computers I have.

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.)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2022, 11:36am UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/8 "2022-10-28T11:36:58Z")

</div>

> [@ufechner7](#):
>
> Found a solution for Python, but not yet for Julia: [python - How to use the new support for ANSI escape sequences in the Windows 10 console? - Stack Overflow](https://stackoverflow.com/questions/36760127/how-to-use-the-new-support-for-ansi-escape-sequences-in-the-windows-10-console)

Can’t you use the [`SetConsoleMode` solution](https://learn.microsoft.com/en-us/windows/console/setconsolemode) from that answer? Something like this in your `startup.jl`:

```julia
if isinteractive()
    let consoleout = ccall(:GetStdHandle, stdcall, Ptr{Cvoid}, (Int32,), -11)
        ccall(:SetConsoleMode, stdcall, Cint, (Ptr{Cvoid}, Int32), consoleout, 0x0001 | 0x0004 | 0x0008)
    end
end

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 28, 2022, 3:40pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/9 "2022-10-28T15:40:20Z")

</div>

I will investigate further on Monday, don’t have access to the laptop that shows this issue right now…

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 31, 2022, 2:43pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/10 "2022-10-31T14:43:26Z")

</div>

> [@pfitzseb](#):
>
> Can you reproduce this outside of VS Code?

Yes, we just tried it using the “windows command prompt”, and the output is the same. So this is not a vscode related issue.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 31, 2022, 2:47pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/11 "2022-10-31T14:47:24Z")

</div>

> [@digital\_carver](#):
>
> 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.)

Well, we are not using any startup.jl files.

I tried:

```julia
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

```

Output:

```julia
git_pull2()
041 06c 072 065 061 064 079 020 075 070 020 074 06f 020 064 061 074 065 02e
←[0m0

```

So the strange characters are not part of the string that I am printing…  
(Still need to check if any control character is in this string…)

But where else do the come from?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [October 31, 2022, 3:08pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/12 "2022-10-31T15:08:20Z")

</div>

git prints the odd characters to stderr.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 31, 2022, 3:30pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/13 "2022-10-31T15:30:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [October 31, 2022, 3:34pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/14 "2022-10-31T15:34:19Z")

</div>

> [@digital\_carver](#):
>
> `(`git pull --ff`, stderr = devnull))`

Would there be the risk that I don’t see error messages any longer if I do that?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 31, 2022, 3:41pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/15 "2022-10-31T15:41:24Z")

</div>

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

```julia
  io = IOBuffer()
  readlines(pipeline(`git pull --ff`, stderr = io)) 
  git_err = remove_decorations(String(take!(io)))
  println(git_err)

```

should work.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 2, 2022, 2:46pm UTC](https://discourse.julialang.org/t/strange-characters-in-windows-terminal/89355/16 "2022-11-02T14:46:33Z")

</div>

All working fine now. Thanks everybody for the good support! 🙂

Final code:

```julia
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

```
