1package tui
2
3import (
4 "fmt"
5 "smoothie/tui/bubbles/repo"
6 "smoothie/tui/bubbles/selection"
7
8 tea "github.com/charmbracelet/bubbletea"
9 "github.com/charmbracelet/lipgloss"
10 "github.com/muesli/termenv"
11)
12
13type windowMsg struct{}
14type errMsg struct{ err error }
15
16func (e errMsg) Error() string {
17 return e.err.Error()
18}
19
20func (b *Bubble) windowChangesCmd() tea.Msg {
21 w := <-b.windowChanges
22 b.width = w.Width
23 b.height = w.Height
24 return windowMsg{}
25}
26
27func (b *Bubble) setupCmd() tea.Msg {
28 lipgloss.SetColorProfile(termenv.ANSI256)
29 b.repos = b.repoSource.AllRepos()
30 mes := make([]MenuEntry, 0)
31 rs := make([]string, 0)
32 for _, me := range b.config.Menu {
33 mes = append(mes, me)
34 }
35 if b.config.ShowAllRepos {
36 OUTER:
37 for _, r := range b.repos {
38 for _, me := range mes {
39 if r.Name == me.Repo {
40 continue OUTER
41 }
42 }
43 mes = append(mes, MenuEntry{Name: r.Name, Repo: r.Name})
44 }
45 }
46 var tmplConfig *Config
47 for _, me := range mes {
48 if me.Repo == "config" {
49 tmplConfig = b.config
50 }
51 width := b.width
52 boxLeftWidth := menuStyle.GetWidth() + menuStyle.GetHorizontalFrameSize()
53 // TODO: also send this along with a tea.WindowSizeMsg
54 var heightMargin = lipgloss.Height(b.headerView()) +
55 lipgloss.Height(b.footerView()) +
56 contentBoxStyle.GetVerticalFrameSize() +
57 appBoxStyle.GetVerticalMargins() +
58 3 // TODO: make this dynamic (this is the height of the repo info)
59 rb := repo.NewBubble(b.repoSource, me.Repo, width, boxLeftWidth, b.height, heightMargin, tmplConfig)
60 rb.TitleStyle = contentBoxTitleStyle
61 rb.NoteStyle = contentBoxNoteStyle
62 rb.BodyStyle = contentBoxStyle
63 rb.ActiveBorderColor = activeBorderColor
64 initCmd := rb.Init()
65 msg := initCmd()
66 switch msg := msg.(type) {
67 case repo.ErrMsg:
68 return errMsg{fmt.Errorf("missing %s: %s", me.Repo, msg.Error)}
69 }
70 me.bubble = rb
71 b.repoMenu = append(b.repoMenu, me)
72 rs = append(rs, me.Name)
73 }
74 b.repoSelect = selection.NewBubble(rs, menuItemStyle, selectedMenuItemStyle, menuCursor.String())
75 b.boxes[0] = b.repoSelect
76 /*
77 b.commitsLog = commits.NewBubble(
78 b.height-verticalPadding-2,
79 boxRightWidth-horizontalPadding-2,
80 b.repoSource.GetCommits(200),
81 )
82 */
83 ir := -1
84 if b.initialRepo != "" {
85 for i, me := range b.repoMenu {
86 if me.Repo == b.initialRepo {
87 ir = i
88 }
89 }
90 }
91 if ir == -1 {
92 b.boxes[1] = b.repoMenu[0].bubble
93 b.activeBox = 0
94 } else {
95 b.boxes[1] = b.repoMenu[ir].bubble
96 b.repoSelect.SelectedItem = ir
97 b.activeBox = 1
98 }
99 b.state = loadedState
100 return nil
101}