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