bubble.go

  1package repo
  2
  3import (
  4	"bytes"
  5	"fmt"
  6	"soft-serve/git"
  7	"soft-serve/tui/style"
  8	"strconv"
  9	"text/template"
 10
 11	"github.com/charmbracelet/bubbles/viewport"
 12	tea "github.com/charmbracelet/bubbletea"
 13	"github.com/charmbracelet/glamour"
 14	"github.com/charmbracelet/lipgloss"
 15	"github.com/muesli/reflow/truncate"
 16	"github.com/muesli/reflow/wordwrap"
 17	"github.com/muesli/reflow/wrap"
 18)
 19
 20const (
 21	glamourMaxWidth  = 120
 22	repoNameMaxWidth = 32
 23)
 24
 25type ErrMsg struct {
 26	Error error
 27}
 28
 29type Bubble struct {
 30	templateObject interface{}
 31	repoSource     *git.RepoSource
 32	name           string
 33	repo           *git.Repo
 34	styles         *style.Styles
 35	readmeViewport *ViewportBubble
 36	readme         string
 37	height         int
 38	heightMargin   int
 39	width          int
 40	widthMargin    int
 41	Active         bool
 42
 43	// XXX: ideally, we get these from the parent as a pointer. Currently, we
 44	// can't add a *tui.Config because it's an illegal import cycle. One
 45	// solution would be to (rename and) move this Bubble into the parent
 46	// package.
 47	Host string
 48	Port int64
 49}
 50
 51func NewBubble(rs *git.RepoSource, name string, styles *style.Styles, width, wm, height, hm int, tmp interface{}) *Bubble {
 52	b := &Bubble{
 53		templateObject: tmp,
 54		repoSource:     rs,
 55		name:           name,
 56		styles:         styles,
 57		heightMargin:   hm,
 58		widthMargin:    wm,
 59		readmeViewport: &ViewportBubble{
 60			Viewport: &viewport.Model{},
 61		},
 62	}
 63	b.SetSize(width, height)
 64	return b
 65}
 66
 67func (b *Bubble) Init() tea.Cmd {
 68	return b.setupCmd
 69}
 70
 71func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 72	var cmds []tea.Cmd
 73	switch msg := msg.(type) {
 74	case tea.WindowSizeMsg:
 75		b.SetSize(msg.Width, msg.Height)
 76		// XXX: if we find that longer readmes take more than a few
 77		// milliseconds to render we may need to move Glamour rendering into a
 78		// command.
 79		md, err := b.glamourize(b.readme)
 80		if err != nil {
 81			return b, nil
 82		}
 83		b.readmeViewport.Viewport.SetContent(md)
 84	}
 85	rv, cmd := b.readmeViewport.Update(msg)
 86	b.readmeViewport = rv.(*ViewportBubble)
 87	cmds = append(cmds, cmd)
 88	return b, tea.Batch(cmds...)
 89}
 90
 91func (b *Bubble) SetSize(w, h int) {
 92	b.width = w
 93	b.height = h
 94	b.readmeViewport.Viewport.Width = w - b.widthMargin
 95	b.readmeViewport.Viewport.Height = h - lipgloss.Height(b.headerView()) - b.heightMargin
 96}
 97
 98func (b *Bubble) GotoTop() {
 99	b.readmeViewport.Viewport.GotoTop()
100}
101
102func (b Bubble) headerView() string {
103	ts := b.styles.RepoTitle
104	ns := b.styles.RepoNote
105	if b.Active {
106		ts = ts.Copy().BorderForeground(b.styles.ActiveBorderColor)
107		ns = ns.Copy().BorderForeground(b.styles.ActiveBorderColor)
108	}
109	var gc string
110	n := truncate.StringWithTail(b.name, repoNameMaxWidth, "…")
111	if n == "config" {
112		n = "Home"
113	} else {
114		gc = fmt.Sprintf("git clone %s", b.sshAddress())
115	}
116	title := ts.Render(n)
117	note := ns.Width(b.width - b.widthMargin - lipgloss.Width(title)).Render(gc)
118	return lipgloss.JoinHorizontal(lipgloss.Top, title, note)
119}
120
121func (b *Bubble) View() string {
122	header := b.headerView()
123	bs := b.styles.RepoBody.Copy()
124	if b.Active {
125		bs = bs.BorderForeground(b.styles.ActiveBorderColor)
126	}
127	body := bs.
128		Width(b.width - b.widthMargin - b.styles.RepoBody.GetVerticalFrameSize()).
129		Height(b.height - b.heightMargin - lipgloss.Height(header)).
130		Render(b.readmeViewport.View())
131	return header + body
132}
133
134func (b Bubble) sshAddress() string {
135	p := ":" + strconv.Itoa(int(b.Port))
136	if p == ":22" {
137		p = ""
138	}
139	return fmt.Sprintf("ssh://%s%s/%s", b.Host, p, b.name)
140}
141
142func (b *Bubble) setupCmd() tea.Msg {
143	r, err := b.repoSource.GetRepo(b.name)
144	if err == git.ErrMissingRepo {
145		return nil
146	}
147	if err != nil {
148		return ErrMsg{err}
149	}
150	md := r.Readme
151	if b.templateObject != nil {
152		md, err = b.templatize(md)
153		if err != nil {
154			return ErrMsg{err}
155		}
156	}
157	b.readme = md
158	md, err = b.glamourize(md)
159	if err != nil {
160		return ErrMsg{err}
161	}
162	b.readmeViewport.Viewport.SetContent(md)
163	b.GotoTop()
164	return nil
165}
166
167func (b *Bubble) templatize(mdt string) (string, error) {
168	t, err := template.New("readme").Parse(mdt)
169	if err != nil {
170		return "", err
171	}
172	buf := &bytes.Buffer{}
173	err = t.Execute(buf, b.templateObject)
174	if err != nil {
175		return "", err
176	}
177	return buf.String(), nil
178}
179
180func (b *Bubble) glamourize(md string) (string, error) {
181	// TODO: read gaps in appropriate style to remove the magic number below.
182	w := b.width - b.widthMargin - 4
183	if w > glamourMaxWidth {
184		w = glamourMaxWidth
185	}
186	tr, err := glamour.NewTermRenderer(
187		glamour.WithStandardStyle("dark"),
188		glamour.WithWordWrap(w),
189	)
190
191	if err != nil {
192		return "", err
193	}
194	mdt, err := tr.Render(md)
195	if err != nil {
196		return "", err
197	}
198	// Enforce a maximum width for cases when glamour lines run long.
199	//
200	// TODO: This should utlimately be implemented as a Glamour option.
201	mdt = wrap.String(wordwrap.String((mdt), w), w)
202	return mdt, nil
203}