posh-git-sh icon indicating copy to clipboard operation
posh-git-sh copied to clipboard

Shell option `failglob` results in errors during prompt rendering

Open neodon opened this issue 1 year ago • 1 comments

I usually enable the failglob shell option by default in bash via shopt -s failglob. This results in errors being displayed when combined with using __posh_git_ps1() in my PROMPT_COMMAND.

Here is an example:

~/p/dev [main ≡ +0 ~3 -0 !] $ shopt -s failglob
bash: no match: \[\e[m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;96m\]
bash: no match: \[\033[0;92m\]
bash: no match: \[\033[0;91m\]
bash: no match: \[\033[0;93m\]
bash: no match: \[\033[0;32m\]
bash: no match: \[\033[0;32m\]
bash: no match: \[\033[0;31m\]
bash: no match: \[\033[0;91m\]
bash: no match: \[\033[0;31m\]
bash: no match: \[\033[0;96m\]
bash: no match: \[\e[0m\]
~/p/dev [main ≡ +0 ~3 -0 !] $ shopt -u failglob
~/p/dev [main ≡ +0 ~3 -0 !] $

I was able to resolve the issue by changing these lines in the __posh_color() function:

From:

echo \\["$1"\\]

To:

(shopt -u failglob && echo \\["$1"\\])

However, I'm not sure if this might break other use cases. I could look into making sure it is robust.

Let me know if you're interested in a pull request with the fix + tests.

neodon avatar Dec 23 '23 17:12 neodon

Thanks for your report! I was able to reproduce this.

__posh_color emits the ANSI escape sequence passed to echo. We can use literals for the \[\033[ and \] pair. See #63.

lyze avatar Jan 09 '24 06:01 lyze