Skip to content

Commit

Permalink
central utility for the charm, reusable classes
Browse files Browse the repository at this point in the history
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
  • Loading branch information
amands98 committed May 26, 2024
1 parent c6c56f2 commit a502930
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 231 deletions.
8 changes: 4 additions & 4 deletions pkg/views/artifact/select/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views"
"github.com/goharbor/harbor-cli/pkg/views/base/selection"
)

func ListArtifacts(artifacts []*models.Artifact, choice chan<- string) {
itemsList := make([]list.Item, len(artifacts))

for i, a := range artifacts {
itemsList[i] = views.Item(a.Digest)
itemsList[i] = selection.Item(a.Digest)
}

m := views.NewModel(itemsList, "Artifact")
m := selection.NewModel(itemsList, "Artifact")

p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()

Expand All @@ -26,7 +26,7 @@ func ListArtifacts(artifacts []*models.Artifact, choice chan<- string) {
os.Exit(1)
}

if p, ok := p.(views.Model); ok {
if p, ok := p.(selection.Model); ok {
choice <- p.Choice
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/views/model.go → pkg/views/base/selection/model.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package views
package selection

import (
"fmt"
Expand All @@ -7,6 +7,7 @@ import (

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/harbor-cli/pkg/views"
)

const listHeight = 14
Expand All @@ -28,10 +29,10 @@ func (d ItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list

str := fmt.Sprintf("%d. %s", index+1, i)

fn := ItemStyle.Render
fn := views.ItemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return SelectedItemStyle.Render("> " + strings.Join(s, " "))
return views.SelectedItemStyle.Render("> " + strings.Join(s, " "))
}
}

Expand All @@ -49,9 +50,9 @@ func NewModel(items []list.Item, construct string) Model {
l.Title = "Select an " + construct
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = TitleStyle
l.Styles.PaginationStyle = PaginationStyle
l.Styles.HelpStyle = HelpStyle
l.Styles.Title = views.TitleStyle
l.Styles.PaginationStyle = views.PaginationStyle
l.Styles.HelpStyle = views.HelpStyle

return Model{List: l}
}
Expand Down
50 changes: 50 additions & 0 deletions pkg/views/base/tablelist/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tablelist

import (
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/harbor-cli/pkg/views"
)

type Model struct {
Table table.Model
}

func NewModel(columns []table.Column, rows []table.Row, height int) Model {
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(height),
)

// Set the styles for the table
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
Bold(false)

s.Selected = s.Selected.
Foreground(s.Cell.GetForeground()).
Background(s.Cell.GetBackground()).
Bold(false)
t.SetStyles(s)

return Model{Table: t}
}

func (m Model) Init() tea.Cmd {
return tea.Quit
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.Table, cmd = m.Table.Update(msg)
return m, cmd
}

func (m Model) View() string {
return views.BaseStyle.Render(m.Table.View()) + "\n"
}
43 changes: 2 additions & 41 deletions pkg/views/project/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ import (

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "Project Name", Width: 12},
{Title: "Access Level", Width: 12},
Expand All @@ -25,21 +20,6 @@ var columns = []table.Column{
{Title: "Creation Time", Width: 30},
}

func (m model) Init() tea.Cmd {
return tea.Quit
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListProjects(projects []*models.Project) {
var rows []table.Row
for _, project := range projects {
Expand All @@ -63,27 +43,8 @@ func ListProjects(projects []*models.Project) {
})
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)

// Set the styles for the table
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
Bold(false)

s.Selected = s.Selected.
Foreground(s.Cell.GetForeground()).
Background(s.Cell.GetBackground()).
Bold(false)
t.SetStyles(s)
m := tablelist.NewModel(columns, rows, len(rows))

m := model{t}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
Expand Down
45 changes: 2 additions & 43 deletions pkg/views/project/logs/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import (

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).Padding(0, 1)

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "Username", Width: 12},
{Title: "Resource", Width: 24},
Expand All @@ -26,21 +19,6 @@ var columns = []table.Column{
{Title: "Timestamp", Width: 30},
}

func (m model) Init() tea.Cmd {
return tea.Quit
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return baseStyle.Render(m.table.View()) + "\n"
}

func LogsProject(logs []*models.AuditLog) {
var rows []table.Row
for _, log := range logs {
Expand All @@ -54,27 +32,8 @@ func LogsProject(logs []*models.AuditLog) {
createTime,
})
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)

// Set the styles for the table
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
Bold(false)

s.Selected = s.Selected.
Foreground(s.Cell.GetForeground()).
Background(s.Cell.GetBackground()).
Bold(false)
t.SetStyles(s)

m := model{t}
m := tablelist.NewModel(columns, rows, len(rows))
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/views/project/select/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/views"
"github.com/goharbor/harbor-cli/pkg/views/base/selection"
)

func ProjectList(project []*models.Project, choice chan<- string) {
items := make([]list.Item, len(project))
for i, p := range project {
items[i] = views.Item(p.Name)
items[i] = selection.Item(p.Name)
}

m := views.NewModel(items, "Project")
m := selection.NewModel(items, "Project")

p, err := tea.NewProgram(m, tea.WithAltScreen()).Run()

Expand All @@ -25,7 +25,7 @@ func ProjectList(project []*models.Project, choice chan<- string) {
os.Exit(1)
}

if p, ok := p.(views.Model); ok {
if p, ok := p.(selection.Model); ok {
choice <- p.Choice
}

Expand Down
58 changes: 20 additions & 38 deletions pkg/views/registry/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import (

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

type model struct {
table table.Model
}

var columns = []table.Column{
{Title: "ID", Width: 6},
{Title: "Name", Width: 12},
Expand All @@ -27,20 +22,6 @@ var columns = []table.Column{
// {Title: "Description", Width: 12},
}

func (m model) Init() tea.Cmd {
return tea.Quit
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return views.BaseStyle.Render(m.table.View()) + "\n"
}

func ListRegistry(registry []*models.Registry) {
var rows []table.Row
for _, regis := range registry {
Expand All @@ -56,27 +37,28 @@ func ListRegistry(registry []*models.Registry) {
})
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)
// t := table.New(
// table.WithColumns(columns),
// table.WithRows(rows),
// table.WithFocused(true),
// table.WithHeight(len(rows)),
// )

// // Set the styles for the table
// s := table.DefaultStyles()
// s.Header = s.Header.
// BorderStyle(lipgloss.NormalBorder()).
// BorderBottom(true).
// Bold(false)

// Set the styles for the table
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
Bold(false)
// s.Selected = s.Selected.
// Foreground(s.Cell.GetForeground()).
// Background(s.Cell.GetBackground()).
// Bold(false)
// t.SetStyles(s)

s.Selected = s.Selected.
Foreground(s.Cell.GetForeground()).
Background(s.Cell.GetBackground()).
Bold(false)
t.SetStyles(s)
m := tablelist.NewModel(columns, rows, len(rows))

m := model{t}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
Expand Down
Loading

0 comments on commit a502930

Please sign in to comment.