From 2b4d73526bfd6d87b26aa950652b04d3d0bb1d65 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Fri, 19 Jun 2015 13:37:14 +0200 Subject: [PATCH] attempt to set ncols from terminal environs --- tqdm/_tqdm.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tqdm/_tqdm.py b/tqdm/_tqdm.py index e3da1a7d..e892bdb3 100644 --- a/tqdm/_tqdm.py +++ b/tqdm/_tqdm.py @@ -175,6 +175,26 @@ def tqdm(iterable, desc=None, total=None, leave=False, file=sys.stderr, except (TypeError, AttributeError): total = None + if (ncols is None) and (file in (sys.stderr, sys.stdout)): + try: + from termios import TIOCGWINSZ + from fcntl import ioctl + from array import array + except ImportError: + pass + else: + try: + ncols = array('h', ioctl(file, TIOCGWINSZ, '\0' * 8))[1] + except SystemExit: + raise + except: + try: + from os import environ + except ImportError: + pass + else: + ncols = int(environ.get('COLUMNS', 1)) - 1 + if miniters is None: miniters = 0 dynamic_miniters = True