+ progress_chars = '|/-\\'
+
+ def __init__(self):
+ if os.environ['TERM'] == 'dumb':
+ self.output = None
+ else:
+ try:
+ self.output = open('/dev/tty', 'w')
+ except IOError:
+ self.output = None
+
+ if not self.output:
+ self._clear_colors()
+
+ self.width = self._get_width()
+ self._enabled = True
+
+ def _raw_write(self, string):
+ '''Write raw data if output is enabled.'''
+ if self._enabled and self.output:
+ try:
+ self.output.write(string)
+ self.output.flush()
+ except IOError:
+ self._enabled = False
+
+ def _get_width(self):
+ try:
+ import fcntl, termios, struct, os
+ s = struct.pack('HHHH', 0, 0, 0, 0)
+ x = fcntl.ioctl(self.output.fileno(), termios.TIOCGWINSZ, s)
+ return struct.unpack('HHHH', x)[1]
+ except:
+ return int(getattr(os.environ, 'COLUMNS', 80))
+
+ def _clear_colors(self):
+ '''Sets all color and attribute memebers to empty strings'''
+ for cls in ('attr', 'fg', 'bg'):
+ c = getattr(self, cls)
+ for key in dir(c):