fix(tui): remove unused shutil import; implement width-based auto_diff
Remove the unused `shutil` import from models.py (fixes lint F401). Implement `auto_diff()` with actual terminal-width-based dispatch as spec §30139 requires: terminals >= 120 columns get split (side-by-side) format, narrower terminals get unified diff. Previously the method called `difflib.context_diff()` with a docstring falsely claiming width-based selection. ISSUES CLOSED: #1480
This commit is contained in:
@@ -6,7 +6,6 @@ tool permission requests with diff views and record user decisions.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from enum import StrEnum
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
@@ -140,23 +139,21 @@ class PermissionRequest(BaseModel):
|
||||
return left, right
|
||||
|
||||
def auto_diff(self, *, context_lines: int = 3) -> str:
|
||||
"""Generate a context diff showing only changed lines with context.
|
||||
"""Select unified or split diff based on available terminal width.
|
||||
|
||||
In AUTO mode, the display mode is selected based on available
|
||||
terminal width: unified for narrow terminals, split for wide.
|
||||
Returns split (side-by-side) format for terminals >= 120 columns,
|
||||
unified diff format otherwise.
|
||||
"""
|
||||
import difflib
|
||||
import shutil
|
||||
|
||||
before_lines = (self.before_content or "").splitlines(keepends=True)
|
||||
after_lines = (self.after_content or "").splitlines(keepends=True)
|
||||
diff_lines = difflib.context_diff(
|
||||
before_lines,
|
||||
after_lines,
|
||||
fromfile=f"a/{self.path}",
|
||||
tofile=f"b/{self.path}",
|
||||
n=context_lines,
|
||||
)
|
||||
return "".join(diff_lines)
|
||||
terminal_width = shutil.get_terminal_size().columns
|
||||
if terminal_width >= 120:
|
||||
left, right = self.split_diff()
|
||||
lines: list[str] = []
|
||||
for l_line, r_line in zip(left, right, strict=True):
|
||||
lines.append(f"{l_line:<40} | {r_line}")
|
||||
return "\n".join(lines)
|
||||
return self.unified_diff(context_lines=context_lines)
|
||||
|
||||
def render_diff(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user