1package shell
2
3import (
4 "errors"
5
6 "github.com/charmbracelet/bubbles/key"
7 "github.com/charmbracelet/bubbles/list"
8 tea "github.com/charmbracelet/bubbletea"
9 "github.com/charmbracelet/lipgloss"
10 "github.com/charmbracelet/soft-serve/git"
11 "github.com/charmbracelet/soft-serve/pkg/proto"
12 "github.com/charmbracelet/soft-serve/pkg/ui/common"
13 "github.com/charmbracelet/soft-serve/pkg/ui/components/footer"
14 "github.com/charmbracelet/soft-serve/pkg/ui/components/header"
15 "github.com/charmbracelet/soft-serve/pkg/ui/components/selector"
16 "github.com/charmbracelet/soft-serve/pkg/ui/pages/repo"
17 "github.com/charmbracelet/soft-serve/pkg/ui/pages/selection"
18)
19
20type page int
21
22const (
23 selectionPage page = iota
24 repoPage
25)
26
27type sessionState int
28
29const (
30 loadingState sessionState = iota
31 errorState
32 readyState
33)
34
35// UI is the main UI model.
36type UI struct {
37 serverName string
38 initialRepo string
39 common common.Common
40 pages []common.Component
41 activePage page
42 state sessionState
43 header *header.Header
44 footer *footer.Footer
45 showFooter bool
46 error error
47}
48
49// NewUI returns a new UI model.
50func NewUI(c common.Common, initialRepo string) *UI {
51 serverName := c.Config().Name
52 h := header.New(c, serverName)
53 ui := &UI{
54 serverName: serverName,
55 common: c,
56 pages: make([]common.Component, 2), // selection & repo
57 activePage: selectionPage,
58 state: loadingState,
59 header: h,
60 initialRepo: initialRepo,
61 showFooter: true,
62 }
63 ui.footer = footer.New(c, ui)
64 return ui
65}
66
67func (ui *UI) getMargins() (wm, hm int) {
68 style := ui.common.Styles.App.Copy()
69 switch ui.activePage {
70 case selectionPage:
71 hm += ui.common.Styles.ServerName.GetHeight() +
72 ui.common.Styles.ServerName.GetVerticalFrameSize()
73 case repoPage:
74 }
75 wm += style.GetHorizontalFrameSize()
76 hm += style.GetVerticalFrameSize()
77 if ui.showFooter {
78 // NOTE: we don't use the footer's style to determine the margins
79 // because footer.Height() is the height of the footer after applying
80 // the styles.
81 hm += ui.footer.Height()
82 }
83 return
84}
85
86// ShortHelp implements help.KeyMap.
87func (ui *UI) ShortHelp() []key.Binding {
88 b := make([]key.Binding, 0)
89 switch ui.state {
90 case errorState:
91 b = append(b, ui.common.KeyMap.Back)
92 case readyState:
93 b = append(b, ui.pages[ui.activePage].ShortHelp()...)
94 }
95 if !ui.IsFiltering() {
96 b = append(b, ui.common.KeyMap.Quit)
97 }
98 b = append(b, ui.common.KeyMap.Help)
99 return b
100}
101
102// FullHelp implements help.KeyMap.
103func (ui *UI) FullHelp() [][]key.Binding {
104 b := make([][]key.Binding, 0)
105 switch ui.state {
106 case errorState:
107 b = append(b, []key.Binding{ui.common.KeyMap.Back})
108 case readyState:
109 b = append(b, ui.pages[ui.activePage].FullHelp()...)
110 }
111 h := []key.Binding{
112 ui.common.KeyMap.Help,
113 }
114 if !ui.IsFiltering() {
115 h = append(h, ui.common.KeyMap.Quit)
116 }
117 b = append(b, h)
118 return b
119}
120
121// SetSize implements common.Component.
122func (ui *UI) SetSize(width, height int) {
123 ui.common.SetSize(width, height)
124 wm, hm := ui.getMargins()
125 ui.header.SetSize(width-wm, height-hm)
126 ui.footer.SetSize(width-wm, height-hm)
127 for _, p := range ui.pages {
128 if p != nil {
129 p.SetSize(width-wm, height-hm)
130 }
131 }
132}
133
134// Init implements tea.Model.
135func (ui *UI) Init() tea.Cmd {
136 ui.pages[selectionPage] = selection.New(ui.common)
137 ui.pages[repoPage] = repo.New(ui.common,
138 repo.NewReadme(ui.common),
139 repo.NewFiles(ui.common),
140 repo.NewLog(ui.common),
141 repo.NewRefs(ui.common, git.RefsHeads),
142 repo.NewRefs(ui.common, git.RefsTags),
143 )
144 ui.SetSize(ui.common.Width, ui.common.Height)
145 cmds := make([]tea.Cmd, 0)
146 cmds = append(cmds,
147 ui.pages[selectionPage].Init(),
148 ui.pages[repoPage].Init(),
149 )
150 if ui.initialRepo != "" {
151 cmds = append(cmds, ui.initialRepoCmd(ui.initialRepo))
152 }
153 ui.state = readyState
154 ui.SetSize(ui.common.Width, ui.common.Height)
155 return tea.Batch(cmds...)
156}
157
158// IsFiltering returns true if the selection page is filtering.
159func (ui *UI) IsFiltering() bool {
160 if ui.activePage == selectionPage {
161 if s, ok := ui.pages[selectionPage].(*selection.Selection); ok && s.FilterState() == list.Filtering {
162 return true
163 }
164 }
165 return false
166}
167
168// Update implements tea.Model.
169func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
170 ui.common.Logger.Debugf("msg received: %T", msg)
171 cmds := make([]tea.Cmd, 0)
172 switch msg := msg.(type) {
173 case tea.WindowSizeMsg:
174 ui.SetSize(msg.Width, msg.Height)
175 for i, p := range ui.pages {
176 m, cmd := p.Update(msg)
177 ui.pages[i] = m.(common.Component)
178 if cmd != nil {
179 cmds = append(cmds, cmd)
180 }
181 }
182 case tea.KeyMsg, tea.MouseMsg:
183 switch msg := msg.(type) {
184 case tea.KeyMsg:
185 switch {
186 case key.Matches(msg, ui.common.KeyMap.Back) && ui.error != nil:
187 ui.error = nil
188 ui.state = readyState
189 // Always show the footer on error.
190 ui.showFooter = ui.footer.ShowAll()
191 case key.Matches(msg, ui.common.KeyMap.Help):
192 cmds = append(cmds, footer.ToggleFooterCmd)
193 case key.Matches(msg, ui.common.KeyMap.Quit):
194 if !ui.IsFiltering() {
195 // Stop bubblezone background workers.
196 ui.common.Zone.Close()
197 return ui, tea.Quit
198 }
199 case ui.activePage == repoPage && key.Matches(msg, ui.common.KeyMap.Back):
200 ui.activePage = selectionPage
201 // Always show the footer on selection page.
202 ui.showFooter = true
203 }
204 case tea.MouseMsg:
205 switch msg.Type {
206 case tea.MouseLeft:
207 switch {
208 case ui.common.Zone.Get("footer").InBounds(msg):
209 cmds = append(cmds, footer.ToggleFooterCmd)
210 }
211 }
212 }
213 case footer.ToggleFooterMsg:
214 ui.footer.SetShowAll(!ui.footer.ShowAll())
215 // Show the footer when on repo page and shot all help.
216 if ui.error == nil && ui.activePage == repoPage {
217 ui.showFooter = !ui.showFooter
218 }
219 case repo.RepoMsg:
220 ui.common.SetValue(common.RepoKey, msg)
221 ui.activePage = repoPage
222 // Show the footer on repo page if show all is set.
223 ui.showFooter = ui.footer.ShowAll()
224 cmds = append(cmds, repo.UpdateRefCmd(msg))
225 case common.ErrorMsg:
226 ui.error = msg
227 ui.state = errorState
228 ui.showFooter = true
229 case selector.SelectMsg:
230 switch msg.IdentifiableItem.(type) {
231 case selection.Item:
232 if ui.activePage == selectionPage {
233 cmds = append(cmds, ui.setRepoCmd(msg.ID()))
234 }
235 }
236 }
237 h, cmd := ui.header.Update(msg)
238 ui.header = h.(*header.Header)
239 if cmd != nil {
240 cmds = append(cmds, cmd)
241 }
242 f, cmd := ui.footer.Update(msg)
243 ui.footer = f.(*footer.Footer)
244 if cmd != nil {
245 cmds = append(cmds, cmd)
246 }
247 if ui.state != loadingState {
248 m, cmd := ui.pages[ui.activePage].Update(msg)
249 ui.pages[ui.activePage] = m.(common.Component)
250 if cmd != nil {
251 cmds = append(cmds, cmd)
252 }
253 }
254 // This fixes determining the height margin of the footer.
255 ui.SetSize(ui.common.Width, ui.common.Height)
256 return ui, tea.Batch(cmds...)
257}
258
259// View implements tea.Model.
260func (ui *UI) View() string {
261 var view string
262 wm, hm := ui.getMargins()
263 switch ui.state {
264 case loadingState:
265 view = "Loading..."
266 case errorState:
267 err := ui.common.Styles.ErrorTitle.Render("Bummer")
268 err += ui.common.Styles.ErrorBody.Render(ui.error.Error())
269 view = ui.common.Styles.Error.Copy().
270 Width(ui.common.Width -
271 wm -
272 ui.common.Styles.ErrorBody.GetHorizontalFrameSize()).
273 Height(ui.common.Height -
274 hm -
275 ui.common.Styles.Error.GetVerticalFrameSize()).
276 Render(err)
277 case readyState:
278 view = ui.pages[ui.activePage].View()
279 default:
280 view = "Unknown state :/ this is a bug!"
281 }
282 if ui.activePage == selectionPage {
283 view = lipgloss.JoinVertical(lipgloss.Top, ui.header.View(), view)
284 }
285 if ui.showFooter {
286 view = lipgloss.JoinVertical(lipgloss.Top, view, ui.footer.View())
287 }
288 return ui.common.Zone.Scan(
289 ui.common.Styles.App.Render(view),
290 )
291}
292
293func (ui *UI) openRepo(rn string) (proto.Repository, error) {
294 cfg := ui.common.Config()
295 if cfg == nil {
296 return nil, errors.New("config is nil")
297 }
298
299 ctx := ui.common.Context()
300 be := ui.common.Backend()
301 repos, err := be.Repositories(ctx)
302 if err != nil {
303 ui.common.Logger.Debugf("ui: failed to list repos: %v", err)
304 return nil, err
305 }
306 for _, r := range repos {
307 if r.Name() == rn {
308 return r, nil
309 }
310 }
311 return nil, common.ErrMissingRepo
312}
313
314func (ui *UI) setRepoCmd(rn string) tea.Cmd {
315 return func() tea.Msg {
316 r, err := ui.openRepo(rn)
317 if err != nil {
318 return common.ErrorMsg(err)
319 }
320 return repo.RepoMsg(r)
321 }
322}
323
324func (ui *UI) initialRepoCmd(rn string) tea.Cmd {
325 return func() tea.Msg {
326 r, err := ui.openRepo(rn)
327 if err != nil {
328 return nil
329 }
330 return repo.RepoMsg(r)
331 }
332}