diff options
author | Jerome Martin <jxm@risingtidesystems.com> | 2011-11-01 11:25:53 +0100 |
---|---|---|
committer | Jerome Martin <jxm@risingtidesystems.com> | 2011-11-01 11:27:52 +0100 |
commit | d866b2459dcf480a7202b86085c8d02e8ea5d2d6 (patch) | |
tree | 1cdfc611e19c9250a0961cb3ccb37ff973e1045b /configshell | |
parent | 8f4781f7690f54f0b56771e97152e849fbff4986 (diff) | |
download | configshell-fb-d866b2459dcf480a7202b86085c8d02e8ea5d2d6.tar.gz |
Fixed console get_width()
* Default max width is now 132 cols.
* Now also works with non-terminal device, problem was reported by
"Pál Gergely" <nightw@niif.hu>
Diffstat (limited to 'configshell')
-rw-r--r-- | configshell/console.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/configshell/console.py b/configshell/console.py index 52558dd..d463879 100644 --- a/configshell/console.py +++ b/configshell/console.py @@ -18,12 +18,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. import re import sys import tty -import fcntl import prefs import struct -import termios import textwrap +from fcntl import ioctl import epydoc.markup.epytext +from termios import TIOCGWINSZ, TCSADRAIN, tcsetattr, tcgetattr class Console(object): ''' @@ -31,7 +31,7 @@ class Console(object): most notably an epytext-to-console text renderer using ANSI escape sequences. It uses the Borg pattern to share state between instances. ''' - _max_width = 80 + _max_width = 132 _escape = '\033[' _ansi_format = _escape + '%dm%s' _ansi_reset = _escape + '0m' @@ -75,7 +75,7 @@ class Console(object): @param reply_terminator: The expected end-of-reply marker. @type reply_terminator: str ''' - attributes = termios.tcgetattr(self._stdin) + attributes = tcgetattr(self._stdin) tty.setraw(self._stdin) try: self.raw_write(self._escape + sequence) @@ -84,7 +84,7 @@ class Console(object): while reply[-len(reply_terminator):] != reply_terminator: reply += self._stdin.read(1) finally: - termios.tcsetattr(self._stdin, termios.TCSADRAIN, attributes) + tcsetattr(self._stdin, TCSADRAIN, attributes) if reply_terminator is not None: reply = reply[:-len(reply_terminator)] reply = reply.replace(self._escape, '').split(';') @@ -92,16 +92,20 @@ class Console(object): def get_width(self): ''' - Returns the console width + Returns the console width, or maximum width if we are not a terminal + device. ''' - width = struct.unpack("HHHH", - fcntl.ioctl(self._stdout.fileno(), - termios.TIOCGWINSZ, - struct.pack("HHHH", 0, 0, 0, 0)))[1] - if width > self._max_width: - return self._max_width + try: + winsize = struct.pack("HHHH", 0, 0, 0, 0) + winsize = ioctl(self._stdout.fileno(), TIOCGWINSZ, winsize) + width = struct.unpack("HHHH", winsize)[1] + except IOError: + width = self._max_width else: - return width + if width > self._max_width: + width = self._max_width + + return width def get_cursor_xy(self): ''' |