bubble.go

  1package tui
  2
  3import (
  4	"fmt"
  5	"smoothie/git"
  6	"smoothie/tui/bubbles/commits"
  7	"smoothie/tui/bubbles/repo"
  8	"smoothie/tui/bubbles/selection"
  9	"smoothie/tui/style"
 10	"strings"
 11
 12	tea "github.com/charmbracelet/bubbletea"
 13	"github.com/charmbracelet/lipgloss"
 14	"github.com/gliderlabs/ssh"
 15)
 16
 17type sessionState int
 18
 19const (
 20	startState sessionState = iota
 21	errorState
 22	loadedState
 23	quittingState
 24	quitState
 25)
 26
 27type Config struct {
 28	Name         string      `json:"name"`
 29	Host         string      `json:"host"`
 30	Port         int64       `json:"port"`
 31	ShowAllRepos bool        `json:"show_all_repos"`
 32	Menu         []MenuEntry `json:"menu"`
 33	RepoSource   *git.RepoSource
 34}
 35
 36type MenuEntry struct {
 37	Name   string `json:"name"`
 38	Note   string `json:"note"`
 39	Repo   string `json:"repo"`
 40	bubble *repo.Bubble
 41}
 42
 43type SessionConfig struct {
 44	Width       int
 45	Height      int
 46	InitialRepo string
 47}
 48
 49type Bubble struct {
 50	config        *Config
 51	styles        *style.Styles
 52	state         sessionState
 53	error         string
 54	width         int
 55	height        int
 56	windowChanges <-chan ssh.Window
 57	repoSource    *git.RepoSource
 58	initialRepo   string
 59	repoMenu      []MenuEntry
 60	repos         []*git.Repo
 61	boxes         []tea.Model
 62	activeBox     int
 63	repoSelect    *selection.Bubble
 64	commitsLog    *commits.Bubble
 65}
 66
 67func NewBubble(cfg *Config, sCfg *SessionConfig) *Bubble {
 68	b := &Bubble{
 69		config:      cfg,
 70		styles:      style.DefaultStyles(),
 71		width:       sCfg.Width,
 72		height:      sCfg.Height,
 73		repoSource:  cfg.RepoSource,
 74		repoMenu:    make([]MenuEntry, 0),
 75		boxes:       make([]tea.Model, 2),
 76		initialRepo: sCfg.InitialRepo,
 77	}
 78	b.state = startState
 79	return b
 80}
 81
 82func (b *Bubble) Init() tea.Cmd {
 83	return b.setupCmd
 84}
 85
 86func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
 87	cmds := make([]tea.Cmd, 0)
 88	// Always allow state, error, info, window resize and quit messages
 89	switch msg := msg.(type) {
 90	case tea.KeyMsg:
 91		switch msg.String() {
 92		case "q", "ctrl+c":
 93			return b, tea.Quit
 94		case "tab", "shift+tab":
 95			b.activeBox = (b.activeBox + 1) % 2
 96		case "h", "left":
 97			if b.activeBox > 0 {
 98				b.activeBox--
 99			}
100		case "l", "right":
101			if b.activeBox < len(b.boxes)-1 {
102				b.activeBox++
103			}
104		}
105	case errMsg:
106		b.error = msg.Error()
107		b.state = errorState
108		return b, nil
109	case tea.WindowSizeMsg:
110		b.width = msg.Width
111		b.height = msg.Height
112		if b.state == loadedState {
113			for i, bx := range b.boxes {
114				m, cmd := bx.Update(msg)
115				b.boxes[i] = m
116				if cmd != nil {
117					cmds = append(cmds, cmd)
118				}
119			}
120		}
121	case selection.SelectedMsg:
122		b.activeBox = 1
123		rb := b.repoMenu[msg.Index].bubble
124		rb.GotoTop()
125		b.boxes[1] = rb
126	case selection.ActiveMsg:
127		rb := b.repoMenu[msg.Index].bubble
128		rb.GotoTop()
129		b.boxes[1] = b.repoMenu[msg.Index].bubble
130	}
131	if b.state == loadedState {
132		ab, cmd := b.boxes[b.activeBox].Update(msg)
133		b.boxes[b.activeBox] = ab
134		if cmd != nil {
135			cmds = append(cmds, cmd)
136		}
137	}
138	return b, tea.Batch(cmds...)
139}
140
141func (b *Bubble) viewForBox(i int) string {
142	isActive := i == b.activeBox
143	switch box := b.boxes[i].(type) {
144	case *selection.Bubble:
145		var s lipgloss.Style
146		s = b.styles.Menu
147		if isActive {
148			s = s.Copy().BorderForeground(b.styles.ActiveBorderColor)
149		}
150		return s.Render(box.View())
151	case *repo.Bubble:
152		box.Active = isActive
153		return box.View()
154	default:
155		panic(fmt.Sprintf("unknown box type %T", box))
156	}
157}
158
159func (b Bubble) headerView() string {
160	w := b.width - b.styles.App.GetHorizontalFrameSize()
161	return b.styles.Header.Copy().Width(w).Render(b.config.Name)
162}
163
164func (b Bubble) footerView() string {
165	w := &strings.Builder{}
166	var h []helpEntry
167	switch b.state {
168	case errorState:
169		h = []helpEntry{{"q", "quit"}}
170	default:
171		h = []helpEntry{
172			{"tab", "section"},
173			{"↑/↓", "navigate"},
174			{"q", "quit"},
175		}
176		if _, ok := b.boxes[b.activeBox].(*repo.Bubble); ok {
177			h = append(h[:2], helpEntry{"f/b", "pgup/pgdown"}, h[2])
178		}
179	}
180	for i, v := range h {
181		fmt.Fprint(w, v.Render(b.styles))
182		if i != len(h)-1 {
183			fmt.Fprint(w, b.styles.HelpDivider)
184		}
185	}
186	return b.styles.Footer.Copy().Width(b.width).Render(w.String())
187}
188
189func (b Bubble) errorView() string {
190	s := b.styles
191	str := lipgloss.JoinHorizontal(
192		lipgloss.Top,
193		s.ErrorTitle.Render("Bummer"),
194		s.ErrorBody.Render(b.error),
195	)
196	h := b.height -
197		s.App.GetVerticalFrameSize() -
198		lipgloss.Height(b.headerView()) -
199		lipgloss.Height(b.footerView()) -
200		s.RepoBody.GetVerticalFrameSize() +
201		3 // TODO: this is repo header height -- get it dynamically
202	return s.Error.Copy().Height(h).Render(str)
203}
204
205func (b Bubble) View() string {
206	s := strings.Builder{}
207	s.WriteString(b.headerView())
208	s.WriteRune('\n')
209	switch b.state {
210	case loadedState:
211		lb := b.viewForBox(0)
212		rb := b.viewForBox(1)
213		s.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lb, rb))
214	case errorState:
215		s.WriteString(b.errorView())
216	}
217	s.WriteRune('\n')
218	s.WriteString(b.footerView())
219	return b.styles.App.Render(s.String())
220}
221
222type helpEntry struct {
223	key string
224	val string
225}
226
227func (h helpEntry) Render(s *style.Styles) string {
228	return fmt.Sprintf("%s %s", s.HelpKey.Render(h.key), s.HelpValue.Render(h.val))
229}