ui.go

  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	switch ui.state {
 68	case errorState:
 69		b = append(b, ui.common.KeyMap.Back)
 70	case loadedState:
 71		b = append(b, ui.pages[ui.activePage].ShortHelp()...)
 72	}
 73	b = append(b, ui.common.KeyMap.Quit)
 74	return b
 75}
 76
 77// FullHelp implements help.KeyMap.
 78func (ui *UI) FullHelp() [][]key.Binding {
 79	b := make([][]key.Binding, 0)
 80	switch ui.state {
 81	case errorState:
 82		b = append(b, []key.Binding{ui.common.KeyMap.Back})
 83	case loadedState:
 84		b = append(b, ui.pages[ui.activePage].FullHelp()...)
 85	}
 86	b = append(b, []key.Binding{ui.common.KeyMap.Quit})
 87	return b
 88}
 89
 90// SetSize implements common.Component.
 91func (ui *UI) SetSize(width, height int) {
 92	ui.common.SetSize(width, height)
 93	wm, hm := ui.getMargins()
 94	ui.header.SetSize(width-wm, height-hm)
 95	ui.footer.SetSize(width-wm, height-hm)
 96	for _, p := range ui.pages {
 97		if p != nil {
 98			p.SetSize(width-wm, height-hm)
 99		}
100	}
101}
102
103// Init implements tea.Model.
104func (ui *UI) Init() tea.Cmd {
105	cfg := ui.s.Config()
106	ui.pages[0] = selection.New(ui.s, ui.common)
107	ui.pages[1] = repo.New(ui.common, &source{cfg.Source})
108	ui.SetSize(ui.common.Width, ui.common.Height)
109	ui.state = loadedState
110	return tea.Batch(
111		ui.pages[0].Init(),
112		ui.pages[1].Init(),
113	)
114}
115
116// Update implements tea.Model.
117// TODO show full help
118func (ui *UI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
119	log.Printf("msg: %T", msg)
120	cmds := make([]tea.Cmd, 0)
121	switch msg := msg.(type) {
122	case tea.WindowSizeMsg:
123		h, cmd := ui.header.Update(msg)
124		ui.header = h.(*header.Header)
125		if cmd != nil {
126			cmds = append(cmds, cmd)
127		}
128		f, cmd := ui.footer.Update(msg)
129		ui.footer = f.(*footer.Footer)
130		if cmd != nil {
131			cmds = append(cmds, cmd)
132		}
133		for i, p := range ui.pages {
134			m, cmd := p.Update(msg)
135			ui.pages[i] = m.(common.Page)
136			if cmd != nil {
137				cmds = append(cmds, cmd)
138			}
139		}
140		ui.SetSize(msg.Width, msg.Height)
141	case tea.KeyMsg:
142		switch {
143		case key.Matches(msg, ui.common.KeyMap.Back) && ui.error != nil:
144			ui.error = nil
145			ui.state = loadedState
146		case key.Matches(msg, ui.common.KeyMap.Quit):
147			return ui, tea.Quit
148		case ui.activePage == 1 && key.Matches(msg, ui.common.KeyMap.Back):
149			ui.activePage = 0
150		}
151	case common.ErrorMsg:
152		ui.error = msg
153		ui.state = errorState
154		return ui, nil
155	case selector.SelectMsg:
156		switch msg.IdentifiableItem.(type) {
157		case selection.Item:
158			if ui.activePage == 0 {
159				ui.activePage = 1
160				cmds = append(cmds, ui.setRepoCmd(msg.ID()))
161			}
162		}
163	}
164	m, cmd := ui.pages[ui.activePage].Update(msg)
165	ui.pages[ui.activePage] = m.(common.Page)
166	if cmd != nil {
167		cmds = append(cmds, cmd)
168	}
169	return ui, tea.Batch(cmds...)
170}
171
172// View implements tea.Model.
173func (ui *UI) View() string {
174	s := strings.Builder{}
175	switch ui.state {
176	case startState:
177		s.WriteString("Loading...")
178	case errorState:
179		err := ui.common.Styles.ErrorTitle.Render("Bummer")
180		err += ui.common.Styles.ErrorBody.Render(ui.error.Error())
181		view := ui.common.Styles.ErrorBody.Copy().
182			Width(ui.common.Width -
183				ui.common.Styles.App.GetHorizontalFrameSize() -
184				ui.common.Styles.ErrorBody.GetHorizontalFrameSize()).
185			Height(ui.common.Height -
186				ui.common.Styles.App.GetVerticalFrameSize() -
187				ui.common.Styles.Header.GetVerticalFrameSize() - 2).
188			Render(err)
189		s.WriteString(lipgloss.JoinVertical(
190			lipgloss.Bottom,
191			ui.header.View(),
192			view,
193			ui.footer.View(),
194		))
195	case loadedState:
196		s.WriteString(lipgloss.JoinVertical(
197			lipgloss.Bottom,
198			ui.header.View(),
199			ui.pages[ui.activePage].View(),
200			ui.footer.View(),
201		))
202	default:
203		s.WriteString("Unknown state :/ this is a bug!")
204	}
205	return ui.common.Styles.App.Render(s.String())
206}
207
208func (ui *UI) setRepoCmd(rn string) tea.Cmd {
209	rs := ui.s.Config().Source
210	return func() tea.Msg {
211		for _, r := range rs.AllRepos() {
212			if r.Name() == rn {
213				return repo.RepoMsg(r)
214			}
215		}
216		return common.ErrorMsg(git.ErrMissingRepo)
217	}
218}