From patchwork Fri Nov 4 07:49:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [6, of, 7] scmutil: extend termwidth() to return terminal height, renamed to termsize() From: Yuya Nishihara X-Patchwork-Id: 17338 Message-Id: <5a6ee9257e4d6c67c641.1478245741@mimosa> To: mercurial-devel@mercurial-scm.org Date: Fri, 04 Nov 2016 16:49:01 +0900 # HG changeset patch # User Yuya Nishihara # Date 1476972545 -32400 # Thu Oct 20 23:09:05 2016 +0900 # Node ID 5a6ee9257e4d6c67c6414b9d0f0fef35d3f2d545 # Parent 1bdd3d02c485da8592d7da1fcb5778bea5890706 # EXP-Topic stdio scmutil: extend termwidth() to return terminal height, renamed to termsize() It appears crecord.py has its own termsize() function. I want to get rid of it. The fallback height is chosen from the default of cmd.exe on Windows, and VT100 on Unix. diff --git a/mercurial/scmposix.py b/mercurial/scmposix.py --- a/mercurial/scmposix.py +++ b/mercurial/scmposix.py @@ -42,12 +42,12 @@ def userrcpath(): else: return [os.path.expanduser('~/.hgrc')] -def termwidth(ui): +def termsize(ui): try: import termios TIOCGWINSZ = termios.TIOCGWINSZ # unavailable on IRIX (issue3449) except (AttributeError, ImportError): - return 80 + return 80, 24 for dev in (ui.ferr, ui.fout, ui.fin): try: @@ -58,9 +58,9 @@ def termwidth(ui): if not os.isatty(fd): continue arri = fcntl.ioctl(fd, TIOCGWINSZ, '\0' * 8) - width = array.array('h', arri)[1] - if width > 0: - return width + height, width = array.array('h', arri)[:2] + if width > 0 and height > 0: + return width, height except ValueError: pass except IOError as e: @@ -68,4 +68,4 @@ def termwidth(ui): pass else: raise - return 80 + return 80, 24 diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py --- a/mercurial/scmutil.py +++ b/mercurial/scmutil.py @@ -39,7 +39,7 @@ else: systemrcpath = scmplatform.systemrcpath userrcpath = scmplatform.userrcpath -termwidth = scmplatform.termwidth +termsize = scmplatform.termsize class status(tuple): '''Named tuple with a list of files per status. The 'deleted', 'unknown' diff --git a/mercurial/scmwindows.py b/mercurial/scmwindows.py --- a/mercurial/scmwindows.py +++ b/mercurial/scmwindows.py @@ -53,5 +53,5 @@ def userrcpath(): path.append(os.path.join(userprofile, '.hgrc')) return path -def termwidth(ui): - return win32.termwidth() +def termsize(ui): + return win32.termsize() diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -822,7 +822,7 @@ class ui(object): return int(encoding.environ['COLUMNS']) except ValueError: pass - return scmutil.termwidth(self) + return scmutil.termsize(self)[0] def formatted(self): '''should formatted output be used? diff --git a/mercurial/win32.py b/mercurial/win32.py --- a/mercurial/win32.py +++ b/mercurial/win32.py @@ -347,23 +347,25 @@ def hidewindow(): pid = _kernel32.GetCurrentProcessId() _user32.EnumWindows(_WNDENUMPROC(callback), pid) -def termwidth(): +def termsize(): # cmd.exe does not handle CR like a unix console, the CR is # counted in the line length. On 80 columns consoles, if 80 # characters are written, the following CR won't apply on the # current line but on the new one. Keep room for it. width = 80 - 1 + height = 25 # Query stderr to avoid problems with redirections screenbuf = _kernel32.GetStdHandle( _STD_ERROR_HANDLE) # don't close the handle returned if screenbuf is None or screenbuf == _INVALID_HANDLE_VALUE: - return width + return width, height csbi = _CONSOLE_SCREEN_BUFFER_INFO() if not _kernel32.GetConsoleScreenBufferInfo( screenbuf, ctypes.byref(csbi)): - return width + return width, height width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1' - return width + height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 + return width, height def _1stchild(pid): '''return the 1st found child of the given pid