Repository: xonsh/xonsh

Issue:
Title isn't behaving as expected (prompt)
## xonfig

<details>

```
$ xonfig
+------------------+-----------------+
| xonsh            | 0.9.27          |
| Python           | 3.9.2           |
| PLY              | 3.11            |
| have readline    | True            |
| prompt toolkit   | 3.0.14          |
| shell type       | prompt_toolkit  |
| pygments         | 2.7.4           |
| on posix         | True            |
| on linux         | True            |
| distro           | unknown         |
| 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          | []              |
+------------------+-----------------+
```

</details>

## Expected Behavior

I'd like to use your `_dynamically_collapsed_pwd` function with slight modification for my terminal title, however, it seems to behave a bit different vs. the prompt function, where I managed to make it work

```python
from xonsh.prompt.cwd import _dynamically_collapsed_pwd
def rprompt():
  cwd = _dynamically_collapsed_pwd().removeprefix('/mnt/')
  out = cwd + '|'
  return out
```
I'd expect this function to use your `cwd`, but without the `/mnt/` prefix

## Current Behavior
This works fine for a prompt:
`$RIGHT_PROMPT = rprompt`
Right prompt is using your function without the prefix

However, this fails for a title
`$TITLE = rprompt`
Title displays `<function rprompt a...>`, so the function isn't expanded

## Steps to Reproduce

Define the function above in xonsh and assign it to the `$TITLE` env var

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:
Hi @eugenesvk ! Here we see that TITLE is not checked for being callable:

https://github.com/xonsh/xonsh/blob/b5b88b18f1e4ad2177944de4576505a247e2d9d1/xonsh/base_shell.py#L531-L536

So PR is welcome!
As a workaround you can try something like:
```python
def _myprompt_with_title():
    # any custom logic
    __xonsh__.env['TITLE'] = 'mytitle'
    return '@ '
$PROMPT = _myprompt_with_title
del _myprompt_with_title
```
Thank you, that's a good workaround!
(Not sure I can help with a PR, but will see if I can understand it enough to fix it)
This seems a bit trickier, the `$TITLE` environment variable seems to be handled in a special way:
```
> $WHATEVER=lambda: "hello"
> $WHATEVER
<function __main__.<lambda>>
> $WHATEVER()
'hello'
> $TITLE=lambda: "hello"
> $TITLE
'<function <lambda> at 0x7f452a3fb550>'  # <= already a string
> $TITLE()
TypeError: 'str' object is not callable
```

Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -1337,13 +1337,15 @@ class PromptSetting(Xettings):
         False,
         "Whether or not to suppress branch timeout warning messages when getting {gitstatus} PROMPT_FIELD.",
     )
-    TITLE = Var.with_default(
+    TITLE = Var(
+        is_string_or_callable,
+        ensure_string,
+        ensure_string,
         DEFAULT_TITLE,
         "The title text for the window in which xonsh is running. Formatted "
         "in the same manner as ``$PROMPT``, see 'Customizing the Prompt' "
         "http://xon.sh/tutorial.html#customizing-the-prompt.",
         doc_default="``xonsh.environ.DEFAULT_TITLE``",
-        type_str="str",
     )
     UPDATE_PROMPT_ON_KEYPRESS = Var.with_default(
         False,


Test patch: