async def _process_stdout_tty(root: Root, stream: StdStream, stdout: Output, helper: AttachHelper) -> None: codec_info = codecs.lookup("utf8") decoder = codec_info.incrementaldecoder("replace") while True: chunk = await stream.read_out() if chunk is None: txt = decoder.decode(b"", final=True) if not txt: return else: txt = decoder.decode(chunk.data) async with helper.write_sem: if not helper.quiet and not helper.attach_ready: # Print header to stdout only, # logs are printed to stdout and never to # stderr (but logs printing is stopped by # helper.attach_ready = True regardless # what stream had receive text in attached mode. if helper.log_printed: s = ATTACH_STARTED_AFTER_LOGS if root.tty: s = click.style("√ ", fg="green") + s stdout.write_raw(s + "\n") helper.attach_ready = True stdout.write_raw(txt) stdout.flush()
def print_formatted_text( output: Output, formatted_text: AnyFormattedText, style: BaseStyle, style_transformation: Optional[StyleTransformation] = None, color_depth: Optional[ColorDepth] = None, ) -> None: """ Print a list of (style_str, text) tuples in the given style to the output. """ fragments = to_formatted_text(formatted_text) style_transformation = style_transformation or DummyStyleTransformation() color_depth = color_depth or output.get_default_color_depth() # Reset first. output.reset_attributes() output.enable_autowrap() last_attrs: Optional[Attrs] = None # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str, style_transformation) for style_str, text, *_ in fragments: attrs = attrs_for_style_string[style_str] # Set style attributes if something changed. if attrs != last_attrs: if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() last_attrs = attrs # Eliminate carriage returns text = text.replace("\r", "") # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) output.write(text.replace("\n", "\r\n")) # Reset again. output.reset_attributes() output.flush()
def print_formatted_text( output: Output, formatted_text: AnyFormattedText, style: BaseStyle, style_transformation: Optional[StyleTransformation] = None, color_depth: Optional[ColorDepth] = None) -> None: """ Print a list of (style_str, text) tuples in the given style to the output. """ fragments = to_formatted_text(formatted_text) style_transformation = style_transformation or DummyStyleTransformation() color_depth = color_depth or ColorDepth.default() # Reset first. output.reset_attributes() output.enable_autowrap() # Print all (style_str, text) tuples. attrs_for_style_string = _StyleStringToAttrsCache( style.get_attrs_for_style_str, style_transformation) for style_str, text, *_ in fragments: attrs = attrs_for_style_string[style_str] if attrs: output.set_attributes(attrs, color_depth) else: output.reset_attributes() # Assume that the output is raw, and insert a carriage return before # every newline. (Also important when the front-end is a telnet client.) assert '\r' not in text output.write(text.replace('\n', '\r\n')) # Reset again. output.reset_attributes() output.flush()