diff --git a/.tasks/2025-10-22_1_improve-compare-output-format.md b/.tasks/2025-10-22_1_improve-compare-output-format.md deleted file mode 100644 index a3de6aa..0000000 --- a/.tasks/2025-10-22_1_improve-compare-output-format.md +++ /dev/null @@ -1,60 +0,0 @@ -# 背景 -文件名:2025-10-22_1_improve-compare-output-format.md -创建于:2025-10-22_23:39:29 -创建者:用户 -主分支:main -任务分支:main -Yolo模式:Off - -# 任务描述 -改进 `-c` 参数的文本对比输出格式,使其能够: -1. 显示行号 -2. 显示每行的修改状态符号(+代表只有add,-代表只有delete,@代表既有add又有delete,=代表无修改) -3. 在分组间添加分隔符(如横线) -4. 保持现有的颜色功能 - -当前输出格式: -``` -4123 -i get 13 history -i get 24 history -123 -operations: -add "4" on [0,0] -add "3 -i get 1" on [2,2] -add " history -i get 2" on [3,3] -add " history -123" on [4,4] -``` - -期望输出格式: -``` -+1|4123 -+2|i get 13 history -+3|i get 24 history -+4|123 ----- -``` - -# 项目概览 -这是一个基于Python的文本版本控制系统,使用 `-c` 参数进行文本对比。项目使用Convention模块进行配置管理和颜色输出。 - -# 分析 -当前 `compare` 方法在 `app.py` 第151-186行,使用 `levenshtein_distance_with_operations` 函数计算差异。需要: -1. 修改输出逻辑,将操作转换为按行显示 -2. 为每行添加符号和行号 -3. 添加分组分隔符 -4. 保持颜色功能 - -# 提议的解决方案 -[待填写] - -# 当前执行步骤:"1. 分析现有代码结构" - -# 任务进度 -[待填写] - -# 最终审查 -[待填写] diff --git a/app.py b/app.py index 96af61f..660e793 100644 --- a/app.py +++ b/app.py @@ -127,7 +127,20 @@ def levenshtein_distance_with_operations(s1:str, s2:str) -> Tuple[int, List[Tupl class Cli: def print_out(self) -> None: - print(self.prints, ConsoleFrontColor.RESET) + sp = self.prints.split("\n") + status_len = len(str(len(sp))) + for line_index, line in enumerate(sp): + if ConsoleFrontColor.GREEN in line and ConsoleFrontColor.RED in line: + perfix = f"{ConsoleFrontColor.YELLOW}@" + elif ConsoleFrontColor.GREEN in line: + perfix = f"{ConsoleFrontColor.GREEN}+" + elif ConsoleFrontColor.RED in line: + perfix = f"{ConsoleFrontColor.RED}-" + else: + perfix = "=" + + print(f"{perfix}{line_index}{" "*max(0,status_len-len(str(line_index)))}{ConsoleFrontColor.RESET} | {line}") + print(ConsoleFrontColor.RESET) def print(self, *args) -> None: self.prints += "".join(args) @@ -156,34 +169,36 @@ class Cli: current_content = self.file.LoadAsText() step, operations = levenshtein_distance_with_operations(head_commit, current_content) if step == 0: - self.print(f"{ConsoleFrontColor.LIGHTMAGENTA_EX}No changes") + PrintColorful(ConsoleFrontColor.LIGHTMAGENTA_EX, "No changes") return index = 0 for operation in operations: # 显示操作前的不变内容 - sp = head_commit[index:operation[1]].split("\n") - if len(sp) > 3: - self.print("\n".join(sp[-3:])) - else: - self.print("\n".join(sp)) + self.print(head_commit[index:operation[1]]) if operation[0] == "add": - self.print(f"{ConsoleFrontColor.GREEN}{operation[3]}{ConsoleFrontColor.RESET}") - index = operation[2] + color = ConsoleFrontColor.GREEN elif operation[0] == "delete": - self.print(f"{ConsoleFrontColor.RED}{operation[3]}{ConsoleFrontColor.RESET}") - index = operation[2] + color = ConsoleFrontColor.RED else: raise ValueError(f"Invalid operation: {operation}") - sp = head_commit[index:].split("\n") - if len(sp) > 3: - self.print("\n".join(sp[:3])) - else: - self.print("\n".join(sp)) + + sp = operation[3].split("\n") + if operation[3][0] == "\n": + self.print("\n") + for line_index, line in enumerate(sp): + self.print(f"{color}{line}{ConsoleFrontColor.RESET}") + if line_index == len(sp) - 1: + if operation[3][-1] == "\n": + self.print("\n") + else: + self.print("\n") + index = operation[2] + self.print(head_commit[index:]) + self.print_out() - - self.print(f"\n{ConsoleFrontColor.LIGHTMAGENTA_EX}operations:\n") - self.print(f"{'\n'.join([f"{ConsoleFrontColor.GREEN if item[0] == "add" else ConsoleFrontColor.RED}{item[0]}{ConsoleFrontColor.RESET} \"{item[3]}\" on [{item[1]},{item[2]}]" for item in operations])}") + PrintColorful(ConsoleFrontColor.LIGHTMAGENTA_EX, "\noperations:") + print(f"{'\n'.join([f"{ConsoleFrontColor.GREEN if item[0] == "add" else ConsoleFrontColor.RED}{item[0]}{ConsoleFrontColor.RESET} \"{ConsoleFrontColor.YELLOW}{item[3]}{ConsoleFrontColor.RESET}\" on [{item[1]},{item[2]}]" for item in operations])}") def save(self) -> None: @@ -239,17 +254,11 @@ def run() -> int: # 比较 if args.compare: - try: - cli.compare() - finally: - cli.print_out() + cli.compare() return 0 # 保存 elif args.save: - try: - cli.save() - finally: - cli.print_out() + cli.save() return 0 raise NotImplementedError("Not implemented mode")