[Git][NTPsec/ntpsec][master] A safer version of termsize, and referencing it.

Eric S. Raymond gitlab at mg.gitlab.com
Thu Dec 1 21:33:06 UTC 2016


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
b973b1fd by Eric S. Raymond at 2016-12-01T16:32:28-05:00
A safer version of termsize, and referencing it.

- - - - -


2 changed files:

- ntpq/ntpq
- pylib/util.py


Changes:

=====================================
ntpq/ntpq
=====================================
--- a/ntpq/ntpq
+++ b/ntpq/ntpq
@@ -341,9 +341,10 @@ usage: help [ command ]
         if not self.__dogetassoc():
             return
         if self.showhostnames:
-            termwidth = ntp.util.termsize()[1]
+            termwidth = ntp.util.termsize().width
         else:
-            termwidth = None	# Default width 
+            termwidth = None	# Default width
+        print("Width: %d" % termwidth)
         report = ntp.util.PeerSummary(mode,
                                       self.pktversion,
                                       self.showhostnames,
@@ -496,7 +497,7 @@ usage: help [ command ]
                         lastcount = 0
                     else:
                         lastcount += 1
-                if lastcount + len(item) > ntp.util.termsize()[1] - 2:
+                if lastcount + len(item) > ntp.util.termsize().width - 2:
                     text = text[:-1] + "\n"
                 text += item
             text = text[:-2] + "\n"


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -9,6 +9,7 @@ import time
 import os
 import re
 import shutil
+import collections
 
 import ntp.ntpc
 import ntp.version
@@ -63,21 +64,23 @@ def canonicalize_dns(hostname, family=socket.AF_UNSPEC):
         return canonicalized.lower() + portsuffix
     return name[0].lower() + portsuffix
 
+TermSize = collections.namedtuple("TermSize", ["width", "height"])
+
 def termsize():
     "Return the current terminal size."
     # Alternatives at http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
     if not os.isatty(1):
-        return (80, 24)
+        size = (80, 24)
     try:
-        return shutil.get_terminal_size((80, 24))
+        size = shutil.get_terminal_size((80, 24))
     except AttributeError:
-        pass
-    # OK, Python version < 3.3, cope
-    import fcntl, termios, struct
-    h, w, hp, wp = struct.unpack('HHHH',
-        fcntl.ioctl(2, termios.TIOCGWINSZ,
-        struct.pack('HHHH', 0, 0, 0, 0)))
-    return w, h
+        # OK, Python version < 3.3, cope
+        import fcntl, termios, struct
+        h, w, hp, wp = struct.unpack('HHHH',
+            fcntl.ioctl(2, termios.TIOCGWINSZ,
+            struct.pack('HHHH', 0, 0, 0, 0)))
+        size = (w, h)
+    return TermSize(w, h)
 
 class PeerSummary:
     "Reusable report generator for peer statistics"



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/b973b1fdb9d9d15b9cd7115e834653f5a961e0be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161201/21b7be87/attachment.html>


More information about the vc mailing list