Skip to content

Commit a129e12

Browse files
committed
feat: show git diff in reverted messages
1 parent c0ee6a6 commit a129e12

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

packages/tui/internal/components/chat/messages.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"log/slog"
77
"slices"
8+
"sort"
9+
"strconv"
810
"strings"
911

1012
tea "github.com/charmbracelet/bubbletea/v2"
@@ -14,6 +16,7 @@ import (
1416
"github.com/sst/opencode/internal/app"
1517
"github.com/sst/opencode/internal/commands"
1618
"github.com/sst/opencode/internal/components/dialog"
19+
"github.com/sst/opencode/internal/components/diff"
1720
"github.com/sst/opencode/internal/components/toast"
1821
"github.com/sst/opencode/internal/layout"
1922
"github.com/sst/opencode/internal/styles"
@@ -569,6 +572,36 @@ func (m *messagesComponent) renderView() tea.Cmd {
569572
hint += revertedStyle.Render(" (or /redo) to restore")
570573

571574
content += "\n" + hint
575+
if m.app.Session.Revert.Diff != "" {
576+
t := theme.CurrentTheme()
577+
s := styles.NewStyle().Background(t.BackgroundPanel())
578+
green := s.Foreground(t.Success()).Render
579+
red := s.Foreground(t.Error()).Render
580+
content += "\n"
581+
stats, err := diff.ParseStats(m.app.Session.Revert.Diff)
582+
if err != nil {
583+
slog.Error("Failed to parse diff stats", "error", err)
584+
} else {
585+
var files []string
586+
for file := range stats {
587+
files = append(files, file)
588+
}
589+
sort.Strings(files)
590+
591+
for _, file := range files {
592+
fileStats := stats[file]
593+
display := file
594+
if fileStats.Added > 0 {
595+
display += green(" +" + strconv.Itoa(int(fileStats.Added)))
596+
}
597+
if fileStats.Removed > 0 {
598+
display += red(" -" + strconv.Itoa(int(fileStats.Removed)))
599+
}
600+
content += "\n" + display
601+
}
602+
}
603+
}
604+
572605
content = styles.NewStyle().
573606
Background(t.BackgroundPanel()).
574607
Width(width - 6).
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package diff
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
type DiffStats struct {
10+
Added int
11+
Removed int
12+
Modified int
13+
}
14+
15+
func ParseStats(diff string) (map[string]DiffStats, error) {
16+
stats := make(map[string]DiffStats)
17+
var currentFile string
18+
scanner := bufio.NewScanner(strings.NewReader(diff))
19+
20+
for scanner.Scan() {
21+
line := scanner.Text()
22+
if strings.HasPrefix(line, "---") {
23+
continue
24+
} else if strings.HasPrefix(line, "+++") {
25+
parts := strings.SplitN(line, " ", 2)
26+
if len(parts) == 2 {
27+
currentFile = strings.TrimPrefix(parts[1], "b/")
28+
}
29+
continue
30+
}
31+
if strings.HasPrefix(line, "@@") {
32+
continue
33+
}
34+
if currentFile == "" {
35+
continue
36+
}
37+
38+
fileStats := stats[currentFile]
39+
switch {
40+
case strings.HasPrefix(line, "+"):
41+
fileStats.Added++
42+
case strings.HasPrefix(line, "-"):
43+
fileStats.Removed++
44+
}
45+
stats[currentFile] = fileStats
46+
}
47+
48+
if err := scanner.Err(); err != nil {
49+
return nil, fmt.Errorf("error reading diff string: %w", err)
50+
}
51+
52+
for file, fileStats := range stats {
53+
fileStats.Modified = fileStats.Added + fileStats.Removed
54+
stats[file] = fileStats
55+
}
56+
57+
return stats, nil
58+
}

0 commit comments

Comments
 (0)