| 1 | import time |
| 2 | from collections.abc import Callable |
| 3 | from os import get_terminal_size |
| 4 | |
| 5 | from loguru import logger |
| 6 | from rich.console import Console |
| 7 | from rich.markdown import Markdown |
| 8 | from rich.markup import escape |
| 9 | from rich.panel import Panel |
| 10 | |
| 11 | |
| 12 | def terminal_width(): |
| 13 | try: |
| 14 | return get_terminal_size().columns |
| 15 | except OSError: |
| 16 | return 80 |
| 17 | |
| 18 | |
| 19 | WIDTH = min(120, terminal_width() - 10) |
| 20 | |
| 21 | console = Console() |
| 22 | |
| 23 | print_stdout = True |
| 24 | |
| 25 | |
| 26 | def log_exception(exception): |
| 27 | logger.exception(exception) |
| 28 | |
| 29 | |
| 30 | def print_banner(msg: str) -> None: |
| 31 | if not print_stdout: |
| 32 | return |
| 33 | |
| 34 | banner = f" {msg} ".center(WIDTH, "=") |
| 35 | console.print() |
| 36 | console.print(banner, style="bold") |
| 37 | console.print() |
| 38 | |
| 39 | |
| 40 | def replace_html_tags(content: str): |
| 41 | """ |
| 42 | Helper method to process the content before printing to markdown. |
| 43 | """ |
| 44 | replace_dict = { |
| 45 | "<file>": "[file]", |
| 46 | "<class>": "[class]", |
| 47 | "<func>": "[func]", |
| 48 | "<method>": "[method]", |
| 49 | "<code>": "[code]", |
| 50 | "<original>": "[original]", |
| 51 | "<patched>": "[patched]", |
| 52 | "</file>": "[/file]", |
| 53 | "</class>": "[/class]", |
| 54 | "</func>": "[/func]", |
| 55 | "</method>": "[/method]", |
| 56 | "</code>": "[/code]", |
| 57 | "</original>": "[/original]", |
| 58 | "</patched>": "[/patched]", |
| 59 | } |
| 60 | for key, value in replace_dict.items(): |
| 61 | content = content.replace(key, value) |
| 62 | return content |
| 63 | |
| 64 | |
| 65 | def print_acr( |
| 66 | msg: str, desc="", print_callback: Callable[[dict], None] | None = None |
| 67 | ) -> None: |
| 68 | if not print_stdout: |
| 69 | return |
| 70 | |
| 71 | msg = replace_html_tags(msg) |
| 72 | markdown = Markdown(msg) |
| 73 | |
| 74 | name = "AutoCodeRover" |
| 75 | if desc: |
| 76 | title = f"{name} ({desc})" |
| 77 | else: |
| 78 | title = name |
| 79 | |
| 80 | panel = Panel( |
| 81 | markdown, title=title, title_align="left", border_style="magenta", width=WIDTH |
| 82 | ) |
| 83 | console.print(panel) |
| 84 | |
| 85 | if print_callback: |
| 86 | print_callback( |
| 87 | {"title": f"{name} ({desc})", "message": msg, "category": "auto_code_rover"} |
| 88 | ) |
| 89 | |
| 90 | |
| 91 | def print_retrieval( |
| 92 | msg: str, desc="", print_callback: Callable[[dict], None] | None = None |
| 93 | ) -> None: |
| 94 | if not print_stdout: |
| 95 | return |
| 96 | |
| 97 | msg = replace_html_tags(msg) |
| 98 | markdown = Markdown(msg) |
| 99 | |
| 100 | name = "Context Retrieval Agent" |
| 101 | if desc: |
| 102 | title = f"{name} ({desc})" |
| 103 | else: |
| 104 | title = name |
| 105 | |
| 106 | panel = Panel( |
| 107 | markdown, title=title, title_align="left", border_style="blue", width=WIDTH |
| 108 | ) |
| 109 | console.print(panel) |
| 110 | if print_callback: |
| 111 | print_callback( |
| 112 | { |
| 113 | "title": f"{name} ({desc})", |
| 114 | "message": msg, |
| 115 | "category": "context_retrieval_agent", |
| 116 | } |
| 117 | ) |
| 118 | |
| 119 | |
| 120 | def print_patch_generation( |
| 121 | msg: str, desc="", print_callback: Callable[[dict], None] | None = None |
| 122 | ) -> None: |
| 123 | if not print_stdout: |
| 124 | return |
| 125 | |
| 126 | msg = replace_html_tags(msg) |
| 127 | markdown = Markdown(msg) |
| 128 | |
| 129 | name = "Patch Generation" |
| 130 | if desc: |
| 131 | title = f"{name} ({desc})" |
| 132 | else: |
| 133 | title = name |
| 134 | |
| 135 | panel = Panel( |
| 136 | markdown, title=title, title_align="left", border_style="yellow", width=WIDTH |
| 137 | ) |
| 138 | console.print(panel) |
| 139 | if print_callback: |
| 140 | print_callback( |
| 141 | { |
| 142 | "title": f"{name} ({desc})", |
| 143 | "message": msg, |
| 144 | "category": "patch_generation", |
| 145 | } |
| 146 | ) |
| 147 | |
| 148 | |
| 149 | def print_fix_loc_generation( |
| 150 | msg: str, desc="", print_callback: Callable[[dict], None] | None = None |
| 151 | ) -> None: |
| 152 | if not print_stdout: |
| 153 | return |
| 154 | |
| 155 | msg = replace_html_tags(msg) |
| 156 | markdown = Markdown(msg) |
| 157 | |
| 158 | name = "Fix Location Generation" |
| 159 | if desc: |
| 160 | title = f"{name} ({desc})" |
| 161 | else: |
| 162 | title = name |
| 163 | |
| 164 | panel = Panel( |
| 165 | markdown, title=title, title_align="left", border_style="green", width=WIDTH |
| 166 | ) |
| 167 | console.print(panel) |
| 168 | if print_callback: |
| 169 | print_callback( |
| 170 | { |
| 171 | "title": f"{name} ({desc})", |
| 172 | "message": msg, |
| 173 | "category": "fix_loc_generation", |
| 174 | } |
| 175 | ) |
| 176 | |
| 177 | |
| 178 | def print_issue(content: str) -> None: |
| 179 | if not print_stdout: |
| 180 | return |
| 181 | |
| 182 | title = "Issue description" |
| 183 | panel = Panel( |
| 184 | escape(content), |
| 185 | title=title, |
| 186 | title_align="left", |
| 187 | border_style="red", |
| 188 | width=WIDTH, |
| 189 | ) |
| 190 | console.print(panel) |
| 191 | |
| 192 | |
| 193 | def log_and_print(msg): |
| 194 | logger.info(msg) |
| 195 | if print_stdout: |
| 196 | console.print(msg) |
| 197 | |
| 198 | |
| 199 | def log_and_cprint(msg, **kwargs): |
| 200 | logger.info(msg) |
| 201 | if print_stdout: |
| 202 | console.print(msg, **kwargs) |
| 203 | |
| 204 | |
| 205 | def log_and_always_print(msg): |
| 206 | """ |
| 207 | A mode which always print to stdout, no matter what. |
| 208 | Useful when running multiple tasks and we just want to see the important information. |
| 209 | """ |
| 210 | logger.info(msg) |
| 211 | # always include time for important messages |
| 212 | t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) |
| 213 | console.print(f"\n[{t}] {msg}") |
| 214 | |
| 215 | |
| 216 | def print_with_time(msg): |
| 217 | """ |
| 218 | Print a msg to console with timestamp. |
| 219 | """ |
| 220 | t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) |
| 221 | console.print(f"\n[{t}] {msg}") |