Skip to content

Commit a7337d7

Browse files
committed
feat(ai): add color-coded diff formatting with progress bar
- Added a new `FormatDiff` function to format git diff output with color-coded lines and a progress bar. - Added a `createProgressBar` function to generate a visual progress bar for diff statistics. - Simplified the `diff` command in the UI by delegating diff formatting to the new `FormatDiff` function. - Removed redundant code for manually highlighting diff lines in the UI. Signed-off-by: codiing-hui <wecoding@yeah.net>
1 parent bebc880 commit a7337d7

File tree

2 files changed

+74
-18
lines changed

2 files changed

+74
-18
lines changed

internal/git/git.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,72 @@ func (c *Command) commit(val string) *exec.Cmd {
241241
args...,
242242
)
243243
}
244+
245+
// FormatDiff formats git diff output with color and stats
246+
func (c *Command) FormatDiff(diffOutput string) string {
247+
lines := strings.Split(diffOutput, "\n")
248+
var formattedLines []string
249+
var stats struct {
250+
added int
251+
removed int
252+
total int
253+
}
254+
255+
// First pass to calculate stats
256+
for _, line := range lines {
257+
switch {
258+
case strings.HasPrefix(line, "+"):
259+
stats.added++
260+
stats.total++
261+
case strings.HasPrefix(line, "-"):
262+
stats.removed++
263+
stats.total++
264+
case strings.HasPrefix(line, "@@"):
265+
stats.total++
266+
}
267+
}
268+
269+
// Create progress bar
270+
progress := c.createProgressBar(stats.added, stats.removed, stats.total)
271+
272+
// Second pass to format lines
273+
for _, line := range lines {
274+
switch {
275+
case strings.HasPrefix(line, "+"):
276+
formattedLines = append(formattedLines,
277+
fmt.Sprintf("\x1b[32m%s\x1b[0m", line)) // Green for additions
278+
case strings.HasPrefix(line, "-"):
279+
formattedLines = append(formattedLines,
280+
fmt.Sprintf("\x1b[31m%s\x1b[0m", line)) // Red for deletions
281+
case strings.HasPrefix(line, "@@"):
282+
formattedLines = append(formattedLines,
283+
fmt.Sprintf("\x1b[33m%s\x1b[0m", line)) // Yellow for headers
284+
default:
285+
formattedLines = append(formattedLines, line)
286+
}
287+
}
288+
289+
// Add header with stats
290+
header := fmt.Sprintf("Changes (%d added, %d removed):\n%s\n",
291+
stats.added, stats.removed, progress)
292+
293+
return header + strings.Join(formattedLines, "\n")
294+
}
295+
296+
// createProgressBar generates a visual progress bar for diff stats
297+
func (c *Command) createProgressBar(added, removed, total int) string {
298+
const barWidth = 30
299+
if total == 0 {
300+
return ""
301+
}
302+
303+
addedBlocks := int(float64(added) / float64(total) * barWidth)
304+
removedBlocks := int(float64(removed) / float64(total) * barWidth)
305+
unchangedBlocks := barWidth - addedBlocks - removedBlocks
306+
307+
bar := strings.Repeat("█", addedBlocks) +
308+
strings.Repeat("░", removedBlocks) +
309+
strings.Repeat(" ", unchangedBlocks)
310+
311+
return fmt.Sprintf("[%s] %d changes", bar, total)
312+
}

internal/ui/coders/commands.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -351,27 +351,14 @@ func (c *CommandExecutor) diff(ctx context.Context, _ ...string) error {
351351
return errbook.Wrap("Failed to get diff", err)
352352
}
353353

354-
// Split and highlight diff lines
355-
lines := strings.Split(diffOutput, "\n")
356-
var highlightedLines []string
357-
358-
for _, line := range lines {
359-
switch {
360-
case strings.HasPrefix(line, "+"):
361-
highlightedLines = append(highlightedLines, console.StdoutStyles().CommitSuccess.Render(line))
362-
case strings.HasPrefix(line, "-"):
363-
highlightedLines = append(highlightedLines, console.StdoutStyles().InlineCode.Render(line))
364-
case strings.HasPrefix(line, "@@"):
365-
highlightedLines = append(highlightedLines, console.StdoutStyles().Flag.Render(line))
366-
default:
367-
highlightedLines = append(highlightedLines, line)
368-
}
369-
}
370-
371-
console.Render("Changes:\n%s", strings.Join(highlightedLines, "\n"))
354+
// Process and format the diff
355+
formattedDiff := c.coder.repo.FormatDiff(diffOutput)
356+
console.Render("Changes:\n%s", formattedDiff)
372357
return nil
373358
}
374359

360+
361+
375362
func (c *CommandExecutor) exit(_ context.Context, _ ...string) error {
376363
fmt.Println("Bye!")
377364
os.Exit(0)

0 commit comments

Comments
 (0)