]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/ceph_daemon.py: move _gettermsize() into Termsize
authorKefu Chai <kchai@redhat.com>
Tue, 23 May 2017 03:28:10 +0000 (11:28 +0800)
committerKefu Chai <kchai@redhat.com>
Tue, 23 May 2017 03:55:01 +0000 (11:55 +0800)
as the latter is the only consumer of _gettermsize(). and a little bit
refactor to improve the readability and be more pep8 compliant.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/ceph_daemon.py

index 48bd4e0d7cef66681aadbf44ecbca7d20b8af3d6..15bf1f43149098ab10b6126b279dbfc0f860101a 100755 (executable)
@@ -87,36 +87,37 @@ def admin_socket(asok_path, cmd, format=''):
     return ret
 
 
-def _gettermsize():
-    try:
-        rows, cols = struct.unpack('hhhh', ioctl(0, TIOCGWINSZ, 8*'\x00'))[0:2]
-    except IOError:
-        return 25, 80
-
-    return rows,cols
-
-
 class Termsize(object):
-
+    DEFAULT_SIZE = (25, 80)
     def __init__(self):
-        self.rows, self.cols = _gettermsize()
+        self.rows, self.cols = self._gettermsize()
         self.changed = False
 
+    def _gettermsize(self):
+        try:
+            fd = sys.stdin.fileno()
+            sz = struct.pack('hhhh', 0, 0, 0, 0)
+            rows, cols = struct.unpack('hhhh', ioctl(fd, TIOCGWINSZ, sz))[:2]
+            return rows, cols
+        except IOError:
+            return self.DEFAULT_SIZE
+
     def update(self):
-        rows, cols = _gettermsize()
-        self.changed = self.changed or (
-            (self.rows != rows) or (self.cols != cols)
-        )
+        rows, cols = self._gettermsize()
+        if not self.changed:
+            self.changed = (self.raw,self.col) != (rows, cols)
         self.rows, self.cols = rows, cols
 
     def reset_changed(self):
         self.changed = False
 
     def __str__(self):
-        return '%s(%dx%d, changed %s)' % (self.__class__, self.rows, self.cols, self.changed)
+        return '%s(%dx%d, changed %s)' % (self.__class__,
+                                          self.rows, self.cols, self.changed)
 
     def __repr__(self):
-        return 'Termsize(%d,%d,%s)' % (self.__class__, self.rows, self.cols, self.changed)
+        return 'Termsize(%d,%d,%s)' % (self.__class__,
+                                       self.rows, self.cols, self.changed)
 
 
 class DaemonWatcher(object):