Search code examples
bashpowershellwindows-terminal

How do I use an ANSI sequence to signify progress in Windows Terminal using Bash?


PowerShell has a Write-Progress cmdlet that would update the Terminal icon to indicate progress.

I would like to do the same in Bash (and NOT exec powershell)

I think it is doable from the link

PowerShell displays certain information using ANSI escape sequences

So I was wondering what those ANSI sequences are.

enter image description here


Solution

  • The escape sequence is ESC ] 9 ; 4 ; st ; pr ST.

    pr is the current progress. st can be one of the following:

    Value Purpose
    0 Clear the progress indicator
    1 Set the progress to pr %
    2 Set the progress to pr % in error state
    3 Set the progress indicator to indeterminate state
    4 Set the progress to pr % in warning state

    For example,

    # Show indeterminate indicator for a couple seconds.
    echo -en "\e]9;4;3;0\e\\"
    sleep 3
    
    # Run normally up to 40 %.
    for i in {10..40..10}
    do
        echo -en "\e]9;4;1;$i\e\\"
        sleep 1
    done
    
    # Continue in error state for a couple seconds.
    for i in {50..70..10}
    do
        echo -en "\e]9;4;2;$i\e\\"
        sleep 1
    done
    
    # Continue in warning state for a couple seconds.
    for i in {80..100..10}
    do
        echo -en "\e]9;4;4;$i\e\\"
        sleep 1
    done
    
    # Clear progress indicator.
    echo -en "\e]9;4;0;0\e\\"