Use shutil.get_terminal_size for getting the terminal width if it's available (python >= 3.3)

This commit is contained in:
Jaime Marquínez Ferrándiz 2015-02-28 21:42:16 +01:00
parent 0134901108
commit 003c69a84b
4 changed files with 34 additions and 21 deletions

View File

@ -28,6 +28,7 @@ from .compat import (
compat_basestring, compat_basestring,
compat_cookiejar, compat_cookiejar,
compat_expanduser, compat_expanduser,
compat_get_terminal_size,
compat_http_client, compat_http_client,
compat_kwargs, compat_kwargs,
compat_str, compat_str,
@ -46,7 +47,6 @@ from .utils import (
ExtractorError, ExtractorError,
format_bytes, format_bytes,
formatSeconds, formatSeconds,
get_term_width,
locked_file, locked_file,
make_HTTPS_handler, make_HTTPS_handler,
MaxDownloadsReached, MaxDownloadsReached,
@ -284,7 +284,7 @@ class YoutubeDL(object):
try: try:
import pty import pty
master, slave = pty.openpty() master, slave = pty.openpty()
width = get_term_width() width = compat_get_terminal_size().columns
if width is None: if width is None:
width_args = [] width_args = []
else: else:

View File

@ -1,9 +1,11 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import collections
import getpass import getpass
import optparse import optparse
import os import os
import re import re
import shutil
import socket import socket
import subprocess import subprocess
import sys import sys
@ -364,6 +366,33 @@ def workaround_optparse_bug9161():
return real_add_option(self, *bargs, **bkwargs) return real_add_option(self, *bargs, **bkwargs)
optparse.OptionGroup.add_option = _compat_add_option optparse.OptionGroup.add_option = _compat_add_option
if hasattr(shutil, 'get_terminal_size'): # Python >= 3.3
compat_get_terminal_size = shutil.get_terminal_size
else:
_terminal_size = collections.namedtuple('terminal_size', ['columns', 'lines'])
def compat_get_terminal_size():
columns = compat_getenv('COLUMNS', None)
if columns:
columns = int(columns)
else:
columns = None
lines = compat_getenv('LINES', None)
if lines:
lines = int(lines)
else:
lines = None
try:
sp = subprocess.Popen(
['stty', 'size'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
lines, columns = map(int, out.split())
except:
pass
return _terminal_size(columns, lines)
__all__ = [ __all__ = [
'compat_HTTPError', 'compat_HTTPError',
@ -371,6 +400,7 @@ __all__ = [
'compat_chr', 'compat_chr',
'compat_cookiejar', 'compat_cookiejar',
'compat_expanduser', 'compat_expanduser',
'compat_get_terminal_size',
'compat_getenv', 'compat_getenv',
'compat_getpass', 'compat_getpass',
'compat_html_entities', 'compat_html_entities',

View File

@ -8,11 +8,11 @@ import sys
from .downloader.external import list_external_downloaders from .downloader.external import list_external_downloaders
from .compat import ( from .compat import (
compat_expanduser, compat_expanduser,
compat_get_terminal_size,
compat_getenv, compat_getenv,
compat_kwargs, compat_kwargs,
) )
from .utils import ( from .utils import (
get_term_width,
write_string, write_string,
) )
from .version import __version__ from .version import __version__
@ -100,7 +100,7 @@ def parseOpts(overrideArguments=None):
return opts return opts
# No need to wrap help messages if we're on a wide console # No need to wrap help messages if we're on a wide console
columns = get_term_width() columns = compat_get_terminal_size().columns
max_width = columns if columns else 80 max_width = columns if columns else 80
max_help_position = 80 max_help_position = 80

View File

@ -35,7 +35,6 @@ import zlib
from .compat import ( from .compat import (
compat_basestring, compat_basestring,
compat_chr, compat_chr,
compat_getenv,
compat_html_entities, compat_html_entities,
compat_http_client, compat_http_client,
compat_parse_qs, compat_parse_qs,
@ -1173,22 +1172,6 @@ def parse_filesize(s):
return int(float(num_str) * mult) return int(float(num_str) * mult)
def get_term_width():
columns = compat_getenv('COLUMNS', None)
if columns:
return int(columns)
try:
sp = subprocess.Popen(
['stty', 'size'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = sp.communicate()
return int(out.split()[1])
except:
pass
return None
def month_by_name(name): def month_by_name(name):
""" Return the number of a month by (locale-independently) English name """ """ Return the number of a month by (locale-independently) English name """