Repository: Gallopsled/pwntools

Issue:
Importing pwntools breaks carriage return
Normally, a loop like below
```
for i in range(0, 5):
    print(str(i), end="\r")
```
should print each number in the same space. However, when I import pwntools, this behavior breaks and each one is printed on a new line or sequentially.

System is ubuntu 18.04. Latest pwntools version.


Discussion:
As best I can tell, this works for me.  Pwntools makes extensive use of this feature for e.g. `log.progress` so I don't see why it would be broken.

Does it work if you do `python test.py NOTERM`?

```py
from pwn import *

for i in range(0, 5):
    sys.stdout.write(str(i) + '\r')
print('done')
```
```
$ python test.py  | phd
00000000  30 0d 31 0d  32 0d 33 0d  34 0d 64 6f  6e 65 0a     │0·1·│2·3·│4·do│ne·│
0000000f
```
What terminal are you using? Do you also use something like `screen` or `tmux`? What is `$TERM` set to?
I'm using ubuntu 18.04 with i3. $TERM is xterm-256color. 
What terminal emulator, though? If you're not sure, try `pstree -s $$`
Can confirm the carriage return works when ran with `NOTERM`. Otherwise it doesn't. I'm using kali with default terminal. `$TERM = xterm-256color`. terminal emulator `systemd───qterminal───bash───pstree`
> Can confirm the carriage return works when ran with `NOTERM`. Otherwise it doesn't. I'm using kali with default terminal. `$TERM = xterm-256color`. terminal emulator `systemd───qterminal───bash───pstree`

Same issue here. Ubuntu 20.04, tmux, $TERM=xterm-256color. Works when NOTERM is given.

By the time the C function `static PyObject * builtin_print` gets called, the `\r` has already been removed from the buffer. It's just gone, not exactly sure why yet.

I'm pretty confident it has something to do with `pwnlib/term/term.py` (still haven't quite figured out how/why all that works).

Line 320 in `pwnlib/term/term.py`

```python
        elif c == 0x0d:
            x = (CR, None)
            i += 1
```

If you just comment that out then it starts to work again.

```python
from pwn import *
print("hi\rhji", end="")
```

```
00000000  68 69 0d 68  6a 69 0a                               │hi·h│ji·│
00000007
```
However, on the console it prints `hi\x0dhji` (the terminal doesn't react to the \r control character and just echos it).

The blame is on this line:
https://github.com/Gallopsled/pwntools/blob/cde51065bf11c58feb9de4e24f5763f124a41541/pwnlib/term/term.py#L427-L429
There is the `'\r'` commented out in #580, which served as an alternative to #563 and no one protested back then. Try un-commenting this, and let know if the results are satisfactory.
> The blame is on this line:
> https://github.com/Gallopsled/pwntools/blob/cde51065bf11c58feb9de4e24f5763f124a41541/pwnlib/term/term.py#L427-L429
> 
> 
> There is the `'\r'` commented out in #580, which served as an alternative to #563 and no one protested back then. Try un-commenting this, and let know if the results are satisfactory.

Yeah that fixes it. Thanks! Nice to have `\r` back :)
Glad all of this got fixed, ultimately it's some term nonsense that prints hex_escaped non-printable characters to stdout, to make debuggging easier.

Clearly it has its drawbacks, and I have no problem removing \r from the list of things to print the repr() of.

Resolving patch:
diff --git a/pwnlib/term/term.py b/pwnlib/term/term.py
--- a/pwnlib/term/term.py
+++ b/pwnlib/term/term.py
@@ -425,7 +425,7 @@ def render_cell(cell, clear_after = False):
                 put('\x08')
                 col -= 1
         elif t == CR:
-#            put('\r')
+            put('\r')
             col = 0
         elif t == SOH:
             put('\x01')


Test patch: