Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(TUI): display changed files aligned to git diff

ldstyle8736a09133dd7
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index d19f09db73..0820692974 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -1558,13 +1558,12 @@ export class AppScreen implements Component, Focusable {
}
if (this.diffViewerState.viewMode === "list") {
- const visibleFiles = Math.max(1, height - 5);
+ // List view paginates a 5-file centered window derived from
+ // selectedIndex at render time, so navigation only needs to move the
+ // selection; the window follows automatically.
if (matchesKey(data, "up") || data.toLowerCase() === "k") {
if (this.diffViewerState.selectedIndex > 0) {
this.diffViewerState.selectedIndex--;
- if (this.diffViewerState.selectedIndex < this.diffViewerState.scrollOffset) {
- this.diffViewerState.scrollOffset = this.diffViewerState.selectedIndex;
- }
this.tui.requestRender();
}
return;
@@ -1572,9 +1571,6 @@ export class AppScreen implements Component, Focusable {
if (matchesKey(data, "down") || data.toLowerCase() === "j") {
if (this.diffViewerState.selectedIndex < this.diffViewerState.files.length - 1) {
this.diffViewerState.selectedIndex++;
- if (this.diffViewerState.selectedIndex >= this.diffViewerState.scrollOffset + visibleFiles) {
- this.diffViewerState.scrollOffset = this.diffViewerState.selectedIndex - visibleFiles + 1;
- }
this.tui.requestRender();
}
return;
@@ -1590,13 +1586,11 @@ export class AppScreen implements Component, Focusable {
}
if (matchesKey(data, "home") || data.toLowerCase() === "g") {
this.diffViewerState.selectedIndex = 0;
- this.diffViewerState.scrollOffset = 0;
this.tui.requestRender();
return;
}
if (matchesKey(data, "end") || data.toLowerCase() === "shift+g") {
this.diffViewerState.selectedIndex = Math.max(0, this.diffViewerState.files.length - 1);
- this.diffViewerState.scrollOffset = Math.max(0, this.diffViewerState.files.length - visibleFiles);
this.tui.requestRender();
return;
}
@@ -1742,63 +1736,95 @@ export class AppScreen implements Component, Focusable {
lines.push(padToWidth(palette.text.dim(` ${"─".repeat(Math.max(0, safeWidth - 4))}`), safeWidth));
if (this.diffViewerState.viewMode === "list") {
- const availableHeight = Math.max(1, this.tui.terminal.rows - 5);
+ // Paginate to MAX_VISIBLE files with the selected file centered,
+ // mirroring Claude Code's DiffFileList. When there are more files than
+ // the window, show ↑/↓ "N more files" hints above/below the window.
+ const MAX_VISIBLE = 5;
+ const total = this.diffViewerState.files.length;
- if (this.diffViewerState.files.length === 0) {
+ if (total === 0) {
lines.push(padToWidth(palette.text.dim(" No file changes in this session"), safeWidth));
- }
-
- for (let i = this.diffViewerState.scrollOffset; i < this.diffViewerState.files.length; i++) {
- const file = this.diffViewerState.files[i]!;
- const isSelected = i === this.diffViewerState.selectedIndex;
- const pointer = isSelected ? "❯ " : " ";
- const relativePath = this._toRelativePath(file.filePath);
- const displayPath = relativePath.length > safeWidth - 16
- ? relativePath.slice(0, safeWidth - 19) + "..."
- : relativePath;
-
- // 构建右侧状态标签(对齐 Claude Code FileStats):
- // - untracked → "untracked"
- // - binary → "Binary file"
- // - large file → "Large file modified"
- // - normal/truncated → +N -N [ (truncated)]
- let statsLabel: string;
- let statsStyled: string;
- if (file.isUntracked) {
- statsLabel = "untracked";
- statsStyled = palette.text.dim("untracked");
- } else if (file.isBinary) {
- statsLabel = "Binary file";
- statsStyled = palette.text.dim("Binary file");
- } else if (file.isLargeFile) {
- statsLabel = "Large file modified";
- statsStyled = palette.text.dim("Large file modified");
+ } else {
+ let start: number;
+ let end: number;
+ if (total <= MAX_VISIBLE) {
+ start = 0;
+ end = total;
} else {
- const addPart = palette.status.success(`+${file.linesAdded}`);
- const removePart = palette.status.error(`-${file.linesRemoved}`);
- statsLabel = `+${file.linesAdded} -${file.linesRemoved}`;
- statsStyled = `${addPart} ${removePart}`;
- if (file.isTruncated) {
- statsLabel += " (truncated)";
- statsStyled += palette.text.dim(" (truncated)");
+ start = Math.max(0, this.diffViewerState.selectedIndex - Math.floor(MAX_VISIBLE / 2));
+ end = start + MAX_VISIBLE;
+ if (end > total) {
+ end = total;
+ start = Math.max(0, end - MAX_VISIBLE);
}
}
- const sourceLabel = file.isUntracked
- ? "untracked"
- : file.isNewFile
- ? "(new)"
- : file.source;
- const line = `${pointer}${displayPath}`;
- const padded = padToWidth(line, safeWidth - statsLabel.length - sourceLabel.length - 3);
- const fullLine = `${padded}${palette.text.dim(sourceLabel)} ${statsStyled}`;
- if (isSelected) {
- lines.push(palette.text.accent(fullLine));
- } else {
- lines.push(fullLine);
+ if (start > 0) {
+ const more = start;
+ lines.push(padToWidth(
+ palette.text.dim(` ↑ ${more} more ${more === 1 ? "file" : "files"}`),
+ safeWidth,
+ ));
+ }
+
+ for (let i = start; i < end; i++) {
+ const file = this.diffViewerState.files[i]!;
+ const isSelected = i === this.diffViewerState.selectedIndex;
+ const pointer = isSelected ? "❯ " : " ";
+ const relativePath = this._toRelativePath(file.filePath);
+ const displayPath = relativePath.length > safeWidth - 16
+ ? relativePath.slice(0, safeWidth - 19) + "..."
+ : relativePath;
+
+ // 构建右侧状态标签(对齐 Claude Code FileStats):
+ // - untracked → "untracked"
+ // - binary → "Binary file"
+ // - large file → "Large file modified"
+ // - normal/truncated → +N -N [ (truncated)]
+ let statsLabel: string;
+ let statsStyled: string;
+ if (file.isUntracked) {
+ statsLabel = "untracked";
+ statsStyled = palette.text.dim("untracked");
+ } else if (file.isBinary) {
+ statsLabel = "Binary file";
+ statsStyled = palette.text.dim("Binary file");
+ } else if (file.isLargeFile) {
+ statsLabel = "Large file modified";
+ statsStyled = palette.text.dim("Large file modified");
+ } else {
+ const addPart = palette.status.success(`+${file.linesAdded}`);
+ const removePart = palette.status.error(`-${file.linesRemoved}`);
+ statsLabel = `+${file.linesAdded} -${file.linesRemoved}`;
+ statsStyled = `${addPart} ${removePart}`;
+ if (file.isTruncated) {
+ statsLabel += " (truncated)";
+ statsStyled += palette.text.dim(" (truncated)");
+ }
+ }
+
+ const sourceLabel = file.isUntracked
+ ? "untracked"
+ : file.isNewFile
+ ? "(new)"
+ : file.source;
+ const line = `${pointer}${displayPath}`;
+ const padded = padToWidth(line, safeWidth - statsLabel.length - sourceLabel.length - 3);
+ const fullLine = `${padded}${palette.text.dim(sourceLabel)} ${statsStyled}`;
+ if (isSelected) {
+ lines.push(palette.text.accent(fullLine));
+ } else {
+ lines.push(fullLine);
+ }
}
- if (lines.length >= availableHeight + 4) break;
+ if (end < total) {
+ const more = total - end;
+ lines.push(padToWidth(
+ palette.text.dim(` ↓ ${more} more ${more === 1 ? "file" : "files"}`),
+ safeWidth,
+ ));
+ }
}
} else {
const file = this.diffViewerState.files[this.diffViewerState.selectedIndex];
diff --git a/jiuwenswarm/server/utils/diff_service.py b/jiuwenswarm/server/utils/diff_service.py
index 7e1bb2a363..fd07fd4504 100644
--- a/jiuwenswarm/server/utils/diff_service.py
+++ b/jiuwenswarm/server/utils/diff_service.py
@@ -382,44 +382,114 @@ class DiffService:
if not old_lines and not new_lines:
return [], False
- matcher = difflib.SequenceMatcher(None, old_lines, new_lines)
+ # Emit unified hunks with context_lines of surrounding context and
+ # merge adjacent changes whose context windows overlap, matching
+ # `git diff --unified=3` / jsdiff's structuredPatch. The previous
+ # implementation skipped equal opcodes entirely, producing context-less
+ # isolated hunks that showed far less content than `git diff`.
+ context_lines = 3
+ opcodes = difflib.SequenceMatcher(
+ None, old_lines, new_lines
+ ).get_opcodes()
+ n_old = len(old_lines)
+ n_new = len(new_lines)
+
hunks: list[dict[str, Any]] = []
total_lines = 0
truncated = False
- for tag, i1, i2, j1, j2 in matcher.get_opcodes():
+ i = 0
+ while i < len(opcodes):
+ tag, i1, i2, j1, j2 = opcodes[i]
if tag == "equal":
+ i += 1
continue
- if total_lines >= max_lines:
- truncated = True
- break
-
- old_start = i1 + 1
- old_count = i2 - i1
- new_start = j1 + 1
- new_count = j2 - j1
+ # First change of this hunk is at opcodes[i]; absorb following
+ # changes whose separating equal run is short enough that their
+ # context windows bridge the gap (run length <= 2*context_lines).
+ change_start = i
+ o_lo = max(0, i1 - context_lines)
+ n_lo = max(0, j1 - context_lines)
+ last_i2 = i2
+ last_j2 = j2
+ k = i + 1
+ while k < len(opcodes):
+ ntag, ni1, ni2, nj1, nj2 = opcodes[k]
+ if ntag == "equal":
+ if (ni2 - ni1) > 2 * context_lines:
+ break
+ k += 1
+ continue
+ last_i2 = ni2
+ last_j2 = nj2
+ k += 1
+
+ o_hi = min(n_old, last_i2 + context_lines)
+ n_hi = min(n_new, last_j2 + context_lines)
+
+ # Include the leading equal opcode (i-1) and the trailing equal
+ # opcode (k, when present) so leading/trailing context lines are
+ # emitted; both are clamped to the window below.
+ start_idx = (
+ i - 1 if i - 1 >= 0 and opcodes[i - 1][0] == "equal" else i
+ )
+ end_idx = (
+ k + 1
+ if k < len(opcodes) and opcodes[k][0] == "equal"
+ else k
+ )
lines: list[str] = []
- remaining = max_lines - total_lines
-
- for k in range(i1, min(i2, i1 + remaining)):
- lines.append(f"-{old_lines[k].rstrip()}")
- total_lines += 1
-
- for k in range(j1, min(j2, j1 + max(0, remaining - (i2 - i1)))):
- if total_lines >= max_lines:
+ for idx in range(start_idx, end_idx):
+ tag2, ii1, ii2, jj1, jj2 = opcodes[idx]
+ if tag2 == "equal":
+ for m in range(max(ii1, o_lo), min(ii2, o_hi)):
+ if total_lines >= max_lines:
+ truncated = True
+ break
+ lines.append(f" {old_lines[m].rstrip()}")
+ total_lines += 1
+ elif tag2 == "delete":
+ for m in range(max(ii1, o_lo), min(ii2, o_hi)):
+ if total_lines >= max_lines:
+ truncated = True
+ break
+ lines.append(f"-{old_lines[m].rstrip()}")
+ total_lines += 1
+ elif tag2 == "insert":
+ for m in range(max(jj1, n_lo), min(jj2, n_hi)):
+ if total_lines >= max_lines:
+ truncated = True
+ break
+ lines.append(f"+{new_lines[m].rstrip()}")
+ total_lines += 1
+ else: # replace
+ for m in range(max(ii1, o_lo), min(ii2, o_hi)):
+ if total_lines >= max_lines:
+ truncated = True
+ break
+ lines.append(f"-{old_lines[m].rstrip()}")
+ total_lines += 1
+ for m in range(max(jj1, n_lo), min(jj2, n_hi)):
+ if total_lines >= max_lines:
+ truncated = True
+ break
+ lines.append(f"+{new_lines[m].rstrip()}")
+ total_lines += 1
+ if truncated:
break
- lines.append(f"+{new_lines[k].rstrip()}")
- total_lines += 1
hunks.append({
- "oldStart": old_start,
- "oldLines": old_count,
- "newStart": new_start,
- "newLines": new_count,
+ "oldStart": o_lo + 1,
+ "oldLines": o_hi - o_lo,
+ "newStart": n_lo + 1,
+ "newLines": n_hi - n_lo,
"lines": lines,
})
+ if truncated:
+ break
+ i = k
return hunks, truncated