From ff62c39feb0b264d68136c0b90069c44d50cfdff Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 12:49:37 -0700 Subject: [PATCH] aider: Added colorization to `cmd_diff` method when `self.pretty` is True. # Context: USER: in cmd_diff, check self.pretty and colorize the diff if it's true ASSISTANT: To colorize the diff output in the `cmd_diff` method when `self.pretty` is True, we will use the `rich.syntax` module to highlight the diff. ```python aider/coder.py <<<<<<< ORIGINAL from rich.console import Console from rich.prompt import Confirm, Prompt from rich.live import Live from rich.text import Text from rich.markdown import Markdown ======= from rich.console import Console from rich.prompt import Confirm, Prompt from rich.live import Live from rich.text import Text from rich.markdown import Markdown from rich.syntax import Syntax >>>>>>> UPDATED aider/coder.py <<<<<<< ORIGINAL def cmd_diff(self, args): "Display the diff of the last aider commit" if not self.repo: self.console.print("[red]No git repository found.") return if not self.last_aider_commit_hash: self.console.print("[red]No previous aider commit found.") return diff = self.repo.git.diff(f"{self.last_aider_commit_hash}~1", self.last_aider_commit_hash) self.console.print(Text(diff)) ======= def cmd_diff(self, args): "Display the diff of the last aider commit" if not self.repo: self.console.print("[red]No git repository found.") return if not self.last_aider_commit_hash: self.console.print("[red]No previous aider commit found.") return diff = self.repo.git.diff(f"{self.last_aider_commit_hash}~1", self.last_aider_commit_hash) if self.pretty: syntax_diff = Syntax(diff, "diff", theme="monokai", line_numbers=False) self.console.print(syntax_diff) else: self.console.print(Text(diff)) >>>>>>> UPDATED ``` Now, the `cmd_diff` method will colorize the diff output when `self.pretty` is True. --- aider/coder.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index 42190efaa..52fc39ee7 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -7,10 +7,10 @@ import traceback from rich.console import Console from rich.prompt import Confirm, Prompt - from rich.live import Live from rich.text import Text from rich.markdown import Markdown +from rich.syntax import Syntax from pathlib import Path @@ -530,8 +530,11 @@ class Coder: return diff = self.repo.git.diff(f"{self.last_aider_commit_hash}~1", self.last_aider_commit_hash) - self.console.print(Text(diff)) - + if self.pretty: + syntax_diff = Syntax(diff, "diff", theme="monokai", line_numbers=False) + self.console.print(syntax_diff) + else: + self.console.print(Text(diff)) def cmd_add(self, args): "Add matching files to the chat"