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)).
111 Render(b.sshAddress())
112 return lipgloss.JoinHorizontal(lipgloss.Top, title, note)
113}
114
115func (b *Bubble) View() string {
116 s := b.styles
117 header := b.headerView()
118 bs := s.RepoBody.Copy()
119 if b.Active {
120 bs = bs.BorderForeground(s.ActiveBorderColor)
121 }
122 body := bs.
123 Width(b.width - b.widthMargin - s.RepoBody.GetVerticalFrameSize()).
124 Height(b.height - b.heightMargin - lipgloss.Height(header)).
125 Render(b.readmeViewport.View())
126 return header + body
127}
128
129func (b Bubble) sshAddress() string {
130 p := ":" + strconv.Itoa(int(b.Port))
131 if p == ":22" {
132 p = ""
133 }
134 return fmt.Sprintf("ssh://%s%s/%s", b.Host, p, b.name)
135}
136
137func (b *Bubble) setupCmd() tea.Msg {
138 r, err := b.repoSource.GetRepo(b.name)
139 if err == git.ErrMissingRepo {
140 return nil
141 }
142 if err != nil {
143 return ErrMsg{err}
144 }
145 md := r.Readme
146 if b.templateObject != nil {
147 md, err = b.templatize(md)
148 if err != nil {
149 return ErrMsg{err}
150 }
151 }
152 b.readme = md
153 md, err = b.glamourize(md)
154 if err != nil {
155 return ErrMsg{err}
156 }
157 b.readmeViewport.Viewport.SetContent(md)
158 b.GotoTop()
159 return nil
160}
161
162func (b *Bubble) templatize(mdt string) (string, error) {
163 t, err := template.New("readme").Parse(mdt)
164 if err != nil {
165 return "", err
166 }
167 buf := &bytes.Buffer{}
168 err = t.Execute(buf, b.templateObject)
169 if err != nil {
170 return "", err
171 }
172 return buf.String(), nil
173}
174
175func (b *Bubble) glamourize(md string) (string, error) {
176 // TODO: read gaps in appropriate style to remove the magic number below.
177 w := b.width - b.widthMargin - 4
178 if w > glamourMaxWidth {
179 w = glamourMaxWidth
180 }
181 tr, err := glamour.NewTermRenderer(
182 glamour.WithStandardStyle("dark"),
183 glamour.WithWordWrap(w),
184 )
185
186 if err != nil {
187 return "", err
188 }
189 mdt, err := tr.Render(md)
190 if err != nil {
191 return "", err
192 }
193 // Enforce a maximum width for cases when glamour lines run long.
194 //
195 // TODO: This should utlimately be implemented as a Glamour option.
196 mdt = wrap.String(wordwrap.String((mdt), w), w)
197 return mdt, nil
198}