From: Kefu Chai Date: Tue, 23 May 2017 03:28:10 +0000 (+0800) Subject: pybind/ceph_daemon.py: move _gettermsize() into Termsize X-Git-Tag: v12.1.0~10^2~10^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=84f9c1251bb285a875895c352cab0fe067c1536b;p=ceph.git pybind/ceph_daemon.py: move _gettermsize() into Termsize 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 --- diff --git a/src/pybind/ceph_daemon.py b/src/pybind/ceph_daemon.py index 48bd4e0d7ce..15bf1f43149 100755 --- a/src/pybind/ceph_daemon.py +++ b/src/pybind/ceph_daemon.py @@ -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):