[PATCH] churn: obtain current terminal width if possible

Alexis S. L. Carvalho alexis at cecm.usp.br
Thu Jul 19 12:30:47 UTC 2007


Thus spake Christian Ebert:
> # 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

This should really go into some kind of get_tty_width function so that
it's only called when needed.  And it'd be nice to respect $COLUMNS as
well.

Alexis



More information about the Mercurial-devel mailing list