Show `root and @#` in prompt if user is superuser. (#5409)

### Before

```xsh
sudo xonsh --no-rc
# <user>@host ~ #
```


### After

```xsh
sudo xonsh --no-rc
# root@host ~ @#
```

It's in sync with:

```xsh
docker run -it ubuntu bash
# root@ce01c2856819:/#
echo $USER
# 
# root@ce01c2856819:/#
```
## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andy Kipp 2024-05-22 15:27:03 +02:00 committed by GitHub
parent aa69ce868a
commit c5f7b74ca8
Failed to generate hash of commit
2 changed files with 26 additions and 2 deletions

23
news/prompt_superuser.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* Show ``root`` and ``@#`` in prompt if user is superuser.
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -338,9 +338,10 @@ class PromptFields(tp.MutableMapping[str, "FieldType"]):
self.update(
dict(
user=xp.os_environ.get(
"USERNAME" if xp.ON_WINDOWS else "USER", "<user>"
"USERNAME" if xp.ON_WINDOWS else "USER",
"root" if xt.is_superuser() else "<user>",
),
prompt_end="#" if xt.is_superuser() else "@",
prompt_end="@#" if xt.is_superuser() else "@",
hostname=socket.gethostname().split(".", 1)[0],
cwd=_dynamically_collapsed_pwd,
cwd_dir=lambda: os.path.join(os.path.dirname(_replace_home_cwd()), ""),