[PATCH 1 of 7] scmutil: move util.termwidth()
Yuya Nishihara
yuya at tcha.org
Fri Nov 4 07:48:56 UTC 2016
# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1476967124 -32400
# Thu Oct 20 21:38:44 2016 +0900
# Node ID e8790ea449691089861957e8259adb2a753748c8
# Parent d44c407a59998c0a023e58bc278e19e939185e4f
# EXP-Topic stdio
scmutil: move util.termwidth()
I'm going to get rid of sys.stderr|out|in references from posix.termwidth().
In order to do that, termwidth() needs to take a ui, but functions in util.py
shouldn't depend on a ui object. So moves termwidth() to scmutil.py.
diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -463,36 +463,6 @@ def spawndetached(args):
def gethgcmd():
return sys.argv[:1]
-def termwidth():
- try:
- import array
- import termios
- for dev in (sys.stderr, sys.stdout, sys.stdin):
- try:
- try:
- fd = dev.fileno()
- except AttributeError:
- continue
- if not os.isatty(fd):
- continue
- try:
- arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
- width = array.array('h', arri)[1]
- if width > 0:
- return width
- except AttributeError:
- pass
- except ValueError:
- pass
- except IOError as e:
- if e[0] == errno.EINVAL:
- pass
- else:
- raise
- except ImportError:
- pass
- return 80
-
def makedir(path, notindexed):
os.mkdir(path)
diff --git a/mercurial/scmposix.py b/mercurial/scmposix.py
--- a/mercurial/scmposix.py
+++ b/mercurial/scmposix.py
@@ -1,5 +1,7 @@
from __future__ import absolute_import
+import errno
+import fcntl
import os
import sys
@@ -38,3 +40,33 @@ def userrcpath():
return [encoding.environ['home'] + '/lib/hgrc']
else:
return [os.path.expanduser('~/.hgrc')]
+
+def termwidth():
+ try:
+ import array
+ import termios
+ for dev in (sys.stderr, sys.stdout, sys.stdin):
+ try:
+ try:
+ fd = dev.fileno()
+ except AttributeError:
+ continue
+ if not os.isatty(fd):
+ continue
+ try:
+ arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8)
+ width = array.array('h', arri)[1]
+ if width > 0:
+ return width
+ except AttributeError:
+ pass
+ except ValueError:
+ pass
+ except IOError as e:
+ if e[0] == errno.EINVAL:
+ pass
+ else:
+ raise
+ except ImportError:
+ pass
+ return 80
diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -39,6 +39,7 @@ else:
systemrcpath = scmplatform.systemrcpath
userrcpath = scmplatform.userrcpath
+termwidth = scmplatform.termwidth
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
@@ -5,6 +5,7 @@ import os
from . import (
osutil,
util,
+ win32,
)
try:
@@ -51,3 +52,6 @@ def userrcpath():
path.append(os.path.join(userprofile, 'mercurial.ini'))
path.append(os.path.join(userprofile, '.hgrc'))
return path
+
+def termwidth():
+ return win32.termwidth()
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 util.termwidth()
+ return scmutil.termwidth()
def formatted(self):
'''should formatted output be used?
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -122,7 +122,6 @@ sshargs = platform.sshargs
statfiles = getattr(osutil, 'statfiles', platform.statfiles)
statisexec = platform.statisexec
statislink = platform.statislink
-termwidth = platform.termwidth
testpid = platform.testpid
umask = platform.umask
unlink = platform.unlink
diff --git a/mercurial/windows.py b/mercurial/windows.py
--- a/mercurial/windows.py
+++ b/mercurial/windows.py
@@ -38,7 +38,6 @@ samefile = win32.samefile
setsignalhandler = win32.setsignalhandler
spawndetached = win32.spawndetached
split = os.path.split
-termwidth = win32.termwidth
testpid = win32.testpid
unlink = win32.unlink
More information about the Mercurial-devel
mailing list