|
| 1 | +from functools import lru_cache |
1 | 2 | import logging
|
2 | 3 | import os
|
| 4 | +import sys |
| 5 | +from typing import TYPE_CHECKING, List, Dict, Any, Optional, Union |
3 | 6 | from rich.console import Console
|
4 | 7 | from rich.theme import Theme
|
| 8 | +from safety.emoji import load_emoji |
| 9 | + |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from rich.console import HighlighterType, JustifyMethod, OverflowMethod |
| 13 | + from rich.style import Style |
| 14 | + from rich.text import Text |
| 15 | + |
5 | 16 |
|
6 | 17 | LOG = logging.getLogger(__name__)
|
7 | 18 |
|
| 19 | + |
| 20 | +@lru_cache() |
| 21 | +def should_use_ascii(): |
| 22 | + """ |
| 23 | + Check if we should use ASCII alternatives for emojis |
| 24 | + """ |
| 25 | + encoding = getattr(sys.stdout, "encoding", "").lower() |
| 26 | + |
| 27 | + if encoding in {"utf-8", "utf8", "cp65001", "utf-8-sig"}: |
| 28 | + return False |
| 29 | + |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +def get_spinner_animation() -> List[str]: |
| 34 | + """ |
| 35 | + Get the spinner animation based on the encoding |
| 36 | + """ |
| 37 | + if should_use_ascii(): |
| 38 | + spinner = [ |
| 39 | + "[ ]", |
| 40 | + "[= ]", |
| 41 | + "[== ]", |
| 42 | + "[=== ]", |
| 43 | + "[====]", |
| 44 | + "[ ===]", |
| 45 | + "[ ==]", |
| 46 | + "[ =]", |
| 47 | + ] |
| 48 | + else: |
| 49 | + spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] |
| 50 | + return spinner |
| 51 | + |
| 52 | + |
| 53 | +def replace_non_ascii_chars(text: str): |
| 54 | + """ |
| 55 | + Replace non-ascii characters with ascii alternatives |
| 56 | + """ |
| 57 | + CHARS_MAP = { |
| 58 | + "━": "-", |
| 59 | + "’": "'", |
| 60 | + } |
| 61 | + |
| 62 | + for char, replacement in CHARS_MAP.items(): |
| 63 | + text = text.replace(char, replacement) |
| 64 | + |
| 65 | + try: |
| 66 | + text.encode("ascii") |
| 67 | + except UnicodeEncodeError: |
| 68 | + LOG.warning("No handled non-ascii characters detected, encoding with replace") |
| 69 | + text = text.encode("ascii", "replace").decode("ascii") |
| 70 | + |
| 71 | + return text |
| 72 | + |
| 73 | + |
| 74 | +class SafeConsole(Console): |
| 75 | + """ |
| 76 | + Console subclass that handles emoji encoding issues by detecting |
| 77 | + problematic encoding environments and replacing emojis with ASCII alternatives. |
| 78 | + Uses string replacement for custom emoji namespace to avoid private API usage. |
| 79 | + """ |
| 80 | + |
| 81 | + def render_str( |
| 82 | + self, |
| 83 | + text: str, |
| 84 | + *, |
| 85 | + style: Union[str, "Style"] = "", |
| 86 | + justify: Optional["JustifyMethod"] = None, |
| 87 | + overflow: Optional["OverflowMethod"] = None, |
| 88 | + emoji: Optional[bool] = None, |
| 89 | + markup: Optional[bool] = None, |
| 90 | + highlight: Optional[bool] = None, |
| 91 | + highlighter: Optional["HighlighterType"] = None, |
| 92 | + ) -> "Text": |
| 93 | + """ |
| 94 | + Override render_str to pre-process our custom emojis before Rich handles the text. |
| 95 | + """ |
| 96 | + |
| 97 | + use_ascii = should_use_ascii() |
| 98 | + text = load_emoji(text, use_ascii=use_ascii) |
| 99 | + |
| 100 | + if use_ascii: |
| 101 | + text = replace_non_ascii_chars(text) |
| 102 | + |
| 103 | + # Let Rich handle everything else normally |
| 104 | + return super().render_str( |
| 105 | + text, |
| 106 | + style=style, |
| 107 | + justify=justify, |
| 108 | + overflow=overflow, |
| 109 | + emoji=emoji, |
| 110 | + markup=markup, |
| 111 | + highlight=highlight, |
| 112 | + highlighter=highlighter, |
| 113 | + ) |
| 114 | + |
| 115 | + |
8 | 116 | SAFETY_THEME = {
|
9 | 117 | "file_title": "bold default on default",
|
10 | 118 | "dep_name": "bold yellow on default",
|
|
23 | 131 | "vulns_found_number": "red on default",
|
24 | 132 | }
|
25 | 133 |
|
26 |
| -non_interactive = os.getenv('NON_INTERACTIVE') == '1' |
27 | 134 |
|
28 |
| -console_kwargs = {"theme": Theme(SAFETY_THEME, inherit=False)} |
| 135 | +non_interactive = os.getenv("NON_INTERACTIVE") == "1" |
29 | 136 |
|
30 |
| -if non_interactive: |
31 |
| - LOG.info("NON_INTERACTIVE environment variable is set, forcing non-interactive mode") |
32 |
| - console_kwargs.update({"force_terminal": True, "force_interactive": False}) |
| 137 | +console_kwargs: Dict[str, Any] = { |
| 138 | + "theme": Theme(SAFETY_THEME, inherit=False), |
| 139 | + "emoji": not should_use_ascii(), |
| 140 | +} |
33 | 141 |
|
| 142 | +if non_interactive: |
| 143 | + LOG.info( |
| 144 | + "NON_INTERACTIVE environment variable is set, forcing non-interactive mode" |
| 145 | + ) |
| 146 | + console_kwargs["force_terminal"] = True |
| 147 | + console_kwargs["force_interactive"] = False |
34 | 148 |
|
35 |
| -main_console = Console(**console_kwargs) |
| 149 | +main_console = SafeConsole(**console_kwargs) |
0 commit comments