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