[PATCH] obtain terminal_width via curses

Matthew Hannigan mlh at zip.com.au
Thu Apr 27 02:48:31 BST 2006


On Tue, Apr 25, 2006 at 07:46:06PM -0700, Robey Pointer wrote:
> 
> === modified file 'bzrlib/osutils.py'
> --- bzrlib/osutils.py
> +++ bzrlib/osutils.py
> @@ -617,4 +617,12 @@
>      try:
>          return int(os.environ['COLUMNS'])
>      except (IndexError, KeyError, ValueError):
> -        return 80
> +        pass
> +    try:
> +        # this only works on posix
> +        import fcntl, termios, struct
> +        height, width = struct.unpack('hh', fcntl.ioctl(sys.stdout,  

I think you should ioctl sys.stdin, not stdout.
A typical usage might be to preview on the screen, then
run again to direct into a file.  You don't want the latter
to fail.  Speaking of which, you should catch
	IOError: [Errno 25] Inappropriate ioctl for device
as well.

> termios.TIOCGWINSZ, "    "))
> +        return width
> +    except ImportError:
> +        pass
> +    return 80

It's an open question whether COLUMNS should override
ioctl or not.  On all modern terminals and glass ttys
they should always be the same, but I guess if COLUMNS
is set explicitly it should take precedence. OTOH it
(used to be?) a common bug that resizing a window didn't
update COLUMNS for some *xterms/shell combos.

OTOH I've run out of hands.


Matt





More information about the bazaar mailing list