1package commits
 2
 3import (
 4	"smoothie/git"
 5	"strings"
 6
 7	"github.com/charmbracelet/bubbles/viewport"
 8	tea "github.com/charmbracelet/bubbletea"
 9	"github.com/dustin/go-humanize"
10)
11
12type Bubble struct {
13	Commits  []git.RepoCommit
14	Margin   int
15	Width    int
16	Height   int
17	viewport viewport.Model
18}
19
20func NewBubble(height int, margin int, width int, rcs []git.RepoCommit) *Bubble {
21	b := &Bubble{
22		Commits:  rcs,
23		viewport: viewport.Model{Height: height - margin, Width: width},
24	}
25	s := ""
26	for _, rc := range rcs {
27		s += b.renderCommit(rc) + "\n"
28	}
29	b.viewport.SetContent(s)
30	return b
31}
32
33func (b *Bubble) Init() tea.Cmd {
34	return nil
35}
36
37func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
38	cmds := make([]tea.Cmd, 0)
39	switch msg := msg.(type) {
40	case tea.WindowSizeMsg:
41		b.viewport.Height = msg.Height - b.Margin
42	case tea.KeyMsg:
43		switch msg.String() {
44		case "up", "k":
45			b.viewport.LineUp(1)
46		case "down", "j":
47			b.viewport.LineDown(1)
48		}
49	}
50	return b, tea.Batch(cmds...)
51}
52
53func (b *Bubble) renderCommit(rc git.RepoCommit) string {
54	s := ""
55	s += commitRepoNameStyle.Render(rc.Name)
56	s += " "
57	s += commitDateStyle.Render(humanize.Time(rc.Commit.Author.When))
58	s += "\n"
59	s += commitCommentStyle.Render(strings.TrimSpace(rc.Commit.Message))
60	s += "\n"
61	s += commitAuthorStyle.Render(rc.Commit.Author.Name)
62	s += " "
63	s += commitAuthorEmailStyle.Render(rc.Commit.Author.Email)
64	s += " "
65	return commitBoxStyle.Width(b.viewport.Width).Render(s)
66}
67
68func (b *Bubble) View() string {
69	return b.viewport.View()
70}