遵循 NO_COLOR 环境变量规范

用户将 no-color.org 上的 NO_COLOR 环境变量协议规范粘贴到 aider 聊天界面中。

aider 识别出需要修改哪些文件,并请求用户确认是否进行更改。 然后它会禁用所有颜色和美化打印功能(如果设置了 NO_COLOR 环境变量),使工具符合该规范。

用户随后要求添加测试用例,aider 也一并完成了添加。

$ aider
使用 git 仓库: .git
输入 /help 查看聊天命令。

默认情况下会为输出添加 ANSI 颜色的命令行软件应当检查 NO_COLOR 环境变量,当该变量存在且不为空字符串时(无论其值为何),应阻止添加 ANSI 颜色。

为了实现这个功能,我们需要修改 aider/io.py 文件。请将其设为可读写状态,以便我提供必要的更改。

aider/io.py
要将这些文件添加到聊天中吗? y

很好!现在我将进行必要的修改,以检查 NO_COLOR 环境变量并在其存在且不为空字符串时禁用 ANSI 颜色。

aider/io.py
<<<<<<< ORIGINAL
import os
=======
import os
import os.path
>>>>>>> UPDATED
aider/io.py
<<<<<<< ORIGINAL
    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",
    ):
        self.user_input_color = user_input_color
        self.tool_output_color = tool_output_color
        self.tool_error_color = tool_error_color
        self.input = input
        self.output = output
        self.pretty = pretty
        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=True, no_color=True)
=======
    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
        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.console = Console(force_terminal=not pretty, no_color=not pretty)
>>>>>>> UPDATED

这些修改将使软件检查 NO_COLOR 环境变量,并在其存在且不为空字符串时禁用 ANSI 颜色。

已应用对 aider/io.py 的编辑
已应用对 aider/io.py 的编辑
提交 3218242 aider: 添加了对 NO_COLOR 环境变量的检查以禁用 ANSI 颜色。

添加测试

为了测试 NO_COLOR 环境变量,我们将在 tests/test_io.py 文件中创建一个新的测试函数。由于该文件不存在,我们将首先创建它。

tests/test_io.py
<<<<<<< ORIGINAL
=======
import os
import unittest
from unittest.mock import patch
from aider.io import InputOutput

class TestInputOutput(unittest.TestCase):
    def test_no_color_environment_variable(self):
        with patch.dict(os.environ, {"NO_COLOR": "1"}):
            io = InputOutput()
            self.assertFalse(io.pretty)

if __name__ == "__main__":
    unittest.main()
>>>>>>> UPDATED

这个测试函数用于检查当设置了 NO_COLOR 环境变量时,InputOutput 类的 pretty 属性是否被设置为 False

允许创建新文件 tests/test_io.py? y
将 tests/test_io.py 添加到 git? y
已应用对 tests/test_io.py 的编辑
提交 52a9df5 aider: 添加了测试函数来检查当设置了 NO_COLOR 环境变量时,InputOutput 类的 pretty 属性是否被设置为 False

/run pytest

============================= 测试会话开始 ==============================
平台 darwin – Python 3.11.3, pytest-7.3.1, pluggy-1.0.0
根目录: /Users/gauthier/Projects/aider
收集到 25 个项目

tests/test_coder.py …… [ 24%]
tests/test_commands.py . [ 28%]
tests/test_io.py . [ 32%]
tests/test_main.py …. [ 48%]
tests/test_repomap.py … [ 60%]
tests/test_utils.py ………. [100%]

============================== 25 个测试通过,耗时 1.41 秒 ==============================

是否将输出添加到聊天? y


目录