bubble.go

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