1package ui
2
3import (
4 "log"
5 "strings"
6
7 "github.com/charmbracelet/bubbles/key"
8 tea "github.com/charmbracelet/bubbletea"
9 "github.com/charmbracelet/lipgloss"
10 "github.com/charmbracelet/soft-serve/ui/common"
11 "github.com/charmbracelet/soft-serve/ui/components/footer"
12 "github.com/charmbracelet/soft-serve/ui/components/header"
13 "github.com/charmbracelet/soft-serve/ui/components/selector"
14 "github.com/charmbracelet/soft-serve/ui/git"
15 "github.com/charmbracelet/soft-serve/ui/pages/repo"
16 "github.com/charmbracelet/soft-serve/ui/pages/selection"
17 "github.com/charmbracelet/soft-serve/ui/session"
18)
19
20type sessionState int
21
22const (
23 startState sessionState = iota
24 errorState
25 loadedState
26)
27
28// UI is the main UI model.
29type UI struct {
30 s session.Session
31 common common.Common
32 pages []common.Page
33 activePage int
34 state sessionState
35 header *header.Header
36 footer *footer.Footer
37 error error
38}
39
40// New returns a new UI model.
41func New(s session.Session, c common.Common, initialRepo string) *UI {
42 h := header.New(c, s.Config().Name)
43 ui := &UI{
44 s: s,
45 common: c,
46 pages: make([]common.Page, 2), // selection & repo
47 activePage: 0,
48 state: startState,
49 header: h,
50 }
51 ui.footer = footer.New(c, ui)
52 ui.SetSize(c.Width, c.Height)
53 return ui
54}
55
56func (ui *UI) getMargins() (wm, hm int) {
57 wm = ui.common.Styles.App.GetHorizontalFrameSize()
58 hm = ui.common.Styles.App.GetVerticalFrameSize() +
59 ui.common.Styles.Header.GetHeight() +
60 ui.common.Styles.Footer.GetHeight()
61 return
62}
63
64// ShortHelp implements help.KeyMap.
65func (ui *UI) ShortHelp() []key.Binding {
66 b := make([]key.Binding, 0)
67 b = append(b, ui.pages[ui.activePage].ShortHelp()...)
68 b = append(b, ui.common.KeyMap.Quit)
69 return b
70}
71
72// FullHelp implements help.KeyMap.
73func (ui *UI) FullHelp() [][]key.Binding {
74 b := make([][]key.Binding, 0)
75 b = append(b, ui.pages[ui.activePage].FullHelp()...)
76 b = append(b, []key.Binding{ui.common.KeyMap.Quit})
77 return b
78}
79
80// SetSize implements common.Component.
81func (ui *UI) SetSize(width, height int) {
82 ui.common.SetSize(width, height)
83 wm, hm := ui.getMargins()
84 ui.header.SetSize(width-wm, height-hm)
85 ui.footer.SetSize(width-wm, height-hm)
86 for _, p := range ui.pages {
87 if p != nil {
88 p.SetSize(width-wm, height-hm)
89 }
90 }
91}
92
93// Init implements tea.Model.
94func (ui *UI) Init() tea.Cmd {
95 cfg := ui.s.Config()
96 ui.pages[0] = selection.New(ui.s, ui.common)
97 ui.pages[1] = repo.New(ui.common, &source{cfg.Source})
98 ui.SetSize(ui.common.Width, ui.common.Height)
99 ui.state = loadedState
100 return tea.Batch(
101 ui.pages[0].Init(),
102 ui.pages[1].Init(),
103 )
104}
105
106// Update implements tea.Model.
107// TODO show full help
108func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
109 log.Printf("msg: %T", msg)
110 cmds := make([]tea.Cmd, 0)
111 switch msg := msg.(type) {
112 case tea.WindowSizeMsg:
113 h, cmd := ui.header.Update(msg)
114 ui.header = h.(*header.Header)
115 if cmd != nil {
116 cmds = append(cmds, cmd)
117 }
118 f, cmd := ui.footer.Update(msg)
119 ui.footer = f.(*footer.Footer)
120 if cmd != nil {
121 cmds = append(cmds, cmd)
122 }
123 for i, p := range ui.pages {
124 m, cmd := p.Update(msg)
125 ui.pages[i] = m.(common.Page)
126 if cmd != nil {
127 cmds = append(cmds, cmd)
128 }
129 }
130 ui.SetSize(msg.Width, msg.Height)
131 case tea.KeyMsg:
132 switch {
133 case key.Matches(msg, ui.common.KeyMap.Back) && ui.error != nil:
134 ui.error = nil
135 ui.state = loadedState
136 case key.Matches(msg, ui.common.KeyMap.Quit):
137 return ui, tea.Quit
138 case ui.activePage == 1 && key.Matches(msg, ui.common.KeyMap.Back):
139 ui.activePage = 0
140 }
141 case common.ErrorMsg:
142 ui.error = msg
143 ui.state = errorState
144 return ui, nil
145 case selector.SelectMsg:
146 switch msg.IdentifiableItem.(type) {
147 case selection.Item:
148 if ui.activePage == 0 {
149 ui.activePage = 1
150 cmds = append(cmds, ui.setRepoCmd(msg.ID()))
151 }
152 }
153 }
154 m, cmd := ui.pages[ui.activePage].Update(msg)
155 ui.pages[ui.activePage] = m.(common.Page)
156 if cmd != nil {
157 cmds = append(cmds, cmd)
158 }
159 return ui, tea.Batch(cmds...)
160}
161
162// View implements tea.Model.
163func (ui *UI) View() string {
164 s := strings.Builder{}
165 switch ui.state {
166 case startState:
167 s.WriteString("Loading...")
168 case errorState:
169 err := ui.common.Styles.ErrorTitle.Render("Bummer")
170 err += ui.common.Styles.ErrorBody.Render(ui.error.Error())
171 s.WriteString(err)
172 case loadedState:
173 s.WriteString(lipgloss.JoinVertical(
174 lipgloss.Bottom,
175 ui.header.View(),
176 ui.pages[ui.activePage].View(),
177 ui.footer.View(),
178 ))
179 default:
180 s.WriteString("Unknown state :/ this is a bug!")
181 }
182 return ui.common.Styles.App.Render(s.String())
183}
184
185func (ui *UI) setRepoCmd(rn string) tea.Cmd {
186 rs := ui.s.Config().Source
187 return func() tea.Msg {
188 for _, r := range rs.AllRepos() {
189 if r.Name() == rn {
190 return repo.RepoMsg(r)
191 }
192 }
193 return common.ErrorMsg(git.ErrMissingRepo)
194 }
195}