bubble.go

  1package tui
  2
  3import (
  4	"fmt"
  5	"io"
  6	"smoothie/git"
  7	"smoothie/tui/bubbles/commits"
  8	"smoothie/tui/bubbles/repo"
  9	"smoothie/tui/bubbles/selection"
 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	WindowChanges <-chan ssh.Window
 47	InitialRepo   string
 48}
 49
 50type Bubble struct {
 51	config        *Config
 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		width:         sCfg.Width,
 71		height:        sCfg.Height,
 72		windowChanges: sCfg.WindowChanges,
 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 tea.Batch(b.windowChangesCmd, 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":
 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 windowMsg:
110		cmds = append(cmds, b.windowChangesCmd)
111	case tea.WindowSizeMsg:
112		b.width = msg.Width
113		b.height = msg.Height
114		if b.state == loadedState {
115			ab, cmd := b.boxes[b.activeBox].Update(msg)
116			b.boxes[b.activeBox] = ab
117			if cmd != nil {
118				cmds = append(cmds, cmd)
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, width int, height int) string {
142	var ls lipgloss.Style
143	if i == b.activeBox {
144		ls = activeBoxStyle.Copy()
145	} else {
146		ls = inactiveBoxStyle.Copy()
147	}
148	ls.Width(width)
149	if height > 0 {
150		ls.Height(height).MarginBottom(3)
151	}
152	return ls.Render(b.boxes[i].View())
153}
154
155func (b Bubble) footerView(w io.Writer) {
156	h := []helpEntry{
157		{"tab", "section"},
158		{"↑/↓", "navigate"},
159		{"q", "quit"},
160	}
161	for i, v := range h {
162		fmt.Fprint(w, v)
163		if i != len(h)-1 {
164			fmt.Fprint(w, helpDivider)
165		}
166	}
167}
168
169func (b *Bubble) View() string {
170	s := strings.Builder{}
171	w := b.width - 3
172	s.WriteString(headerStyle.Width(w - 2).Render(b.config.Name))
173	s.WriteRune('\n')
174	switch b.state {
175	case loadedState:
176		lb := b.viewForBox(0, boxLeftWidth, 0)
177		rb := b.viewForBox(1, b.width-boxLeftWidth-10, b.height-8)
178		s.WriteString(lipgloss.JoinHorizontal(lipgloss.Top, lb, rb))
179	case errorState:
180		s.WriteString(errorStyle.Render(fmt.Sprintf("Bummer: %s", b.error)))
181	}
182	s.WriteRune('\n')
183	b.footerView(&s)
184	return appBoxStyle.Width(w).Height(b.height).Render(s.String())
185}