From 1e8e60574f09eb261a94bb7005d10fb2d9d78d2d Mon Sep 17 00:00:00 2001 From: Adam Waldenberg Date: Thu, 24 May 2012 01:30:51 +0200 Subject: [PATCH] The terminal module now also checks if the terminal actually is a terminal. The output is now limited to 80 characters; this is useful when cat'ing to a file and terminal.get_size() is called. The previous behaviour was to always output according to the terminal width, something that probably isn't what someone usually wants when outputting to a file. --- terminal.py | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/terminal.py b/terminal.py index a076433..22a7958 100644 --- a/terminal.py +++ b/terminal.py @@ -19,6 +19,7 @@ import os import platform +import sys __bold__ = "\033[1m" __normal__ = "\033[0;0m" @@ -43,19 +44,6 @@ def __get_size_windows__(): else: return None -def __get_size_tput__(): - try: - import subprocess - proc = subprocess.Popen(["tput", "cols"], stdin = subprocess.PIPE, stdout = subprocess.PIPE) - output = proc.communicate(input = None) - cols = int(output[0]) - proc = subprocess.Popen(["tput", "lines"], stdin = subprocess.PIPE, stdout = subprocess.PIPE) - output = proc.communicate(input = None) - rows = int(output[0]) - return (cols, rows) - except: - return None - def __get_size_linux__(): def ioctl_get_window_size(file_descriptor): try: @@ -97,16 +85,12 @@ def printb(string): print __bold__ + string + __normal__ def get_size(): - current_os = platform.system() - tuple_xy = None + if sys.stdout.isatty(): + current_os = platform.system() - if current_os == 'Windows': - tuple_xy = __get_size_windows__() - if tuple_xy is None: - tuple_xy = __get_size_tput__() - if current_os == 'Linux' or current_os == 'Darwin' or current_os.startswith('CYGWIN'): - tuple_xy = __get_size_linux__() - if tuple_xy is None: - tuple_xy = (80, 25) + if current_os == 'Windows': + return __get_size_windows__() + elif current_os == 'Linux' or current_os == 'Darwin' or current_os.startswith('CYGWIN'): + return __get_size_linux__() - return tuple_xy + return (80, 25)