Skip to content

Commit

Permalink
large refactor to prep for easier unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: everettraven <everettraven@gmail.com>
  • Loading branch information
everettraven committed Jan 5, 2024
1 parent 83d62e6 commit 2c9996a
Show file tree
Hide file tree
Showing 19 changed files with 642 additions and 509 deletions.
27 changes: 23 additions & 4 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/models"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/everettraven/buoy/pkg/paneler"
"github.com/everettraven/buoy/pkg/factories/datastream"
"github.com/everettraven/buoy/pkg/factories/panel"
"github.com/everettraven/buoy/pkg/types"
"github.com/spf13/cobra"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -38,6 +39,10 @@ func init() {
rootCommand.Flags().String("theme", styles.DefaultThemePath, "path to theme file")
}

type ErrorSetter interface {
SetError(err error)
}

func run(path string, themePath string) error {
var raw []byte
var ext string
Expand Down Expand Up @@ -76,21 +81,35 @@ func run(path string, themePath string) error {
log.Fatalf("loading theme: %s", err)
}

p := panel.NewPanelFactory(theme)

cfg := config.GetConfigOrDie()
p, err := paneler.NewDefaultPaneler(cfg, theme)
df, err := datastream.NewDatastreamFactory(cfg)
if err != nil {
log.Fatalf("configuring paneler: %s", err)
log.Fatalf("configuring datastream factory: %s", err)
}

panelModels := []tea.Model{}
for _, panel := range dash.Panels {
mod, err := p.Model(panel)
mod, err := p.ModelForPanel(panel)
if err != nil {
log.Fatalf("getting model for panel %q: %s", panel.Name, err)
}
panelModels = append(panelModels, mod)
}

for _, panel := range panelModels {
dataStream, err := df.DatastreamForModel(panel)
if err != nil {
if errSetter, ok := panel.(ErrorSetter); ok {
errSetter.SetError(err)
} else {
log.Fatalf("getting datastream for model: %s", err)
}
}
go dataStream.Run(make(<-chan struct{}))
}

m := models.NewDashboard(models.DefaultDashboardKeys, theme, panelModels...)
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error running program:", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/charm/models/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type Dashboard struct {
width int
help help.Model
keys DashboardKeyMap
theme *styles.Theme
theme styles.Theme
}

func NewDashboard(keys DashboardKeyMap, theme *styles.Theme, panels ...tea.Model) *Dashboard {
func NewDashboard(keys DashboardKeyMap, theme styles.Theme, panels ...tea.Model) *Dashboard {
tabs := []Tab{}
for _, panel := range panels {
if namer, ok := panel.(Namer); ok {
Expand Down
28 changes: 24 additions & 4 deletions pkg/charm/models/panels/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import (

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/everettraven/buoy/pkg/types"
)

// Item is a tea.Model implementation
// that represents an item panel
type Item struct {
viewport viewport.Model
name string
mutex *sync.Mutex
item types.Item
theme styles.Theme
err error
}

func NewItem(name string, viewport viewport.Model) *Item {
func NewItem(item types.Item, viewport viewport.Model, theme styles.Theme) *Item {
return &Item{
viewport: viewport,
name: name,
mutex: &sync.Mutex{},
item: item,
theme: theme,
}
}

Expand All @@ -37,6 +42,9 @@ func (m *Item) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *Item) View() string {
if m.err != nil {
return m.err.Error()
}
return m.viewport.View()
}

Expand All @@ -47,5 +55,17 @@ func (m *Item) SetContent(content string) {
}

func (m *Item) Name() string {
return m.name
return m.item.Name
}

func (m *Item) ItemDefinition() types.Item {
return m.item
}

func (m *Item) Theme() styles.Theme {
return m.theme
}

func (m *Item) SetError(err error) {
m.err = err
}
24 changes: 19 additions & 5 deletions pkg/charm/models/panels/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/everettraven/buoy/pkg/charm/styles"
"github.com/everettraven/buoy/pkg/types"
"github.com/muesli/reflow/wrap"
"github.com/sahilm/fuzzy"
)
Expand Down Expand Up @@ -65,25 +66,26 @@ const modeSearched = "searched"
type Logs struct {
viewport viewport.Model
searchbar textinput.Model
name string
mutex *sync.Mutex
content string
contentUpdated bool
mode string
keys LogsKeyMap
strictSearch bool
theme *styles.Theme
theme styles.Theme
log *types.Logs
err error
}

func NewLogs(keys LogsKeyMap, name string, theme *styles.Theme) *Logs {
func NewLogs(keys LogsKeyMap, log *types.Logs, theme styles.Theme) *Logs {
searchbar := textinput.New()
searchbar.Prompt = "> "
searchbar.Placeholder = "search term"
vp := viewport.New(10, 10)
return &Logs{
viewport: vp,
searchbar: searchbar,
name: name,
log: log,
mutex: &sync.Mutex{},
content: "",
mode: modeLogs,
Expand Down Expand Up @@ -149,6 +151,10 @@ func (m *Logs) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m *Logs) View() string {
if m.err != nil {
return m.err.Error()
}

searchMode := "fuzzy"
if m.strictSearch {
searchMode = "strict"
Expand Down Expand Up @@ -184,7 +190,15 @@ func (m *Logs) AddContent(content string) {
}

func (m *Logs) Name() string {
return m.name
return m.log.Name
}

func (m *Logs) LogDefinition() *types.Logs {
return m.log
}

func (m *Logs) SetError(err error) {
m.err = err
}

// searchLogs searches the logs for the term in the searchbar
Expand Down
72 changes: 41 additions & 31 deletions pkg/charm/models/panels/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ type RowInfo struct {
// Table is a tea.Model implementation
// that represents a table panel
type Table struct {
table tbl.Model
lister cache.GenericLister
scope meta.RESTScopeName
viewport viewport.Model
mode string
name string
mutex *sync.Mutex
rows map[types.UID]*RowInfo
columns []buoytypes.Column
err error
tempRows []tbl.Row
keys TableKeyMap
theme *styles.Theme
tableModel tbl.Model
lister cache.GenericLister
scope meta.RESTScopeName
viewport viewport.Model
mode string
mutex *sync.Mutex
rows map[types.UID]*RowInfo
columns []buoytypes.Column
err error
tempRows []tbl.Row
keys TableKeyMap
theme styles.Theme
table *buoytypes.Table
}

func NewTable(keys TableKeyMap, table *buoytypes.Table, lister cache.GenericLister, scope meta.RESTScopeName, theme *styles.Theme) *Table {
func NewTable(keys TableKeyMap, table *buoytypes.Table, theme styles.Theme) *Table {
tblColumns := []string{}
width := 0
for _, column := range table.Columns {
Expand All @@ -89,17 +89,15 @@ func NewTable(keys TableKeyMap, table *buoytypes.Table, lister cache.GenericList
tab.Styles.SelectedRow = theme.TableSelectedRowStyle()

return &Table{
table: tab,
viewport: viewport.New(0, 0),
scope: scope,
lister: lister,
mode: modeTable,
name: table.Name,
mutex: &sync.Mutex{},
rows: map[types.UID]*RowInfo{},
columns: table.Columns,
keys: keys,
theme: theme,
tableModel: tab,
viewport: viewport.New(0, 0),
mode: modeTable,
mutex: &sync.Mutex{},
rows: map[types.UID]*RowInfo{},
columns: table.Columns,
keys: keys,
theme: theme,
table: table,
}
}

Expand All @@ -111,7 +109,7 @@ func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.table.SetSize(msg.Width, msg.Height/2)
m.tableModel.SetSize(msg.Width, msg.Height/2)
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height / 2
case tea.KeyMsg:
Expand All @@ -120,7 +118,7 @@ func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.mode {
case modeTable:
m.mode = modeView
vpContent, err := m.FetchContentForIndex(m.table.Cursor())
vpContent, err := m.FetchContentForIndex(m.tableModel.Cursor())
if err != nil {
m.viewport.SetContent(err.Error())
} else {
Expand All @@ -134,13 +132,13 @@ func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if len(m.tempRows) > 0 {
m.table.SetRows(m.tempRows)
m.tableModel.SetRows(m.tempRows)
m.tempRows = []tbl.Row{}
}

switch m.mode {
case modeTable:
m.table, cmd = m.table.Update(msg)
m.tableModel, cmd = m.tableModel.Update(msg)
case modeView:
m.viewport, cmd = m.viewport.Update(msg)
}
Expand All @@ -153,7 +151,7 @@ func (m *Table) View() string {
}
switch m.mode {
case modeTable:
return m.table.View()
return m.tableModel.View()
case modeView:
return m.viewport.View()
default:
Expand Down Expand Up @@ -206,7 +204,19 @@ func (m *Table) Columns() []buoytypes.Column {
}

func (m *Table) Name() string {
return m.name
return m.table.Name
}

func (m *Table) TableDefinition() *buoytypes.Table {
return m.table
}

func (m *Table) SetLister(lister cache.GenericLister) {
m.lister = lister
}

func (m *Table) SetScope(scope meta.RESTScopeName) {
m.scope = scope
}

func (m *Table) SetError(err error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/charm/models/tabber.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type Tabber struct {
selected int
keyMap TabberKeyMap
width int
theme *styles.Theme
theme styles.Theme
}

func NewTabber(keyMap TabberKeyMap, theme *styles.Theme, tabs ...Tab) *Tabber {
func NewTabber(keyMap TabberKeyMap, theme styles.Theme, tabs ...Tab) *Tabber {
return &Tabber{
tabs: tabs,
keyMap: keyMap,
Expand Down Expand Up @@ -144,7 +144,7 @@ type pager struct {
pages []page
tabRightArrow string
tabLeftArrow string
theme *styles.Theme
theme styles.Theme
}

func (p *pager) renderForSelectedTab(selected int) string {
Expand Down
9 changes: 5 additions & 4 deletions pkg/charm/styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const DefaultThemePath = "~/.config/buoy/themes/default.json"

var DefaultColor = lipgloss.AdaptiveColor{Light: "63", Dark: "117"}

func LoadTheme(themePath string) (*Theme, error) {
t := &Theme{
func LoadTheme(themePath string) (Theme, error) {
t := Theme{
TabColor: DefaultColor,
SelectedRowHighlightColor: DefaultColor,
LogSearchHighlightColor: DefaultColor,
Expand All @@ -49,12 +49,13 @@ func LoadTheme(themePath string) (*Theme, error) {
return t, fmt.Errorf("reading theme file: %w", err)
}

err = json.Unmarshal(raw, t)
customTheme := &Theme{}
err = json.Unmarshal(raw, customTheme)
if err != nil {
return t, fmt.Errorf("unmarshalling theme: %w", err)
}

return t, nil
return *customTheme, nil
}

func (t *Theme) SelectedTabStyle() lipgloss.Style {
Expand Down
Loading

0 comments on commit 2c9996a

Please sign in to comment.