Skip to content

Commit

Permalink
chore(tui): Simplify conditional rows
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 1, 2024
1 parent 48abba8 commit 2a277e6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
12 changes: 4 additions & 8 deletions internal/actions/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,10 @@ func (action Dump) summary(err error, took time.Duration, size *util.SizeWriter,
t := tui.MinimalTable(r).
Row("Context", action.Context).
Row("Namespace", action.Namespace).
Row("Pod", action.DBPod.Name)
if action.Username != "" {
t.Row("Username", action.Username)
}
if action.Database != "" {
t.Row("Database", action.Database)
}
t.Row("File", tui.OutPath(action.Filename, r)).
Row("Pod", action.DBPod.Name).
RowIfNotEmpty("Username", action.Username).
RowIfNotEmpty("Database", action.Database).
Row("File", tui.OutPath(action.Filename, r)).
Row("Took", took.String())
if err != nil {
t.Row("Error", lipgloss.NewStyle().Renderer(r).Foreground(lipgloss.Color("1")).Render(err.Error()))
Expand Down
17 changes: 5 additions & 12 deletions internal/actions/portforward/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"

"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/clevyr/kubedb/internal/config"
kdblog "github.com/clevyr/kubedb/internal/log"
"github.com/clevyr/kubedb/internal/tui"
Expand Down Expand Up @@ -88,18 +87,12 @@ func (a PortForward) printTable() {
Row("Type", a.Dialect.PrettyName()).
Row("Namespace", a.Namespace).
Row("Hostname", "localhost").
Row("Port", strconv.Itoa(int(a.LocalPort)))
if a.Username != "" {
params.Row("Username", a.Username)
}
if a.Password != "" {
params.Row("Password", a.Password[:17])
}
if a.Database != "" {
params.Row("Database", a.Database)
}
Row("Port", strconv.Itoa(int(a.LocalPort))).
RowIfNotEmpty("Username", a.Username).
RowIfNotEmpty("Password", a.Password[:17]).
RowIfNotEmpty("Database", a.Database)

tables := []*table.Table{info, params}
tables := []*tui.Table{info, params}
widths := make([]int, 0, len(tables))
for _, t := range tables {
widths = append(widths, lipgloss.Width(t.Render()))
Expand Down
18 changes: 6 additions & 12 deletions internal/actions/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/lipgloss/table"
"github.com/clevyr/kubedb/internal/command"
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/consts"
Expand Down Expand Up @@ -226,19 +225,14 @@ func (action Restore) runInDatabasePod(ctx context.Context, r *io.PipeReader, st
return nil
}

func (action Restore) Table() *table.Table {
t := tui.MinimalTable(nil).
func (action Restore) Table() *tui.Table {
return tui.MinimalTable(nil).
Row("Context", action.Context).
Row("Namespace", action.Namespace).
Row("Pod", action.DBPod.Name)
if action.Username != "" {
t.Row("Username", action.Username)
}
if action.Database != "" {
t.Row("Database", action.Database)
}
t.Row("File", tui.InPath(action.Filename, nil))
return t
Row("Pod", action.DBPod.Name).
RowIfNotEmpty("Username", action.Username).
RowIfNotEmpty("Database", action.Database).
Row("File", tui.InPath(action.Filename, nil))
}

func (action Restore) Confirm() (bool, error) {
Expand Down
39 changes: 30 additions & 9 deletions internal/tui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,42 @@ import (
"github.com/charmbracelet/lipgloss/table"
)

type Table struct {
*table.Table
}

func (t *Table) Row(row ...string) *Table {
t.Table.Row(row...)
return t
}

func (t *Table) RowIfNotEmpty(row ...string) *Table {
for _, cell := range row {
if cell == "" {
return t
}
}
t.Table.Row(row...)
return t
}

func BorderStyle(r *lipgloss.Renderer) lipgloss.Style {
return lipgloss.NewStyle().Renderer(r).
Foreground(lipgloss.AdaptiveColor{Light: "", Dark: "243"})
}

func MinimalTable(r *lipgloss.Renderer) *table.Table {
func MinimalTable(r *lipgloss.Renderer) *Table {
colStyle := BorderStyle(r).Padding(0, 1)
firstColStyle := colStyle.Copy().Align(lipgloss.Right).Bold(true)

return table.New().
BorderStyle(BorderStyle(r)).
StyleFunc(func(_, col int) lipgloss.Style {
if col == 0 {
return firstColStyle
}
return colStyle
})
return &Table{
Table: table.New().
BorderStyle(BorderStyle(r)).
StyleFunc(func(_, col int) lipgloss.Style {
if col == 0 {
return firstColStyle
}
return colStyle
}),
}
}

0 comments on commit 2a277e6

Please sign in to comment.