fix: escape curly braces for the prompt cwd (#4511)

If a directory was named for example '{RED}', that would get
interpreted as a color string and the prompt would not show the
direcotry name and would color it instead. Using dir names like
'{{foo}}' or simply '{' would break the prompt outright.

There was not much documentation on the prompt formating, but it seems
that escaping a curly by doubling it makes the prompt display all
curlies correctly.

fixes #4381
This commit is contained in:
Kaarel Pärtel 2021-11-17 20:39:28 +02:00 committed by GitHub
parent 7331d8aee5
commit 8da06b4446
Failed to generate hash of commit
3 changed files with 41 additions and 1 deletions

23
news/pwd-curly-escape.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* Curly braces { } in directory names are now escaped in the prompt
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

16
tests/prompt/test_cwd.py Normal file
View file

@ -0,0 +1,16 @@
from xonsh.prompt.cwd import _replace_home_cwd
from xonsh.built_ins import XSH
def test_cwd_escapes_curly_brackets_with_more_curly_brackets():
XSH.env["PWD"] = "{foo}"
assert _replace_home_cwd() == "{{foo}}"
XSH.env["PWD"] = "{{foo}}"
assert _replace_home_cwd() == "{{{{foo}}}}"
XSH.env["PWD"] = "{"
assert _replace_home_cwd() == "{{"
XSH.env["PWD"] = "}}"
assert _replace_home_cwd() == "}}}}"

View file

@ -27,7 +27,8 @@ def _replace_home(x):
def _replace_home_cwd():
return _replace_home(XSH.env["PWD"])
pwd = XSH.env["PWD"].replace("{", "{{").replace("}", "}}")
return _replace_home(pwd)
def _collapsed_pwd():