Skip to content

Commit 9c917fa

Browse files
committed
refactor: improve diff output formatting and rendering
- Add a separator before the file header in the formatted diff output. - Simplify the diff output rendering by directly printing the formatted diff. - Remove redundant empty lines in the `diff` command function. Signed-off-by: codiing-hui <wecoding@yeah.net>
1 parent e9dbd2c commit 9c917fa

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

internal/git/git.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,17 @@ func (c *Command) FormatDiff(diffOutput string) string {
264264
stats := c.calculateDiffStats(lines)
265265
formattedLines := c.formatDiffLines(lines, stats)
266266

267+
// Add separator before file header
268+
if stats.FileHeader != "" {
269+
separator := strings.Repeat("─", 80)
270+
formattedLines = append([]string{
271+
console.StdoutStyles().DiffContext.Render(separator),
272+
"",
273+
}, formattedLines...)
274+
}
275+
267276
header := c.createDiffHeader(stats)
268-
return header + strings.Join(formattedLines, "\n")
277+
return header + "\n" + strings.Join(formattedLines, "\n")
269278
}
270279

271280
// calculateDiffStats calculates statistics from diff lines
@@ -326,15 +335,15 @@ func (c *Command) formatDiffLines(lines []string, stats *DiffStats) []string {
326335
}
327336
case strings.HasPrefix(line, "+"):
328337
formattedLines = append(formattedLines,
329-
console.StdoutStyles().DiffAdded.Render(fmt.Sprintf("%4d + %s", updatedLineNum, line[1:])))
338+
console.StdoutStyles().DiffAdded.Render(fmt.Sprintf("%4d +%s", updatedLineNum, line[1:])))
330339
updatedLineNum++
331340
case strings.HasPrefix(line, "-"):
332341
formattedLines = append(formattedLines,
333-
console.StdoutStyles().DiffRemoved.Render(fmt.Sprintf("%4d - %s", origLineNum, line[1:])))
342+
console.StdoutStyles().DiffRemoved.Render(fmt.Sprintf("%4d -%s", origLineNum, line[1:])))
334343
origLineNum++
335344
case strings.HasPrefix(line, " "):
336345
formattedLines = append(formattedLines,
337-
console.StdoutStyles().DiffContext.Render(fmt.Sprintf("%4d %s", origLineNum, line[1:])))
346+
console.StdoutStyles().DiffContext.Render(fmt.Sprintf("%4d %s", origLineNum, line[1:])))
338347
origLineNum++
339348
updatedLineNum++
340349
default:

internal/ui/coders/commands.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,11 @@ func (c *CommandExecutor) diff(ctx context.Context, _ ...string) error {
352352
}
353353

354354
// Process and format the diff
355-
formattedDiff := c.coder.repo.FormatDiff(diffOutput)
356-
console.Render("Changes:\n%s", formattedDiff)
355+
fmt.Println(c.coder.repo.FormatDiff(diffOutput))
356+
357357
return nil
358358
}
359359

360-
361-
362360
func (c *CommandExecutor) exit(_ context.Context, _ ...string) error {
363361
fmt.Println("Bye!")
364362
os.Exit(0)

0 commit comments

Comments
 (0)