MCPcopy
hub / github.com/Textualize/rich / write_styled

Method write_styled

rich/_win32_console.py:405–442  ·  view source on GitHub ↗

Write styled text to the terminal. Args: text (str): The text to write style (Style): The style of the text

(self, text: str, style: Style)

Source from the content-addressed store, hash-verified

403 self.flush()
404
405 def write_styled(self, text: str, style: Style) -> None:
406 """Write styled text to the terminal.
407
408 Args:
409 text (str): The text to write
410 style (Style): The style of the text
411 """
412 color = style.color
413 bgcolor = style.bgcolor
414 if style.reverse:
415 color, bgcolor = bgcolor, color
416
417 if color:
418 fore = color.downgrade(ColorSystem.WINDOWS).number
419 fore = fore if fore is not None else 7 # Default to ANSI 7: White
420 if style.bold:
421 fore = fore | self.BRIGHT_BIT
422 if style.dim:
423 fore = fore & ~self.BRIGHT_BIT
424 fore = self.ANSI_TO_WINDOWS[fore]
425 else:
426 fore = self._default_fore
427
428 if bgcolor:
429 back = bgcolor.downgrade(ColorSystem.WINDOWS).number
430 back = back if back is not None else 0 # Default to ANSI 0: Black
431 back = self.ANSI_TO_WINDOWS[back]
432 else:
433 back = self._default_back
434
435 assert fore is not None
436 assert back is not None
437
438 SetConsoleTextAttribute(
439 self._handle, attributes=ctypes.c_ushort(fore | (back << 4))
440 )
441 self.write_text(text)
442 SetConsoleTextAttribute(self._handle, attributes=self._default_text)
443
444 def move_cursor_to(self, new_position: WindowsCoordinates) -> None:
445 """Set the position of the cursor

Calls 3

write_textMethod · 0.95
SetConsoleTextAttributeFunction · 0.85
downgradeMethod · 0.80