我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用colorama.AnsiToWin32()。
def __init__(self, file=None, stringio=False, encoding=None): if file is None: if stringio: self.stringio = file = py.io.TextIO() else: file = py.std.sys.stdout elif py.builtin.callable(file) and not ( hasattr(file, "write") and hasattr(file, "flush")): file = WriteFile(file, encoding=encoding) if hasattr(file, "isatty") and file.isatty() and colorama: file = colorama.AnsiToWin32(file).stream self.encoding = encoding or getattr(file, 'encoding', "utf-8") self._file = file self.fullwidth = get_terminal_width() self.hasmarkup = should_do_markup(file) self._lastlen = 0
def __init__(self, in_file = None): import sys self._line_wrap = False self._write_pos = 0 self._file = in_file or sys.stdout #self._file = sys.stdout if not (hasattr(self._file, 'isatty') and self._file.isatty()): # the stdout is not a terminal, this for example happens if the # output is piped to less, e.g. "bin/test | less". In this case, # the terminal control sequences would be printed verbatim, so # don't use any colors. self.write = self.normal_write elif sys.platform != "win32": # We are on *nix system we can use ansi self.write = self.ansi_write else: try: import colorama colorama.init(wrap=False) self._file = colorama.AnsiToWin32(self._file).stream self.write = self.ansi_write except ImportError: self.write = self.normal_write
def should_color(consumer, environ, std=(sys.stdout, sys.stderr)): real_consumer = (consumer if not isinstance(consumer, colorama.AnsiToWin32) else consumer.wrapped) # If consumer isn't stdout or stderr we shouldn't colorize it if real_consumer not in std: return False # If consumer is a tty we should color it if hasattr(real_consumer, "isatty") and real_consumer.isatty(): return True # If we have an ASNI term we should color it if environ.get("TERM") == "ANSI": return True # If anything else we should not color it return False
def __init__(self, file=None, stringio=False, encoding=None): if file is None: if stringio: self.stringio = file = py.io.TextIO() else: from sys import stdout as file elif py.builtin.callable(file) and not ( hasattr(file, "write") and hasattr(file, "flush")): file = WriteFile(file, encoding=encoding) if hasattr(file, "isatty") and file.isatty() and colorama: file = colorama.AnsiToWin32(file).stream self.encoding = encoding or getattr(file, 'encoding', "utf-8") self._file = file self.hasmarkup = should_do_markup(file) self._lastlen = 0 self._chars_on_current_line = 0
def __init__(self, **kwargs): """ Use keyword arguments to overwrite any of the class attributes for this instance. """ assert all(hasattr(type(self), attr) for attr in kwargs.keys()) self.__dict__.update(**kwargs) # Keyword arguments > stream.encoding > default utf8 if self.stdin_encoding is None: self.stdin_encoding = getattr( self.stdin, 'encoding', None) or 'utf8' if self.stdout_encoding is None: actual_stdout = self.stdout if is_windows: # noinspection PyUnresolvedReferences from colorama import AnsiToWin32 if isinstance(self.stdout, AnsiToWin32): actual_stdout = self.stdout.wrapped self.stdout_encoding = getattr( actual_stdout, 'encoding', None) or 'utf8'
def auto_wrap_for_ansi(stream, color=None): """This function wraps a stream so that calls through colorama are issued to the win32 console API to recolor on demand. It also ensures to reset the colors if a write call is interrupted to not destroy the console afterwards. """ try: cached = _ansi_stream_wrappers.get(stream) except Exception: cached = None if cached is not None: return cached strip = should_strip_ansi(stream, color) ansi_wrapper = colorama.AnsiToWin32(stream, strip=strip) rv = ansi_wrapper.stream _write = rv.write def _safe_write(s): try: return _write(s) except: ansi_wrapper.reset_all() raise rv.write = _safe_write try: _ansi_stream_wrappers[stream] = rv except Exception: pass return rv
def __init__(self, stream, color_map=None): logging.StreamHandler.__init__(self, colorama.AnsiToWin32(stream).stream) if color_map is not None: self.color_map = color_map
def add_consumers(self, *consumers): if sys.platform.startswith("win"): for level, consumer in consumers: if hasattr(consumer, "write"): self.consumers.append( (level, colorama.AnsiToWin32(consumer)), ) else: self.consumers.append((level, consumer)) else: self.consumers.extend(consumers)
def __init__(self, stream, logger, level_manager): super(ColorizedStreamHandler, self).__init__(stream) if platform.system() == 'Windows': self.stream = colorama.AnsiToWin32(self.stream).stream fmt = self.CONSOLE_LOG_FORMAT[logger.name][self.enable_color] super(ColorizedStreamHandler, self).setFormatter(logging.Formatter(fmt)) super(ColorizedStreamHandler, self).setLevel(level_manager.get_user_setting_level(logger))
def out(self, obj): if platform.system() == 'Windows': self.file = colorama.AnsiToWin32(self.file).stream output = self.formatter(obj) try: print(output, file=self.file, end='') except IOError as ex: if ex.errno == errno.EPIPE: pass else: raise except UnicodeEncodeError: print(output.encode('ascii', 'ignore').decode('utf-8', 'ignore'), file=self.file, end='')
def __init__(self, stream): wrapper = colorama.AnsiToWin32(stream) if wrapper.should_wrap(): stream = wrapper.stream super(LogHandler, self).__init__(stream)