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 os
import platform import platform
import sys
__bold__ = "\033[1m" __bold__ = "\033[1m"
__normal__ = "\033[0;0m" __normal__ = "\033[0;0m"
@ -43,19 +44,6 @@ def __get_size_windows__():
else: else:
return None 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 __get_size_linux__():
def ioctl_get_window_size(file_descriptor): def ioctl_get_window_size(file_descriptor):
try: try:
@ -97,16 +85,12 @@ def printb(string):
print __bold__ + string + __normal__ print __bold__ + string + __normal__
def get_size(): def get_size():
current_os = platform.system() if sys.stdout.isatty():
tuple_xy = None current_os = platform.system()
if current_os == 'Windows': if current_os == 'Windows':
tuple_xy = __get_size_windows__() return __get_size_windows__()
if tuple_xy is None: elif current_os == 'Linux' or current_os == 'Darwin' or current_os.startswith('CYGWIN'):
tuple_xy = __get_size_tput__() return __get_size_linux__()
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)
return tuple_xy return (80, 25)