1.能够输出行号以及状态的对比结果了2.第二次保存后进行对比报错
This commit is contained in:
@@ -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. 分析现有代码结构"
|
|
||||||
|
|
||||||
# 任务进度
|
|
||||||
[待填写]
|
|
||||||
|
|
||||||
# 最终审查
|
|
||||||
[待填写]
|
|
||||||
61
app.py
61
app.py
@@ -127,7 +127,20 @@ def levenshtein_distance_with_operations(s1:str, s2:str) -> Tuple[int, List[Tupl
|
|||||||
|
|
||||||
class Cli:
|
class Cli:
|
||||||
def print_out(self) -> None:
|
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:
|
def print(self, *args) -> None:
|
||||||
self.prints += "".join(args)
|
self.prints += "".join(args)
|
||||||
@@ -156,34 +169,36 @@ class Cli:
|
|||||||
current_content = self.file.LoadAsText()
|
current_content = self.file.LoadAsText()
|
||||||
step, operations = levenshtein_distance_with_operations(head_commit, current_content)
|
step, operations = levenshtein_distance_with_operations(head_commit, current_content)
|
||||||
if step == 0:
|
if step == 0:
|
||||||
self.print(f"{ConsoleFrontColor.LIGHTMAGENTA_EX}No changes")
|
PrintColorful(ConsoleFrontColor.LIGHTMAGENTA_EX, "No changes")
|
||||||
return
|
return
|
||||||
index = 0
|
index = 0
|
||||||
for operation in operations:
|
for operation in operations:
|
||||||
# 显示操作前的不变内容
|
# 显示操作前的不变内容
|
||||||
sp = head_commit[index:operation[1]].split("\n")
|
self.print(head_commit[index:operation[1]])
|
||||||
if len(sp) > 3:
|
|
||||||
self.print("\n".join(sp[-3:]))
|
|
||||||
else:
|
|
||||||
self.print("\n".join(sp))
|
|
||||||
|
|
||||||
if operation[0] == "add":
|
if operation[0] == "add":
|
||||||
self.print(f"{ConsoleFrontColor.GREEN}{operation[3]}{ConsoleFrontColor.RESET}")
|
color = ConsoleFrontColor.GREEN
|
||||||
index = operation[2]
|
|
||||||
elif operation[0] == "delete":
|
elif operation[0] == "delete":
|
||||||
self.print(f"{ConsoleFrontColor.RED}{operation[3]}{ConsoleFrontColor.RESET}")
|
color = ConsoleFrontColor.RED
|
||||||
index = operation[2]
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Invalid operation: {operation}")
|
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")
|
PrintColorful(ConsoleFrontColor.LIGHTMAGENTA_EX, "\noperations:")
|
||||||
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])}")
|
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:
|
def save(self) -> None:
|
||||||
@@ -239,17 +254,11 @@ def run() -> int:
|
|||||||
|
|
||||||
# 比较
|
# 比较
|
||||||
if args.compare:
|
if args.compare:
|
||||||
try:
|
cli.compare()
|
||||||
cli.compare()
|
|
||||||
finally:
|
|
||||||
cli.print_out()
|
|
||||||
return 0
|
return 0
|
||||||
# 保存
|
# 保存
|
||||||
elif args.save:
|
elif args.save:
|
||||||
try:
|
cli.save()
|
||||||
cli.save()
|
|
||||||
finally:
|
|
||||||
cli.print_out()
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
raise NotImplementedError("Not implemented mode")
|
raise NotImplementedError("Not implemented mode")
|
||||||
|
|||||||
Reference in New Issue
Block a user