[Git][NTPsec/ntpsec][master] Try for a better-behaved termsize() function.
Eric S. Raymond
gitlab at mg.gitlab.com
Wed Nov 30 22:41:46 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
91a795a8 by Eric S. Raymond at 2016-11-30T17:41:36-05:00
Try for a better-behaved termsize() function.
- - - - -
1 changed file:
- pylib/util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -6,7 +6,9 @@ from __future__ import print_function
import socket
import sys
import time
+import os
import re
+import shutil
import ntp.ntpc
import ntp.version
@@ -63,18 +65,19 @@ def canonicalize_dns(hostname, family=socket.AF_UNSPEC):
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
+ if not os.isatty(1):
+ return (80, 24)
try:
- output = subprocess.check_output(shlex.split('/bin/stty -a 2>/dev/null'), universal_newlines=True)
- except (OSError, subprocess.CalledProcessError, AttributeError):
- return (24, 80)
- for pattern in ('rows\D+(\d+); columns\D+(\d+);', '\s+(\d+)\D+rows;\s+(\d+)\D+columns;'):
- m = re.search(pattern, output)
- if m:
- return int(m.group(1)), int(m.group(2))
- return (24, 80)
+ return 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
class PeerSummary:
"Reusable report generator for peer statistics"
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/91a795a8c00e1270cf73dddd6d770687586e15d1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161130/aedf26dd/attachment.html>
More information about the vc
mailing list