prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  make promp.env.env_name venv --prompt aware (set in pyvenv.cfg)
  ## xonfig
  
  <details>
  
  ```
  > xonfig
  <xonsh-code>:1:0 - xonfig
  <xonsh-code>:1:0 + ![xonfig]
  +------------------+----------------------+
  | xonsh            | 0.11.0               |
  | Git SHA          | adfa60ea             |
  | Commit Date      | Feb 11 14:53:00 2022 |
  | Python           | 3.9.7                |
  | PLY              | 3.11                 |
  | have readline    | True                 |
  | prompt toolkit   | None                 |
  | shell type       | readline             |
  | history backend  | json                 |
  | pygments         | 2.11.2               |
  | on posix         | True                 |
  | on linux         | True                 |
  | distro           | unknown              |
  | on wsl           | False                |
  | on darwin        | False                |
  | on windows       | False                |
  | on cygwin        | False                |
  | on msys2         | False                |
  | is superuser     | False                |
  | default encoding | utf-8                |
  | xonsh encoding   | utf-8                |
  | encoding errors  | surrogateescape      |
  | on jupyter       | False                |
  | jupyter kernel   | None                 |
  | xontrib 1        | coreutils            |
  | xontrib 2        | vox                  |
  | xontrib 3        | voxapi               |
  | RC file 1        | /home/ob/.xonshrc    |
  +------------------+----------------------+
  ```
  
  </details>
  
  ## Expected Behavior
  
  When activating a venv via `vox activate` the name should be set to the value of `prompt` key in `pyvenv.cfg` if present (file and key) - see https://docs.python.org/3/library/venv.html.
  
  ## Current Behavior
  
  The prompt is always set to name of venv directory independent of prompt settings.
  
  ## Steps to Reproduce
  
  ```shell
  $ python -m venv --prompt "MY SPECIAL PROMPT" .venv
  $ cat .venv/pyvenv.cfg | grep prompt
  prompt = 'MY SPECIAL PROMPT'
  vox activate ./.venv
  ```
  
  new prompt looks like this:
  `[17:58:10] (.venv)  ob@ob1 ~/oss/xonsh dropthedot|✓`
  
  but should look like this:
  `[17:58:10] (MY SPECIAL PROMPT)  ob@ob1 ~/oss/xonsh dropthedot|✓`
  
  ## Also add `--prompt` to `vox new`?
  If this is done, maybe a good idea would be to also add `--prompt` to `vox new` to make this possible on creation (also in the interest of symmetry), but it seems like the common pattern for many xonsh users is to have virtualenvs in `~/.virtualenv` with the venv folder name being the same as the project, therefore automatically having a meaningful prompt, so this does not seem to be an urgently wanted feature for most.
  
  I want to prepare a PR for the "passive" part of the functionality, but could also have a stab at adding `--prompt`  to `vox new` at a later date. My main itch would be to respect `prompt` from already existing venvs first.
  
  ## For community
  ⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `xonsh/prompt/env.py`
  Content:
  ```
  1 """Prompt formatter for virtualenv and others"""
  2 
  3 import os
  4 
  5 from xonsh.built_ins import XSH
  6 
  7 
  8 def find_env_name():
  9     """Finds the current environment name from $VIRTUAL_ENV or
  10     $CONDA_DEFAULT_ENV if that is set.
  11     """
  12     env_path = XSH.env.get("VIRTUAL_ENV", "")
  13     if env_path:
  14         env_name = os.path.basename(env_path)
  15     else:
  16         env_name = XSH.env.get("CONDA_DEFAULT_ENV", "")
  17     return env_name
  18 
  19 
  20 def env_name():
  21     """Returns the current env_name if it non-empty, surrounded by the
  22     ``{env_prefix}`` and ``{env_postfix}`` fields.
  23     """
  24     env_name = find_env_name()
  25     if XSH.env.get("VIRTUAL_ENV_DISABLE_PROMPT") or not env_name:
  26         # env name prompt printing disabled, or no environment; just return
  27         return
  28 
  29     venv_prompt = XSH.env.get("VIRTUAL_ENV_PROMPT")
  30     if venv_prompt is not None:
  31         return venv_prompt
  32     else:
  33         pf = XSH.shell.prompt_formatter
  34         pre = pf._get_field_value("env_prefix")
  35         post = pf._get_field_value("env_postfix")
  36         return pre + env_name + post
  37 
  38 
  39 def vte_new_tab_cwd():
  40     """This prints an escape sequence that tells VTE terminals the hostname
  41     and pwd. This should not be needed in most cases, but sometimes is for
  42     certain Linux terminals that do not read the PWD from the environment
  43     on startup. Note that this does not return a string, it simply prints
  44     and flushes the escape sequence to stdout directly.
  45     """
  46     env = XSH.env
  47     t = "\033]7;file://{}{}\007"
  48     s = t.format(env.get("HOSTNAME"), env.get("PWD"))
  49     print(s, end="", flush=True)
  50
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/xonsh/prompt/env.py b/xonsh/prompt/env.py
  --- a/xonsh/prompt/env.py
  +++ b/xonsh/prompt/env.py
  @@ -1,42 +1,70 @@
   """Prompt formatter for virtualenv and others"""
  -
  -import os
  +import functools
  +import re
  +from pathlib import Path
  +from typing import Optional
   
   from xonsh.built_ins import XSH
   
   
  -def find_env_name():
  -    """Finds the current environment name from $VIRTUAL_ENV or
  -    $CONDA_DEFAULT_ENV if that is set.
  +def find_env_name() -> Optional[str]:
  +    """Find current environment name from available sources.
  +
  +    If ``$VIRTUAL_ENV`` is set, it is determined from the prompt setting in
  +    ``<venv>/pyvenv.cfg`` or from the folder name of the environment.
  +
  +    Otherwise - if it is set - from ``$CONDA_DEFAULT_ENV``.
  +    """
  +    virtual_env = XSH.env.get("VIRTUAL_ENV")
  +    if virtual_env:
  +        name = _determine_env_name(virtual_env)
  +        if name:
  +            return name
  +    conda_default_env = XSH.env.get("CONDA_DEFAULT_ENV")
  +    if conda_default_env:
  +        return conda_default_env
  +
  +
  +def env_name() -> str:
  +    """Build env_name based on different sources. Respect order of precedence.
  +
  +    Name from VIRTUAL_ENV_PROMPT will be used as-is.
  +    Names from other sources are surrounded with ``{env_prefix}`` and
  +    ``{env_postfix}`` fields.
       """
  -    env_path = XSH.env.get("VIRTUAL_ENV", "")
  -    if env_path:
  -        env_name = os.path.basename(env_path)
  -    else:
  -        env_name = XSH.env.get("CONDA_DEFAULT_ENV", "")
  -    return env_name
  +    if XSH.env.get("VIRTUAL_ENV_DISABLE_PROMPT"):
  +        return ""
  +    virtual_env_prompt = XSH.env.get("VIRTUAL_ENV_PROMPT")
  +    if virtual_env_prompt:
  +        return virtual_env_prompt
  +    found_envname = find_env_name()
  +    return _surround_env_name(found_envname) if found_envname else ""
  +
   
  +@functools.lru_cache(maxsize=5)
  +def _determine_env_name(virtual_env: str) -> str:
  +    """Use prompt setting from pyvenv.cfg or basename of virtual_env.
   
  -def env_name():
  -    """Returns the current env_name if it non-empty, surrounded by the
  -    ``{env_prefix}`` and ``{env_postfix}`` fields.
  +    Tries to be resilient to subtle changes in whitespace and quoting in the
  +    configuration file format as it adheres to no clear standard.
       """
  -    env_name = find_env_name()
  -    if XSH.env.get("VIRTUAL_ENV_DISABLE_PROMPT") or not env_name:
  -        # env name prompt printing disabled, or no environment; just return
  -        return
  -
  -    venv_prompt = XSH.env.get("VIRTUAL_ENV_PROMPT")
  -    if venv_prompt is not None:
  -        return venv_prompt
  -    else:
  -        pf = XSH.shell.prompt_formatter
  -        pre = pf._get_field_value("env_prefix")
  -        post = pf._get_field_value("env_postfix")
  -        return pre + env_name + post
  -
  -
  -def vte_new_tab_cwd():
  +    venv_path = Path(virtual_env)
  +    pyvenv_cfg = venv_path / "pyvenv.cfg"
  +    if pyvenv_cfg.is_file():
  +        match = re.search(r"prompt\s*=\s*(.*)", pyvenv_cfg.read_text())
  +        if match:
  +            return match.group(1).strip().lstrip("'\"").rstrip("'\"")
  +    return venv_path.name
  +
  +
  +def _surround_env_name(name: str) -> str:
  +    pf = XSH.shell.prompt_formatter
  +    pre = pf._get_field_value("env_prefix")
  +    post = pf._get_field_value("env_postfix")
  +    return f"{pre}{name}{post}"
  +
  +
  +def vte_new_tab_cwd() -> None:
       """This prints an escape sequence that tells VTE terminals the hostname
       and pwd. This should not be needed in most cases, but sometimes is for
       certain Linux terminals that do not read the PWD from the environment
  
verification_info: {"golden_diff": "diff --git a/xonsh/prompt/env.py b/xonsh/prompt/env.py\n--- a/xonsh/prompt/env.py\n+++ b/xonsh/prompt/env.py\n@@ -1,42 +1,70 @@\n \"\"\"Prompt formatter for virtualenv and others\"\"\"\n-\n-import os\n+import functools\n+import re\n+from pathlib import Path\n+from typing import Optional\n \n from xonsh.built_ins import XSH\n \n \n-def find_env_name():\n-    \"\"\"Finds the current environment name from $VIRTUAL_ENV or\n-    $CONDA_DEFAULT_ENV if that is set.\n+def find_env_name() -> Optional[str]:\n+    \"\"\"Find current environment name from available sources.\n+\n+    If ``$VIRTUAL_ENV`` is set, it is determined from the prompt setting in\n+    ``<venv>/pyvenv.cfg`` or from the folder name of the environment.\n+\n+    Otherwise - if it is set - from ``$CONDA_DEFAULT_ENV``.\n+    \"\"\"\n+    virtual_env = XSH.env.get(\"VIRTUAL_ENV\")\n+    if virtual_env:\n+        name = _determine_env_name(virtual_env)\n+        if name:\n+            return name\n+    conda_default_env = XSH.env.get(\"CONDA_DEFAULT_ENV\")\n+    if conda_default_env:\n+        return conda_default_env\n+\n+\n+def env_name() -> str:\n+    \"\"\"Build env_name based on different sources. Respect order of precedence.\n+\n+    Name from VIRTUAL_ENV_PROMPT will be used as-is.\n+    Names from other sources are surrounded with ``{env_prefix}`` and\n+    ``{env_postfix}`` fields.\n     \"\"\"\n-    env_path = XSH.env.get(\"VIRTUAL_ENV\", \"\")\n-    if env_path:\n-        env_name = os.path.basename(env_path)\n-    else:\n-        env_name = XSH.env.get(\"CONDA_DEFAULT_ENV\", \"\")\n-    return env_name\n+    if XSH.env.get(\"VIRTUAL_ENV_DISABLE_PROMPT\"):\n+        return \"\"\n+    virtual_env_prompt = XSH.env.get(\"VIRTUAL_ENV_PROMPT\")\n+    if virtual_env_prompt:\n+        return virtual_env_prompt\n+    found_envname = find_env_name()\n+    return _surround_env_name(found_envname) if found_envname else \"\"\n+\n \n+@functools.lru_cache(maxsize=5)\n+def _determine_env_name(virtual_env: str) -> str:\n+    \"\"\"Use prompt setting from pyvenv.cfg or basename of virtual_env.\n \n-def env_name():\n-    \"\"\"Returns the current env_name if it non-empty, surrounded by the\n-    ``{env_prefix}`` and ``{env_postfix}`` fields.\n+    Tries to be resilient to subtle changes in whitespace and quoting in the\n+    configuration file format as it adheres to no clear standard.\n     \"\"\"\n-    env_name = find_env_name()\n-    if XSH.env.get(\"VIRTUAL_ENV_DISABLE_PROMPT\") or not env_name:\n-        # env name prompt printing disabled, or no environment; just return\n-        return\n-\n-    venv_prompt = XSH.env.get(\"VIRTUAL_ENV_PROMPT\")\n-    if venv_prompt is not None:\n-        return venv_prompt\n-    else:\n-        pf = XSH.shell.prompt_formatter\n-        pre = pf._get_field_value(\"env_prefix\")\n-        post = pf._get_field_value(\"env_postfix\")\n-        return pre + env_name + post\n-\n-\n-def vte_new_tab_cwd():\n+    venv_path = Path(virtual_env)\n+    pyvenv_cfg = venv_path / \"pyvenv.cfg\"\n+    if pyvenv_cfg.is_file():\n+        match = re.search(r\"prompt\\s*=\\s*(.*)\", pyvenv_cfg.read_text())\n+        if match:\n+            return match.group(1).strip().lstrip(\"'\\\"\").rstrip(\"'\\\"\")\n+    return venv_path.name\n+\n+\n+def _surround_env_name(name: str) -> str:\n+    pf = XSH.shell.prompt_formatter\n+    pre = pf._get_field_value(\"env_prefix\")\n+    post = pf._get_field_value(\"env_postfix\")\n+    return f\"{pre}{name}{post}\"\n+\n+\n+def vte_new_tab_cwd() -> None:\n     \"\"\"This prints an escape sequence that tells VTE terminals the hostname\n     and pwd. This should not be needed in most cases, but sometimes is for\n     certain Linux terminals that do not read the PWD from the environment\n", "issue": "make promp.env.env_name venv --prompt aware (set in pyvenv.cfg)\n## xonfig\r\n\r\n<details>\r\n\r\n```\r\n> xonfig\r\n<xonsh-code>:1:0 - xonfig\r\n<xonsh-code>:1:0 + ![xonfig]\r\n+------------------+----------------------+\r\n| xonsh            | 0.11.0               |\r\n| Git SHA          | adfa60ea             |\r\n| Commit Date      | Feb 11 14:53:00 2022 |\r\n| Python           | 3.9.7                |\r\n| PLY              | 3.11                 |\r\n| have readline    | True                 |\r\n| prompt toolkit   | None                 |\r\n| shell type       | readline             |\r\n| history backend  | json                 |\r\n| pygments         | 2.11.2               |\r\n| on posix         | True                 |\r\n| on linux         | True                 |\r\n| distro           | unknown              |\r\n| on wsl           | False                |\r\n| on darwin        | False                |\r\n| on windows       | False                |\r\n| on cygwin        | False                |\r\n| on msys2         | False                |\r\n| is superuser     | False                |\r\n| default encoding | utf-8                |\r\n| xonsh encoding   | utf-8                |\r\n| encoding errors  | surrogateescape      |\r\n| on jupyter       | False                |\r\n| jupyter kernel   | None                 |\r\n| xontrib 1        | coreutils            |\r\n| xontrib 2        | vox                  |\r\n| xontrib 3        | voxapi               |\r\n| RC file 1        | /home/ob/.xonshrc    |\r\n+------------------+----------------------+\r\n```\r\n\r\n</details>\r\n\r\n## Expected Behavior\r\n\r\nWhen activating a venv via `vox activate` the name should be set to the value of `prompt` key in `pyvenv.cfg` if present (file and key) - see https://docs.python.org/3/library/venv.html.\r\n\r\n## Current Behavior\r\n\r\nThe prompt is always set to name of venv directory independent of prompt settings.\r\n\r\n## Steps to Reproduce\r\n\r\n```shell\r\n$ python -m venv --prompt \"MY SPECIAL PROMPT\" .venv\r\n$ cat .venv/pyvenv.cfg | grep prompt\r\nprompt = 'MY SPECIAL PROMPT'\r\nvox activate ./.venv\r\n```\r\n\r\nnew prompt looks like this:\r\n`[17:58:10] (.venv)  ob@ob1 ~/oss/xonsh dropthedot|\u2713`\r\n\r\nbut should look like this:\r\n`[17:58:10] (MY SPECIAL PROMPT)  ob@ob1 ~/oss/xonsh dropthedot|\u2713`\r\n\r\n## Also add `--prompt` to `vox new`?\r\nIf this is done, maybe a good idea would be to also add `--prompt` to `vox new` to make this possible on creation (also in the interest of symmetry), but it seems like the common pattern for many xonsh users is to have virtualenvs in `~/.virtualenv` with the venv folder name being the same as the project, therefore automatically having a meaningful prompt, so this does not seem to be an urgently wanted feature for most.\r\n\r\nI want to prepare a PR for the \"passive\" part of the functionality, but could also have a stab at adding `--prompt`  to `vox new` at a later date. My main itch would be to respect `prompt` from already existing venvs first.\r\n\r\n## For community\r\n\u2b07\ufe0f  **Please click the \ud83d\udc4d reaction instead of leaving a `+1` or \ud83d\udc4d  comment**\r\n\n", "before_files": [{"content": "\"\"\"Prompt formatter for virtualenv and others\"\"\"\n\nimport os\n\nfrom xonsh.built_ins import XSH\n\n\ndef find_env_name():\n    \"\"\"Finds the current environment name from $VIRTUAL_ENV or\n    $CONDA_DEFAULT_ENV if that is set.\n    \"\"\"\n    env_path = XSH.env.get(\"VIRTUAL_ENV\", \"\")\n    if env_path:\n        env_name = os.path.basename(env_path)\n    else:\n        env_name = XSH.env.get(\"CONDA_DEFAULT_ENV\", \"\")\n    return env_name\n\n\ndef env_name():\n    \"\"\"Returns the current env_name if it non-empty, surrounded by the\n    ``{env_prefix}`` and ``{env_postfix}`` fields.\n    \"\"\"\n    env_name = find_env_name()\n    if XSH.env.get(\"VIRTUAL_ENV_DISABLE_PROMPT\") or not env_name:\n        # env name prompt printing disabled, or no environment; just return\n        return\n\n    venv_prompt = XSH.env.get(\"VIRTUAL_ENV_PROMPT\")\n    if venv_prompt is not None:\n        return venv_prompt\n    else:\n        pf = XSH.shell.prompt_formatter\n        pre = pf._get_field_value(\"env_prefix\")\n        post = pf._get_field_value(\"env_postfix\")\n        return pre + env_name + post\n\n\ndef vte_new_tab_cwd():\n    \"\"\"This prints an escape sequence that tells VTE terminals the hostname\n    and pwd. This should not be needed in most cases, but sometimes is for\n    certain Linux terminals that do not read the PWD from the environment\n    on startup. Note that this does not return a string, it simply prints\n    and flushes the escape sequence to stdout directly.\n    \"\"\"\n    env = XSH.env\n    t = \"\\033]7;file://{}{}\\007\"\n    s = t.format(env.get(\"HOSTNAME\"), env.get(\"PWD\"))\n    print(s, end=\"\", flush=True)\n", "path": "xonsh/prompt/env.py"}], "after_files": [{"content": "\"\"\"Prompt formatter for virtualenv and others\"\"\"\nimport functools\nimport re\nfrom pathlib import Path\nfrom typing import Optional\n\nfrom xonsh.built_ins import XSH\n\n\ndef find_env_name() -> Optional[str]:\n    \"\"\"Find current environment name from available sources.\n\n    If ``$VIRTUAL_ENV`` is set, it is determined from the prompt setting in\n    ``<venv>/pyvenv.cfg`` or from the folder name of the environment.\n\n    Otherwise - if it is set - from ``$CONDA_DEFAULT_ENV``.\n    \"\"\"\n    virtual_env = XSH.env.get(\"VIRTUAL_ENV\")\n    if virtual_env:\n        name = _determine_env_name(virtual_env)\n        if name:\n            return name\n    conda_default_env = XSH.env.get(\"CONDA_DEFAULT_ENV\")\n    if conda_default_env:\n        return conda_default_env\n\n\ndef env_name() -> str:\n    \"\"\"Build env_name based on different sources. Respect order of precedence.\n\n    Name from VIRTUAL_ENV_PROMPT will be used as-is.\n    Names from other sources are surrounded with ``{env_prefix}`` and\n    ``{env_postfix}`` fields.\n    \"\"\"\n    if XSH.env.get(\"VIRTUAL_ENV_DISABLE_PROMPT\"):\n        return \"\"\n    virtual_env_prompt = XSH.env.get(\"VIRTUAL_ENV_PROMPT\")\n    if virtual_env_prompt:\n        return virtual_env_prompt\n    found_envname = find_env_name()\n    return _surround_env_name(found_envname) if found_envname else \"\"\n\n\n@functools.lru_cache(maxsize=5)\ndef _determine_env_name(virtual_env: str) -> str:\n    \"\"\"Use prompt setting from pyvenv.cfg or basename of virtual_env.\n\n    Tries to be resilient to subtle changes in whitespace and quoting in the\n    configuration file format as it adheres to no clear standard.\n    \"\"\"\n    venv_path = Path(virtual_env)\n    pyvenv_cfg = venv_path / \"pyvenv.cfg\"\n    if pyvenv_cfg.is_file():\n        match = re.search(r\"prompt\\s*=\\s*(.*)\", pyvenv_cfg.read_text())\n        if match:\n            return match.group(1).strip().lstrip(\"'\\\"\").rstrip(\"'\\\"\")\n    return venv_path.name\n\n\ndef _surround_env_name(name: str) -> str:\n    pf = XSH.shell.prompt_formatter\n    pre = pf._get_field_value(\"env_prefix\")\n    post = pf._get_field_value(\"env_postfix\")\n    return f\"{pre}{name}{post}\"\n\n\ndef vte_new_tab_cwd() -> None:\n    \"\"\"This prints an escape sequence that tells VTE terminals the hostname\n    and pwd. This should not be needed in most cases, but sometimes is for\n    certain Linux terminals that do not read the PWD from the environment\n    on startup. Note that this does not return a string, it simply prints\n    and flushes the escape sequence to stdout directly.\n    \"\"\"\n    env = XSH.env\n    t = \"\\033]7;file://{}{}\\007\"\n    s = t.format(env.get(\"HOSTNAME\"), env.get(\"PWD\"))\n    print(s, end=\"\", flush=True)\n", "path": "xonsh/prompt/env.py"}]}