[PATCH] churn: obtain current terminal width if possible

Christian Ebert blacktrash at gmx.net
Thu Jul 19 03:59:47 CDT 2007


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1184835530 -7200
# Node ID ca06b2d20e45e91a7598f47d1dacd0958c2b4db4
# Parent  382520bacc17886e0e2fe7fb798a0a5218cf9db1
churn: obtain current terminal width if possible

diff --git a/contrib/churn.py b/contrib/churn.py
--- a/contrib/churn.py
+++ b/contrib/churn.py
@@ -14,6 +14,25 @@ import sys
 import sys
 from mercurial.i18n import gettext as _
 from mercurial import hg, mdiff, cmdutil, ui, util, templater, node
+
+tty_width = 80
+try:
+    import termios, fcntl, os, struct
+    buf = 'abcd'
+    for dev in (sys.stdout, sys.stdin):
+        try:
+            if buf != 'abcd':
+                break
+            fd = dev.fileno()
+            if not os.isatty(fd):
+                continue
+            buf = fcntl.ioctl(fd, termios.TIOCGWINSZ, buf)
+        except ValueError:
+            pass
+    if buf != 'abcd':
+        tty_width = struct.unpack('hh', buf)[1]
+except ImportError:
+    pass
 
 def __gather(ui, repo, node1, node2):
     def dirtywork(f, mmap1, mmap2):
@@ -159,8 +178,8 @@ def churn(ui, repo, **opts):
 
     maximum = ordered[0][1]
 
-    ui.note("Assuming 80 character terminal\n")
-    width = 80 - 1
+    width = tty_width - 1
+    ui.note(_("assuming %i character terminal\n") % tty_width)
 
     for i in ordered:
         person = i[0]


More information about the Mercurial-devel mailing list