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.
This commit is contained in:
Adam Waldenberg 2012-05-24 01:30:51 +02:00
parent 1d30ffa7f4
commit 1e8e60574f
1 changed files with 8 additions and 24 deletions

View File

@ -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)