Mercurial > public > mercurial-scm > hg
comparison mercurial/scmposix.py @ 30309:4b1af1c867fa
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.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 20 Oct 2016 21:38:44 +0900 |
parents | c90a05124fae |
children | 5c379b1f56c7 |
comparison
equal
deleted
inserted
replaced
30308:d500ddae7494 | 30309:4b1af1c867fa |
---|---|
1 from __future__ import absolute_import | 1 from __future__ import absolute_import |
2 | 2 |
3 import errno | |
4 import fcntl | |
3 import os | 5 import os |
4 import sys | 6 import sys |
5 | 7 |
6 from . import ( | 8 from . import ( |
7 encoding, | 9 encoding, |
36 def userrcpath(): | 38 def userrcpath(): |
37 if sys.platform == 'plan9': | 39 if sys.platform == 'plan9': |
38 return [encoding.environ['home'] + '/lib/hgrc'] | 40 return [encoding.environ['home'] + '/lib/hgrc'] |
39 else: | 41 else: |
40 return [os.path.expanduser('~/.hgrc')] | 42 return [os.path.expanduser('~/.hgrc')] |
43 | |
44 def termwidth(): | |
45 try: | |
46 import array | |
47 import termios | |
48 for dev in (sys.stderr, sys.stdout, sys.stdin): | |
49 try: | |
50 try: | |
51 fd = dev.fileno() | |
52 except AttributeError: | |
53 continue | |
54 if not os.isatty(fd): | |
55 continue | |
56 try: | |
57 arri = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 8) | |
58 width = array.array('h', arri)[1] | |
59 if width > 0: | |
60 return width | |
61 except AttributeError: | |
62 pass | |
63 except ValueError: | |
64 pass | |
65 except IOError as e: | |
66 if e[0] == errno.EINVAL: | |
67 pass | |
68 else: | |
69 raise | |
70 except ImportError: | |
71 pass | |
72 return 80 |