Repository: xonsh/xonsh
Pull request: fix extra newlines in ptk2
Weird that this is needed, but it does fix the problem.  Closes #2910

Problem statement:
An extra newline character gets printed in Python mode
## xonfig
```
+------------------+----------------------+
| xonsh            | 0.8.2                |
| Git SHA          | 6159f449             |
| Commit Date      | Sep 21 02:39:43 2018 |
| Python           | 3.7.1                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 2.0.4                |
| shell type       | prompt_toolkit2      |
| pygments         | 2.2.0                |
| on posix         | True                 |
| on linux         | True                 |
| distro           | arch                 |
| 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      |
+------------------+----------------------+
```
## Expected Behavior
Prompt should be rendered immediately after the last command's output.

## Current Behavior
PTK2 shell adds an extra newline to the output of commands executed in Python mode.

## Steps to Reproduce
```
laloch@uriel ~ $ echo @(1+1)
2
laloch@uriel ~ $ 1+1
2

laloch@uriel ~ $
```

Hints from issue discussion:
If I understood him right, @scopatz suggested that the extra newline gets there from
https://github.com/xonsh/xonsh/blob/dca4a477150b55a7011078daba9da8bb0e95ceae/xonsh/base_shell.py#L382, but no, unfortunately that's not the case.
The extra EOL seems to be printed from within `exec()` call in codecache.py:run_compiled_code(). If I replace the call at [line 67](https://github.com/xonsh/xonsh/blob/f409a020387c6e73a58fbac5191675f0d519591e/xonsh/codecache.py#L67) by
```
    print("<")
    func(code, glb, loc)
    print(">")
```
I get
from `xonsh --shell-type=rl --no-rc`:
```
$ 1+1
<
2
>
$
```
from `xonsh --shell-type=ptk2 --no-rc`:
```
$ 1+1
<
2

>
$
```

Commit message: fix extra newlines in ptk2

Patch to review:
diff --git a/news/extranl.rst b/news/extranl.rst
new file mode 100644
index 0000000000..73f7f346d5
--- /dev/null
+++ b/news/extranl.rst
@@ -0,0 +1,24 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* <news item>
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* Fixed issues where the prompt-toolkit v2 shell would print an extra newline
+  after Python evalutions in interactive mode.
+
+**Security:**
+
+* <news item>
diff --git a/xonsh/main.py b/xonsh/main.py
index ae9e857d0c..854005e2f2 100644
--- a/xonsh/main.py
+++ b/xonsh/main.py
@@ -257,7 +257,8 @@ def _pprint_displayhook(value):
         printed_val = repr(value)
     if HAS_PYGMENTS and env.get("COLOR_RESULTS"):
         tokens = list(pygments.lex(printed_val, lexer=pyghooks.XonshLexer()))
-        print_color(tokens)
+        end = "" if env.get("SHELL_TYPE") == "prompt_toolkit2" else "\n"
+        print_color(tokens, end=end)
     else:
         print(printed_val)  # black & white case
     builtins._ = value


Code review comments:
diff_hunk: @@ -0,0 +1,24 @@
+**Added:**
+
+* <news item>
+
+**Changed:**
+
+* <news item>
+
+**Deprecated:**
+
+* <news item>
+
+**Removed:**
+
+* <news item>
+
+**Fixed:**
+
+* Fixed issues where the prompt-toolkit v2 shell would print an extra newline
+  after Python evalutions in interactive mode.
path: news/extranl.rst
text: @user1:
There's a typo in 'evaluations'.
```suggestion
  after Python evaluations in interactive mode.
```

@author:
Thanks!