This documentation was generated with AI assistance and is still being audited. Some, or potentially a lot, of this information may be inaccurate. Learn more.
defsupports_color()->bool:"""Check if the current stream supports color output."""config=StreamConfig.from_env()ifconfig.no_color:returnFalseifconfig.force_color:returnTrue# Check if we're in a TTYreturnis_tty()
defwrite_to_console(message:str,stream:TextIO|None=None,log_fallback:bool=True)->None:"""Write a message to the console stream. Args: message: Message to write stream: Optional specific stream to write to, defaults to current console stream log_fallback: Whether to log when falling back to stderr """target_stream=streamorget_console_stream()try:target_stream.write(message)target_stream.flush()exceptUnicodeEncodeError:# Stream cannot encode the message (e.g., emoji on Windows cp1252).# Encode to UTF-8 with replacement and decode back to get safe text.safe_message=message.encode("utf-8",errors="replace").decode("ascii",errors="replace")try:target_stream.write(safe_message)target_stream.flush()exceptException:# Last resort: write to stderrsys.stderr.write(safe_message)sys.stderr.flush()returnexceptExceptionase:# Log the fallback for debugging if requestediflog_fallback:try:fromprovide.foundation.hub.foundationimportget_foundation_loggerget_foundation_logger().debug("Console write failed, falling back to stderr",error=str(e),error_type=type(e).__name__,stream_type=type(target_stream).__name__,)exceptExceptionaslog_error:# Foundation logger failed, fall back to direct stderr loggingtry:sys.stderr.write(f"[DEBUG] Console write failed (logging also failed): "f"{e.__class__.__name__}: {e} (log_error: {log_error.__class__.__name__})\n")sys.stderr.flush()exceptException:# Even stderr failed - this is a critical system failure, we cannot continueraiseRuntimeError("Critical system failure: unable to write debug information to any stream")frome# Fallback to stderr - if this fails, let it propagatesys.stderr.write(message)sys.stderr.flush()