git » msnlib » commit c66ce3c

Update the window size when redrawing the prompt

author Alberto Bertogli
2011-11-16 21:05:57 UTC
committer Alberto Bertogli
2011-11-16 21:05:57 UTC
parent 7a2c90b3364e29b22e6bfbf2dc519257a44be2c9

Update the window size when redrawing the prompt

This change makes the client update the window size when redrawing the
prompt, so we can gracefully handle window resizes.

We could also do this by handling SIGWINCH, but that has the undersirable
side-effect of interrupting a lot of system calls, and handling that would
make the code even more complicated.

Thanks to Michael P. Soulier for reporting and the bug and helping with
the tests.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

msn +14 -6

diff --git a/msn b/msn
index 2f50535..9a07a76 100755
--- a/msn
+++ b/msn
@@ -576,14 +576,21 @@ except:
 # doing this without using a C module. it's based on 'struct winsize',
 # but as we only use the first 4 bytes, we don't ask for more; then we
 # unpack the two shorts into (lenght, width)
-try:
-	import struct
-	winsize = fcntl.ioctl(stdinfd, termios.TIOCGWINSZ, ' ' * 10)
-	winsize = struct.unpack('hh', winsize[:4])
-except:
-	winsize = (24, 80)
+winsize = (24, 80)
 screenwidth = winsize[1]
 
+def update_winsize(signo = -1, frame = None):
+	global winsize, screenwidth
+	try:
+		import struct
+		winsize = fcntl.ioctl(stdinfd, termios.TIOCGWINSZ, ' ' * 10)
+		winsize = struct.unpack('hh', winsize[:4])
+	except:
+		screenwidth = winsize[1]
+
+update_winsize()
+
+
 # input buffer, where all the characters written by the user are stored in
 inbuf = ''
 
@@ -881,6 +888,7 @@ def redraw_cli():
 	"""Redraws the current prompt line, including user input; it first
 	clears the line, either automatically or up to 'lenght' chars."""
 	global inbuf, screenwidth
+	update_winsize()
 	clear_line()
 	print_prompt()
 	lenght = screenwidth - 7	# we subsctract the prompt lenght + 1