Skip to content

Commit

Permalink
Refactor setupCmd and fix menu entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Oct 5, 2021
1 parent 0a01257 commit aca1ca3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 44 deletions.
4 changes: 0 additions & 4 deletions tui/bubbles/repo/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package repo
import (
"bytes"
"fmt"
"log"
"strconv"
"text/template"
"time"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -162,7 +160,6 @@ func (b Bubble) sshAddress() string {
}

func (b *Bubble) setupCmd() tea.Msg {
ct := time.Now()
r, err := b.repoSource.GetRepo(b.name)
if err == git.ErrMissingRepo {
return nil
Expand All @@ -184,7 +181,6 @@ func (b *Bubble) setupCmd() tea.Msg {
}
b.readmeViewport.Viewport.SetContent(md)
b.GotoTop()
log.Printf("Repo bubble loaded in %s", time.Since(ct))
return nil
}

Expand Down
112 changes: 72 additions & 40 deletions tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package tui

import (
"fmt"
"log"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/soft/config"
"github.com/charmbracelet/soft/tui/bubbles/repo"
br "github.com/charmbracelet/soft/tui/bubbles/repo"
"github.com/charmbracelet/soft/tui/bubbles/selection"
"github.com/muesli/termenv"
)
Expand All @@ -23,50 +21,23 @@ func (b *Bubble) setupCmd() tea.Msg {
if b.config == nil || b.config.Source == nil {
return errMsg{err: fmt.Errorf("config not set")}
}
ct := time.Now()
lipgloss.SetColorProfile(termenv.ANSI256)
b.repos = b.config.Source.AllRepos()
mes := append([]MenuEntry{}, b.repoMenu...)
rs := make([]string, 0)
OUTER:
for _, r := range b.repos {
for _, me := range mes {
if r.Name == me.Repo {
continue OUTER
}
}
mes = append(mes, MenuEntry{Name: r.Name, Repo: r.Name})
mes, err := b.menuEntriesFromSource()
if err != nil {
return errMsg{err}
}
if len(mes) == 0 {
return errMsg{fmt.Errorf("no repos found")}
}
var tmplConfig *config.Config
for _, me := range mes {
if me.Repo == "config" {
tmplConfig = b.config
}
width := b.width
boxLeftWidth := b.styles.Menu.GetWidth() + b.styles.Menu.GetHorizontalFrameSize()
// TODO: also send this along with a tea.WindowSizeMsg
var heightMargin = lipgloss.Height(b.headerView()) +
lipgloss.Height(b.footerView()) +
b.styles.RepoBody.GetVerticalFrameSize() +
b.styles.App.GetVerticalMargins()
rb := repo.NewBubble(b.config.Source, me.Repo, b.styles, width, boxLeftWidth, b.height, heightMargin, tmplConfig)
rb.Host = b.config.Host
rb.Port = b.config.Port
initCmd := rb.Init()
msg := initCmd()
switch msg := msg.(type) {
case repo.ErrMsg:
return errMsg{fmt.Errorf("missing %s: %s", me.Repo, msg.Error)}
}
me.bubble = rb
b.repoMenu = append(b.repoMenu, me)
rs = append(rs, me.Name)
b.repoMenu = mes
rs := make([]string, 0)
for _, m := range mes {
rs = append(rs, m.Name)
}
b.repoSelect = selection.NewBubble(rs, b.styles)
b.boxes[0] = b.repoSelect

// Jump to an initial repo
ir := -1
if b.initialRepo != "" {
for i, me := range b.repoMenu {
Expand All @@ -83,7 +54,68 @@ OUTER:
b.repoSelect.SelectedItem = ir
b.activeBox = 1
}

b.state = loadedState
log.Printf("App bubble loaded in %s", time.Since(ct))
return nil
}

func (b *Bubble) menuEntriesFromSource() ([]MenuEntry, error) {
mes := make([]MenuEntry, 0)
for _, r := range b.config.Repos {
me, err := b.newMenuEntry(r.Name, r.Repo)
if err != nil {
return nil, err
}
mes = append(mes, me)
}
rs := b.config.Source.AllRepos()
OUTER:
for _, r := range rs {
for _, me := range mes {
if r.Name == me.Repo {
continue OUTER
}
}
me, err := b.newMenuEntry(r.Name, r.Name)
if err != nil {
return nil, err
}
mes = append(mes, me)
}
return mes, nil
}

func (b *Bubble) newMenuEntry(name string, repo string) (MenuEntry, error) {
var tmplConfig *config.Config
me := MenuEntry{Name: name, Repo: repo}
width := b.width
boxLeftWidth := b.styles.Menu.GetWidth() + b.styles.Menu.GetHorizontalFrameSize()
// TODO: also send this along with a tea.WindowSizeMsg
var heightMargin = lipgloss.Height(b.headerView()) +
lipgloss.Height(b.footerView()) +
b.styles.RepoBody.GetVerticalFrameSize() +
b.styles.App.GetVerticalMargins()
if repo == "config" {
tmplConfig = b.config
}
rb := br.NewBubble(
b.config.Source,
me.Repo,
b.styles,
width,
boxLeftWidth,
b.height,
heightMargin,
tmplConfig,
)
rb.Host = b.config.Host
rb.Port = b.config.Port
initCmd := rb.Init()
msg := initCmd()
switch msg := msg.(type) {
case br.ErrMsg:
return me, fmt.Errorf("missing %s: %s", me.Repo, msg.Error)
}
me.bubble = rb
return me, nil
}

0 comments on commit aca1ca3

Please sign in to comment.