def __enter__(self): TerminalQueryContext.__enter__(self) # try getting the rgb value for color 0 to decide whether to # bother trying to query any more colors. self.do_query = (self.do_query and self.get_indexed_color(0, self.timeout*2)) if self.color_level >= 1: stdout.write(self.reset) return self
def __init__(self, tty_fd, timeout=100, color_level=3, do_query=True, screen_forward=False): ''' tty_fd: open file descriptor connected to a terminal. timeout: same interpretation as in rgb_query. A larger timeout will be used a small number of times to test the capabilities of the terminal. color_level: how much color should be in the output. Use 0 to suppress all color and 3 or greater for maximum coloredness. do_query: whether to attempt to query RGB values from the terminal or just use placeholders everywhere. screen_forward: whether to attempt to forward queries through a screen or tmux session if we are in one. ''' TerminalQueryContext.__init__(self, tty_fd, screen_forward) self.timeout = timeout self.color_level = color_level self.do_query = do_query def none_factory(): return None # colors for highlighting self.hi = defaultdict(none_factory) self.hi['['] = 10 self.hi[']'] = 10 self.hi['+'] = 9 self.hi['/'] = 9 for c in '0123456789ABCDEF': self.hi[c] = 12 # String to use for color values that couldn't be determined self.rgb_placeholder = '??????' self.fmt = '{:02X}{:02X}{:02X}' self.scale = 0xff
def __exit__(self, exc_type, exc_value, traceback): if self.color_level >= 1: stdout.write(self.reset) TerminalQueryContext.__exit__(self, exc_type, exc_value, traceback)