1package ssh
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
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 &&
200 ui.pages[ui.activePage].(*repo.Repo).Path() == "" &&
201 key.Matches(msg, ui.common.KeyMap.Back):
202 ui.activePage = selectionPage
203 // Always show the footer on selection page.
204 ui.showFooter = true
205 }
206 case tea.MouseMsg:
207 if msg.Action != tea.MouseActionPress {
208 break
209 }
210 switch msg.Button {
211 case tea.MouseButtonLeft:
212 switch {
213 case ui.common.Zone.Get("footer").InBounds(msg):
214 cmds = append(cmds, footer.ToggleFooterCmd)
215 }
216 }
217 }
218 case footer.ToggleFooterMsg:
219 ui.footer.SetShowAll(!ui.footer.ShowAll())
220 // Show the footer when on repo page and shot all help.
221 if ui.error == nil && ui.activePage == repoPage {
222 ui.showFooter = !ui.showFooter
223 }
224 case repo.RepoMsg:
225 ui.common.SetValue(common.RepoKey, msg)
226 ui.activePage = repoPage
227 // Show the footer on repo page if show all is set.
228 ui.showFooter = ui.footer.ShowAll()
229 cmds = append(cmds, repo.UpdateRefCmd(msg))
230 case common.ErrorMsg:
231 ui.error = msg
232 ui.state = errorState
233 ui.showFooter = true
234 case selector.SelectMsg:
235 switch msg.IdentifiableItem.(type) {
236 case selection.Item:
237 if ui.activePage == selectionPage {
238 cmds = append(cmds, ui.setRepoCmd(msg.ID()))
239 }
240 }
241 }
242 h, cmd := ui.header.Update(msg)
243 ui.header = h.(*header.Header)
244 if cmd != nil {
245 cmds = append(cmds, cmd)
246 }
247 f, cmd := ui.footer.Update(msg)
248 ui.footer = f.(*footer.Footer)
249 if cmd != nil {
250 cmds = append(cmds, cmd)
251 }
252 if ui.state != loadingState {
253 m, cmd := ui.pages[ui.activePage].Update(msg)
254 ui.pages[ui.activePage] = m.(common.Component)
255 if cmd != nil {
256 cmds = append(cmds, cmd)
257 }
258 }
259 // This fixes determining the height margin of the footer.
260 ui.SetSize(ui.common.Width, ui.common.Height)
261 return ui, tea.Batch(cmds...)
262}
263
264// View implements tea.Model.
265func (ui *UI) View() string {
266 var view string
267 wm, hm := ui.getMargins()
268 switch ui.state {
269 case loadingState:
270 view = "Loading..."
271 case errorState:
272 err := ui.common.Styles.ErrorTitle.Render("Bummer")
273 err += ui.common.Styles.ErrorBody.Render(ui.error.Error())
274 view = ui.common.Styles.Error.
275 Width(ui.common.Width -
276 wm -
277 ui.common.Styles.ErrorBody.GetHorizontalFrameSize()).
278 Height(ui.common.Height -
279 hm -
280 ui.common.Styles.Error.GetVerticalFrameSize()).
281 Render(err)
282 case readyState:
283 view = ui.pages[ui.activePage].View()
284 default:
285 view = "Unknown state :/ this is a bug!"
286 }
287 if ui.activePage == selectionPage {
288 view = lipgloss.JoinVertical(lipgloss.Left, ui.header.View(), view)
289 }
290 if ui.showFooter {
291 view = lipgloss.JoinVertical(lipgloss.Left, view, ui.footer.View())
292 }
293 return ui.common.Zone.Scan(
294 ui.common.Styles.App.Render(view),
295 )
296}
297
298func (ui *UI) openRepo(rn string) (proto.Repository, error) {
299 cfg := ui.common.Config()
300 if cfg == nil {
301 return nil, errors.New("config is nil")
302 }
303
304 ctx := ui.common.Context()
305 be := ui.common.Backend()
306 repos, err := be.Repositories(ctx)
307 if err != nil {
308 ui.common.Logger.Debugf("ui: failed to list repos: %v", err)
309 return nil, err
310 }
311 for _, r := range repos {
312 if r.Name() == rn {
313 return r, nil
314 }
315 }
316 return nil, common.ErrMissingRepo
317}
318
319func (ui *UI) setRepoCmd(rn string) tea.Cmd {
320 return func() tea.Msg {
321 r, err := ui.openRepo(rn)
322 if err != nil {
323 return common.ErrorMsg(err)
324 }
325 return repo.RepoMsg(r)
326 }
327}
328
329func (ui *UI) initialRepoCmd(rn string) tea.Cmd {
330 return func() tea.Msg {
331 r, err := ui.openRepo(rn)
332 if err != nil {
333 return nil
334 }
335 return repo.RepoMsg(r)
336 }
337}