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	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("%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		if ui.activePage == 0 {
147			ui.activePage = (ui.activePage + 1) % 2
148			cmds = append(cmds, ui.setRepoCmd(string(msg)))
149		}
150	}
151	m, cmd := ui.pages[ui.activePage].Update(msg)
152	ui.pages[ui.activePage] = m.(common.Page)
153	if cmd != nil {
154		cmds = append(cmds, cmd)
155	}
156	return ui, tea.Batch(cmds...)
157}
158
159// View implements tea.Model.
160func (ui *UI) View() string {
161	s := strings.Builder{}
162	switch ui.state {
163	case startState:
164		s.WriteString("Loading...")
165	case errorState:
166		err := ui.common.Styles.ErrorTitle.Render("Bummer")
167		err += ui.common.Styles.ErrorBody.Render(ui.error.Error())
168		s.WriteString(err)
169	case loadedState:
170		s.WriteString(lipgloss.JoinVertical(
171			lipgloss.Bottom,
172			ui.header.View(),
173			ui.pages[ui.activePage].View(),
174			ui.footer.View(),
175		))
176	default:
177		s.WriteString("Unknown state :/ this is a bug!")
178	}
179	return ui.common.Styles.App.Render(s.String())
180}
181
182func (ui *UI) setRepoCmd(rn string) tea.Cmd {
183	rs := ui.s.Config().Source
184	return func() tea.Msg {
185		for _, r := range rs.AllRepos() {
186			if r.Name() == rn {
187				return repo.RepoMsg(r)
188			}
189		}
190		return common.ErrorMsg(git.ErrMissingRepo)
191	}
192}