mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-30 09:14:59 +00:00
aider: Add read_text function to InputOutput class and --encoding command-line switch.
# Context: USER: Add a read_text function to the InputOutput class. It takes the file name as argument, and returns the text for that file. Internally, if we get a Unicode error or a FileNotFound error, it prints ToolErrorOutput to notify the user, and then returns None. Add a command-line switch, and dash-dash encoding, which defaults to UTF-8. That's the encoding which will be used by the read_text function. ASSISTANT: To implement the requested changes, we need to modify the following files: 1. `aider/io.py`: To add the `read_text` function to the `InputOutput` class. 2. `aider/main.py`: To add the `--encoding` command-line switch. Let's start with the changes to `aider/io.py`: ```python aider/io.py <<<<<<< ORIGINAL class InputOutput: num_error_outputs = 0 num_user_asks = 0 def __init__( self, pretty=True, yes=False, input_history_file=None, chat_history_file=None, input=None, output=None, user_input_color="blue", tool_output_color=None, tool_error_color="red", ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": pretty = False self.user_input_color = user_input_color if pretty else None self.tool_output_color = tool_output_color if pretty else None self.tool_error_color = tool_error_color if pretty else None self.input = input self.output = output self.pretty = pretty if self.output: self.pretty = False self.yes = yes self.input_history_file = input_history_file if chat_history_file is not None: self.chat_history_file = Path(chat_history_file) else: self.chat_history_file = None if pretty: self.console = Console() else: self.console = Console(force_terminal=False, no_color=True) current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.append_chat_history(f"\n# aider chat started at {current_time}\n\n") ======= class InputOutput: num_error_outputs = 0 num_user_asks = 0 def __init__( self, pretty=True, yes=False, input_history_file=None, chat_history_file=None, input=None, output=None, user_input_color="blue", tool_output_color=None, tool_error_color="red", encoding="utf-8", ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": pretty = False self.user_input_color = user_input_color if pretty else None self.tool_output_color = tool_output_color if pretty else None self.tool_error_color = tool_error_color if pretty else None self.input = input self.output = output self.pretty = pretty if self.output: self.pretty = False self.yes = yes self.input_history_file = input_history_file if chat_history_file is not None: self.chat_history_file = Path(chat_history_file) else: self.chat_history_file = None self.encoding = encoding if pretty: self.console = Console() else: self.console = Console(force_terminal=False, no_color=True) current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.append_chat_history(f"\n# aider chat started at {current_time}\n\n") def read_text(self, filename): try: with open(filename, "r", encoding=self.encoding) as f: return f.read() except (FileNotFoundError, UnicodeError) as e: self.tool_error(str(e)) return None >>>>>>> UPDATED ``` Now, let's add the `--encoding` command-line switch to `aider/main.py`: ```python aider/main.py <<<<<<< ORIGINAL parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable commits when repo is found dirty", ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", default="https://api.openai.com/v1", help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1)", ) ======= parser.add_argument( "--no-dirty-commits", action="store_false", dest="dirty_commits", help="Disable commits when repo is found dirty", ) parser.add_argument( "--encoding", default="utf-8", help="Specify the encoding to use when reading files (default: utf-8)", ) parser.add_argument( "--openai-api-key", metavar="OPENAI_API_KEY", help="Specify the OpenAI API key", env_var="OPENAI_API_KEY", ) parser.add_argument( "--openai-api-base", metavar="OPENAI_API_BASE", default="https://api.openai.com/v1", help="Specify the OpenAI API base endpoint (default: https://api.openai.com/v1)", ) >>>>>>> UPDATED ``` These changes should implement the requested functionality. Please review and let me know if any further changes are needed.
This commit is contained in:
parent
d9f2c32aa9
commit
5f9947a727
2 changed files with 16 additions and 0 deletions
11
aider/io.py
11
aider/io.py
|
@ -100,6 +100,7 @@ class InputOutput:
|
||||||
user_input_color="blue",
|
user_input_color="blue",
|
||||||
tool_output_color=None,
|
tool_output_color=None,
|
||||||
tool_error_color="red",
|
tool_error_color="red",
|
||||||
|
encoding="utf-8",
|
||||||
):
|
):
|
||||||
no_color = os.environ.get("NO_COLOR")
|
no_color = os.environ.get("NO_COLOR")
|
||||||
if no_color is not None and no_color != "":
|
if no_color is not None and no_color != "":
|
||||||
|
@ -124,6 +125,8 @@ class InputOutput:
|
||||||
else:
|
else:
|
||||||
self.chat_history_file = None
|
self.chat_history_file = None
|
||||||
|
|
||||||
|
self.encoding = encoding
|
||||||
|
|
||||||
if pretty:
|
if pretty:
|
||||||
self.console = Console()
|
self.console = Console()
|
||||||
else:
|
else:
|
||||||
|
@ -132,6 +135,14 @@ class InputOutput:
|
||||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
self.append_chat_history(f"\n# aider chat started at {current_time}\n\n")
|
self.append_chat_history(f"\n# aider chat started at {current_time}\n\n")
|
||||||
|
|
||||||
|
def read_text(self, filename):
|
||||||
|
try:
|
||||||
|
with open(filename, "r", encoding=self.encoding) as f:
|
||||||
|
return f.read()
|
||||||
|
except (FileNotFoundError, UnicodeError) as e:
|
||||||
|
self.tool_error(str(e))
|
||||||
|
return None
|
||||||
|
|
||||||
def get_input(self, root, rel_fnames, addable_rel_fnames, commands):
|
def get_input(self, root, rel_fnames, addable_rel_fnames, commands):
|
||||||
if self.pretty:
|
if self.pretty:
|
||||||
style = dict(style=self.user_input_color) if self.user_input_color else dict()
|
style = dict(style=self.user_input_color) if self.user_input_color else dict()
|
||||||
|
|
|
@ -185,6 +185,11 @@ def main(args=None, input=None, output=None):
|
||||||
dest="dirty_commits",
|
dest="dirty_commits",
|
||||||
help="Disable commits when repo is found dirty",
|
help="Disable commits when repo is found dirty",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--encoding",
|
||||||
|
default="utf-8",
|
||||||
|
help="Specify the encoding to use when reading files (default: utf-8)",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--openai-api-key",
|
"--openai-api-key",
|
||||||
metavar="OPENAI_API_KEY",
|
metavar="OPENAI_API_KEY",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue