File: 0001-Fixed-console-get_width.patch

package info (click to toggle)
configshell 1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 304 kB
  • sloc: python: 3,335; makefile: 18
file content (93 lines) | stat: -rw-r--r-- 3,229 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
From d866b2459dcf480a7202b86085c8d02e8ea5d2d6 Mon Sep 17 00:00:00 2001
From: Jerome Martin <jxm@risingtidesystems.com>
Date: Tue, 1 Nov 2011 11:25:53 +0100
Subject: [PATCH] Fixed console get_width()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Default max width is now 132 cols.
* Now also works with non-terminal device, problem was reported by
  "Pál Gergely" <nightw@niif.hu>
---
 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):
         '''
-- 
1.7.9.1