Repository: pwndbg/pwndbg

Issue:
Typesetting seems to be wrong
![image](https://cloud.githubusercontent.com/assets/7897423/24843862/42e004bc-1dd9-11e7-8447-b8cf87ed6fe1.png)



Discussion:
Can't reproduce, I expect your terminal is broken.

Try opening a new terminal window.  Does this still happen?

Try removing the `source pwndbg/gdbinit.py` line from your `~/.gdbinit`.  Does this still happen?
@zachriggle Happens for me only if pwndbg is initialized in both gnome-terminal and terminator.
FWIW this also happens to me with gnome-terminal (including opening a new terminal).  Goes away if you don't source pwndbg in your .gdbinit
Hmmmmmmmmmm
Does this only occur when attempting to tab-complete?  Does it only occur for tab-completing command names?
@zachriggle I think so: https://asciinema.org/a/652yj24od38z01aot65zehi8k
I am not sure what else should I check
This is caused because of `set width 0` in https://github.com/pwndbg/pwndbg/blob/master/pwndbg/__init__.py#L128

Here is a recording:
https://asciinema.org/a/8dvzsu6keibmua694y2scqpk8
I can confirm this also happens for me and removing `set width 0` seems to fix it.
Hmm, that kind of makes sense that the tab-completion doesn't "wrap" into rows when the width is `0` or `unlimited`.

It might be better for us to do something that grabs the width via `tcgetattr` or similar.
just as a reminder, if we do something like that then we will need a hook for resizing row width of the terminal
Yep! It's not too hard to set up a signal handler for `SIGWINCH`.
everything seems to be right after I grab the latest version
please re-open this is still an issue and happening
I could work on a patch with SIGWINCH and proper `set with` so feel free to assign me this issue.

Resolving patch:
diff --git a/pwndbg/__init__.py b/pwndbg/__init__.py
--- a/pwndbg/__init__.py
+++ b/pwndbg/__init__.py
@@ -5,6 +5,8 @@
 from __future__ import print_function
 from __future__ import unicode_literals
 
+import signal
+
 import gdb
 
 import pwndbg.android
@@ -67,6 +69,7 @@
 import pwndbg.regs
 import pwndbg.stack
 import pwndbg.typeinfo
+import pwndbg.ui
 import pwndbg.version
 import pwndbg.vmmap
 import pwndbg.wrappers
@@ -131,13 +134,13 @@
 set backtrace past-main on
 set step-mode on
 set print pretty on
-set width 0
+set width %i
 set print elements 15
 handle SIGALRM nostop print nopass
 handle SIGBUS  stop   print nopass
 handle SIGPIPE nostop print nopass
 handle SIGSEGV stop   print nopass
-""".strip() % prompt
+""".strip() % (prompt, pwndbg.ui.get_window_size()[1])
 
 for line in pre_commands.strip().splitlines():
     gdb.execute(line)
@@ -147,3 +150,7 @@
     gdb.execute("set disassembly-flavor intel")
 except gdb.error:
     pass
+
+
+# handle resize event to align width and completion
+signal.signal(signal.SIGWINCH, lambda signum, frame: gdb.execute("set width %i" % pwndbg.ui.get_window_size()[1]))
diff --git a/pwndbg/ui.py b/pwndbg/ui.py
--- a/pwndbg/ui.py
+++ b/pwndbg/ui.py
@@ -9,6 +9,7 @@
 from __future__ import unicode_literals
 
 import fcntl
+import os
 import struct
 import sys
 import termios
@@ -22,13 +23,21 @@
 
 def banner(title):
     title = title.upper()
-    try:
-        _height, width = struct.unpack('hh', fcntl.ioctl(sys.stdin.fileno(), termios.TIOCGWINSZ, '1234'))
-    except:
-        width = 80
+    _height, width = get_window_size()
     width -= 2
     return C.banner(("[{:%s^%ss}]" % (config.banner_separator, width)).format(title))
 
 def addrsz(address):
     address = int(address) & pwndbg.arch.ptrmask
     return "%{}x".format(2*pwndbg.arch.ptrsize) % address
+
+def get_window_size():
+    fallback = (int(os.environ.get('LINES', 20)), int(os.environ.get('COLUMNS', 80)))
+    if not sys.stdin.isatty:
+        return fallback
+    try:
+        # get terminal size and force ret buffer len of 4 bytes for safe unpacking by passing equally long arg
+        rows, cols = struct.unpack('hh', fcntl.ioctl(sys.stdin.fileno(), termios.TIOCGWINSZ, '1234'))
+    except:
+        rows, cols = fallback
+    return rows, cols


Test patch: