Repository: xonsh/xonsh
Pull request: fix: web config tool including \r in prompt
<!--- Thanks for opening a PR on xonsh! Please include a news entry with your PR
to help keep our changelog up to date! There are instructions available here:
https://xon.sh/devguide.html#changelog -->

<!--- If there is specific issue / feature request that this PR is addressing,
please link to the corresponding issue by using the `#issuenumber` syntax.
Thanks again! -->

hi guys, this is my first commit. please let me know if i've done anything wrong :)

previously using the xonsh web config tool to set a prompt with a newline added "^M" at the end of the prompt. this fix strips all \r characters in the config file and strips all \r characters in the prompt page's post request.

closes #4960 

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**


Problem statement:
Web config tool includes \r in prompt
When using the web config (xonfig web), newlines are interpreted as "\r\n". On macOS (and Linux, I assume) this adds a "^M" to the prompt, before the newline. 
## xonfig
```
+------------------+--------------------------------+
| xonsh            | 0.13.3                         |
| Python           | 3.10.7                         |
| PLY              | 3.11                           |
| have readline    | True                           |
| prompt toolkit   | 3.0.31                         |
| shell type       | prompt_toolkit                 |
| history backend  | json                           |
| pygments         | 2.13.0                         |
| on posix         | True                           |
| on linux         | False                          |
| on darwin        | True                           |
| on windows       | False                          |
| on cygwin        | False                          |
| on msys2         | False                          |
| is superuser     | False                          |
| default encoding | utf-8                          |
| xonsh encoding   | utf-8                          |
| encoding errors  | surrogateescape                |
| xontrib          | []                             |
+------------------+--------------------------------+
```

## Expected Behavior
I would expect the "\r" to be stripped from the text field before the prompt is set. 

## Current Behavior
I was able to confirm that the "\r" is being added to the prompt variable by checking my .xonshrc after using the web config. Removing this value removes the "^M" from my prompt.

## Steps to Reproduce
* Run xonfig web from your shell.
* Select a multi-line prompt, and set your .xonshrc
* Exit and restart xonsh. You'll see a "^M" before the new line.

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**

Hints from issue discussion:
i'm happy to try working on this, could someone assign me the issue?
All yours, @peipacut -- we historically haven't relied on issue assignment, you should feel free to pick up any open issues and just ping in the issue that you're going to work on them to avoid duplicate effort.

Commit message: [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Patch to review:
diff --git a/xonsh/webconfig/file_writes.py b/xonsh/webconfig/file_writes.py
index 1e4b4aec21..4388b4371d 100644
--- a/xonsh/webconfig/file_writes.py
+++ b/xonsh/webconfig/file_writes.py
@@ -1,5 +1,6 @@
 """functions to update rc files"""
 import os
+import re
 import typing as tp
 
 RENDERERS: tp.List[tp.Callable] = []
@@ -39,7 +40,7 @@ def config_to_xonsh(
     for func in RENDERERS:
         lines.extend(func(config))
     lines.append(suffix)
-    return "\n".join(lines)
+    return re.sub(r"\\r", "", "\n".join(lines))
 
 
 def insert_into_xonshrc(
diff --git a/xonsh/webconfig/routes.py b/xonsh/webconfig/routes.py
index 6568a27a3e..f9b8e36522 100644
--- a/xonsh/webconfig/routes.py
+++ b/xonsh/webconfig/routes.py
@@ -1,5 +1,6 @@
 import cgi
 import inspect
+import re
 import sys
 from typing import TYPE_CHECKING
 
@@ -239,10 +240,13 @@ def get(self):
 
     def post(self, data: "cgi.FieldStorage"):
         if data:
-            prompt = data.getvalue(self.var_name)
+            prompt = data.getvalue(self.var_name).replace("\r", "")
             self.env[self.var_name] = prompt
             self.update_rc(prompt=prompt)
 
+    def ee(self, str):
+        return re.sub(r"\\r", "", str)
+
 
 class XontribsPage(Routes):
     path = "/xontribs"


Code review comments:
diff_hunk: @@ -239,10 +240,13 @@ def get(self):
 
     def post(self, data: "cgi.FieldStorage"):
         if data:
-            prompt = data.getvalue(self.var_name)
+            prompt = data.getvalue(self.var_name).replace("\r", "")
             self.env[self.var_name] = prompt
             self.update_rc(prompt=prompt)
 
+    def ee(self, str):
+        return re.sub(r"\\r", "", str)
+
path: xonsh/webconfig/routes.py
text: @user1:
Is this being used or is this left over from debugging?

@author:
removed