[Git][NTPsec/ntpsec][master] pyntpq now expands the remote-name column when running in a wide window.

Eric S. Raymond gitlab at mg.gitlab.com
Wed Oct 26 11:27:40 UTC 2016


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


Commits:
30783f91 by Eric S. Raymond at 2016-10-26T07:24:06-04:00
pyntpq now expands the remote-name column when running in a wide window.

- - - - -


2 changed files:

- ntpq/pyntpq
- pylib/util.py


Changes:

=====================================
ntpq/pyntpq
=====================================
--- a/ntpq/pyntpq
+++ b/ntpq/pyntpq
@@ -150,6 +150,25 @@ class Ntpq(cmd.Cmd):
         self.debug = 0
         self.pktversion = NTP_OLDVERSION + 1
         self.uservars = collections.OrderedDict()
+        # By default, the peer spreadsheet layout is designed so lines just
+        # fit in 80 characters. This tells us how much extra horizontal space
+        # we have available on a wider terminal emulator
+        self.horizontal_slack = termsize()[1] - 80
+        # Peer spreadsheet column widths
+        self.namewidth = 15 + self.horizontal_slack
+        self.refidwidth = 15
+        # Compute peer spreadsheet headers 
+        self.__remote = "     remote    ".ljust(self.namewidth)
+        self.__common = "st t when poll reach   delay   offset  "
+        self.__opeerheader = self.__remote + \
+                             "       local      ".ljust(self.refidwidth) + \
+                             self.__common + "  disp\n"
+        self.__peerheader  = self.__remote + \
+                             "       refid      ".ljust(self.refidwidth) + \
+                             self.__common + "jitter\n"
+        self.__apeerheader = self.__remote + \
+                             "   refid   assid  ".ljust(self.refidwidth) + \
+                             self.__common + "jitter\n"
 
     def emptyline(self):
         "Called when an empty line is entered in response to the prompt."
@@ -169,11 +188,6 @@ usage: help [ command ]
 
     # Unexposed helper tables and functions begin here
 
-    __common = "st t when poll reach   delay   offset  "
-    __opeerheader = "     remote           local      " + __common + "  disp\n"
-    __peerheader  = "     remote           refid      " + __common + "jitter\n"
-    __apeerheader = "     remote       refid   assid  " + __common + "jitter\n"
-
     @staticmethod
     def high_truncate(hostname, maxlen):
         "Truncate on the left using leading _ to indicate 'more'."
@@ -397,34 +411,32 @@ usage: help [ command ]
         def is_ipv6(addr): return ":" in addr and "." not in addr
         if socket.AF_UNSPEC == af or af == (socket.AF_INET6 if is_ipv6(srcaddr) else socket.AF_INET):
             # Source host or clockname
-            namewidth = 15
-            addrwidth = 15
             if displayname != None and self.showhostnames:
                 clock_name = displayname
             elif srchost != None:
                 clock_name = srchost
             else:
                 clock_name = canonicalize_dns(srcaddr)
-            if interpreter.wideremote and len(clock_name) > namewidth:
+            if interpreter.wideremote and len(clock_name) > self.namewidth:
                 self.say("%c%s\n" % (c, clock_name))
-                sys.stdout(" " * (namewidth + 2))
+                sys.stdout(" " * (self.namewidth + 2))
             else:
-                self.say("%c%-15.15s " % \
-                                 (c, clock_name[:namewidth]))
+                self.say("%c%-*.*s " % \
+                                 (c, self.namewidth, self.namewidth, clock_name[:self.namewidth]))
             # Destination address, assoc ID or refid.
             assocwidth = 7 if "assid" in header else 0
             if "." not in dstadr_refid:
                 dstadr_refid = "." + dstadr_refid + "."
-            if assocwidth and len(dstadr_refid) >= addrwidth - assocwidth:
+            if assocwidth and len(dstadr_refid) >= self.refidwidth - assocwidth:
                 visible = "..."
             else:
                 visible = dstadr_refid
             self.say(visible)
             if "assid" in header:
-                self.say(" " * (addrwidth - len(visible) - assocwidth + 1))
+                self.say(" " * (self.refidwidth - len(visible) - assocwidth + 1))
                 self.say("%-6d" % (associd))
             else:
-                self.say(" " * (addrwidth - len(visible)))
+                self.say(" " * (self.refidwidth - len(visible)))
             # The rest of the story
             last_sync = variables.get("rec") or variables.get("reftime")
             jd = estjitter if have_jitter else estdisp
@@ -465,7 +477,7 @@ usage: help [ command ]
             self.say("%-*.*s " % \
                              (maxhostlen, maxhostlen+1, "server"))
         self.say(header)
-        self.say(("=" * (maxhostlen + 78)) + "\n")
+        self.say(("=" * (maxhostlen + 78 + self.horizontal_slack)) + "\n")
         for peer in self.peers:
             if not showall and \
 		    not (CTL_PEER_STATVAL(peer.status)
@@ -1205,7 +1217,7 @@ usage: pstats assocID
 
     def do_peers(self, line):
         "obtain and print a list of the server's peers [IP version]"
-        self.__dopeers(showall=False, af=line, header=Ntpq.__peerheader)
+        self.__dopeers(showall=False, af=line, header=self.__peerheader)
 
     def help_peers(self):
         self.say("""\
@@ -1215,7 +1227,7 @@ usage: peers [ -4|-6 ]
 
     def do_apeers(self, line):
         "obtain and print a list of the server's peers and their assocIDs [IP version]"
-        self.__dopeers(showall=False, af=line, header=Ntpq.__apeerheader)
+        self.__dopeers(showall=False, af=line, header=self.__apeerheader)
 
     def help_apeers(self):
         self.say("""\
@@ -1225,7 +1237,7 @@ usage: apeers [ -4|-6 ]
 
     def do_lpeers(self, line):
         "obtain and print a list of all peers and clients [IP version]"
-        self.__dopeers(showall=True, af=line, header=Ntpq.__peerheader)
+        self.__dopeers(showall=True, af=line, header=self.__peerheader)
 
     def help_lpeers(self):
         self.say("""\
@@ -1235,7 +1247,7 @@ usage: lpeers [ -4|-6 ]
 
     def do_opeers(self, line):
         "print peer list the old way, with dstadr shown rather than refid [IP version]"
-        self.__dopeers(showall=False, af=line, header=Ntpq.__opeerheader)
+        self.__dopeers(showall=False, af=line, header=self.__opeerheader)
 
     def help_opeers(self):
         self.say("""\
@@ -1245,7 +1257,7 @@ usage: opeers [ -4|-6 ]
 
     def do_lopeers(self, line):
         "obtain and print a list of all peers and clients showing dstadr [IP version]"
-        self.__dopeers(showall=True, af=line, header=Ntpq.__opeerheader)
+        self.__dopeers(showall=True, af=line, header=self.__opeerheader)
 
     def help_lopeers(self):
         self.say("""\


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -4,6 +4,7 @@ from __future__ import print_function
 
 import socket
 import sys
+import subprocess
 
 def canonicalize_dns(hostname):
     try:
@@ -18,4 +19,16 @@ def canonicalize_dns(hostname):
         return canonname.lower()
     return name[0].lower()
 
+def termsize():
+    "Return the current terminal size."
+    # Should work under Linux and Solaris at least.
+    # Alternatives at http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python
+    import shlex, subprocess, re
+    output = subprocess.check_output(shlex.split('/bin/stty -a'))
+    m = re.search('rows\D+(\d+); columns\D+(\d+);', output)
+    if m:
+        return int(m.group(1)), int(m.group(2))
+    else:
+        return (24, 80)
+
 # end



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/30783f91a83bd44977546e1580e1b29cb6a7eda2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161026/18688986/attachment.html>


More information about the vc mailing list