Suppose, for example, that PS1
has the value shown below:
'\w (\[\e[32m\]${__git_ps1_branch_name}\[\e[0m\] \[\e[31m\]*\[\e[31m\]%\[\e[0m\]>)\$ '
...where __git_ps1_branch_name
is some string, like master
or feature123
.
Q: I am looking for a way to compute the on-screen width (in number of characters) of the prompt that bash
will generate from a PS1
like the one shown above.
Recent versions of bash
expand the expression ${PS1@P}
to the string obtained by replacing the elements of the prompt specification above by their final display values. (Thus \w
will be replaced by the path to the current directory, for example, and ${__git_ps1_branch_name}
will be replaced by the string contained in the __git_ps1_branch_name
variable, etc.) This however is not enough to solve the problem.
More specifically, if we set:
EXPANDED_PS1="${PS1@P}"
...the expression ${#EXPANDED_PS1}
(i.e. the number of characters in $EXPANDED_PS1
) yields a number that is significantly greater than the on-screen width of the prompt that bash would generate from $PS1
. The excess comes from the escape sequences that confer color to the displayed prompt.
In any case, I am also interested in solutions that will work with versions of bash that do not support @P
.