diff --git a/pkg/views/artifact/select/view.go b/pkg/views/artifact/select/view.go index b2594676..1c488baf 100644 --- a/pkg/views/artifact/select/view.go +++ b/pkg/views/artifact/select/view.go @@ -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() @@ -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 } diff --git a/pkg/views/model.go b/pkg/views/base/selection/model.go similarity index 85% rename from pkg/views/model.go rename to pkg/views/base/selection/model.go index ef262042..214fbba5 100644 --- a/pkg/views/model.go +++ b/pkg/views/base/selection/model.go @@ -1,4 +1,4 @@ -package views +package selection import ( "fmt" @@ -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 @@ -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, " ")) } } @@ -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} } diff --git a/pkg/views/base/tablelist/model.go b/pkg/views/base/tablelist/model.go new file mode 100644 index 00000000..9dc4b48e --- /dev/null +++ b/pkg/views/base/tablelist/model.go @@ -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" +} diff --git a/pkg/views/project/list/view.go b/pkg/views/project/list/view.go index c107bb7a..0cf10be9 100644 --- a/pkg/views/project/list/view.go +++ b/pkg/views/project/list/view.go @@ -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}, @@ -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 { @@ -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) diff --git a/pkg/views/project/logs/view.go b/pkg/views/project/logs/view.go index 4f4fcc86..51945d88 100644 --- a/pkg/views/project/logs/view.go +++ b/pkg/views/project/logs/view.go @@ -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}, @@ -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 { @@ -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) diff --git a/pkg/views/project/select/view.go b/pkg/views/project/select/view.go index 5d7669cb..a3fad35c 100644 --- a/pkg/views/project/select/view.go +++ b/pkg/views/project/select/view.go @@ -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() @@ -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 } diff --git a/pkg/views/registry/list/view.go b/pkg/views/registry/list/view.go index cd7dbf43..fbfe253f 100644 --- a/pkg/views/registry/list/view.go +++ b/pkg/views/registry/list/view.go @@ -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}, @@ -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 { @@ -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) diff --git a/pkg/views/registry/select/view.go b/pkg/views/registry/select/view.go index 59a89d5b..8fcde193 100644 --- a/pkg/views/registry/select/view.go +++ b/pkg/views/registry/select/view.go @@ -7,7 +7,7 @@ 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 RegistryList(registry []*models.Registry, choice chan<- int64) { @@ -17,10 +17,10 @@ func RegistryList(registry []*models.Registry, choice chan<- int64) { for i, r := range registry { items[r.Name] = r.ID - itemsList[i] = views.Item(r.Name) + itemsList[i] = selection.Item(r.Name) } - m := views.NewModel(itemsList, "Registry") + m := selection.NewModel(itemsList, "Registry") p, err := tea.NewProgram(m, tea.WithAltScreen()).Run() @@ -29,7 +29,7 @@ func RegistryList(registry []*models.Registry, choice chan<- int64) { os.Exit(1) } - if p, ok := p.(views.Model); ok { + if p, ok := p.(selection.Model); ok { choice <- items[p.Choice] } diff --git a/pkg/views/repository/list/view.go b/pkg/views/repository/list/view.go index ce32302b..9d5b17fc 100644 --- a/pkg/views/repository/list/view.go +++ b/pkg/views/repository/list/view.go @@ -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: "Name", Width: 24}, {Title: "Artifacts", Width: 12}, @@ -24,21 +19,6 @@ var columns = []table.Column{ {Title: "Last Modified 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 ListRepositories(repos []*models.Repository) { var rows []table.Row for _, repo := range repos { @@ -52,27 +32,7 @@ func ListRepositories(repos []*models.Repository) { }) } - 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) diff --git a/pkg/views/repository/select/view.go b/pkg/views/repository/select/view.go index b920b7c2..da30963b 100644 --- a/pkg/views/repository/select/view.go +++ b/pkg/views/repository/select/view.go @@ -8,7 +8,7 @@ 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 RepositoryList(repos []*models.Repository, choice chan<- string) { @@ -16,10 +16,10 @@ func RepositoryList(repos []*models.Repository, choice chan<- string) { for i, r := range repos { split := strings.Split(r.Name, "/") - itemsList[i] = views.Item(strings.Join(split[1:], "/")) + itemsList[i] = selection.Item(strings.Join(split[1:], "/")) } - m := views.NewModel(itemsList, "Repository") + m := selection.NewModel(itemsList, "Repository") p, err := tea.NewProgram(m, tea.WithAltScreen()).Run() @@ -28,7 +28,7 @@ func RepositoryList(repos []*models.Repository, choice chan<- string) { os.Exit(1) } - if p, ok := p.(views.Model); ok { + if p, ok := p.(selection.Model); ok { choice <- p.Choice } diff --git a/pkg/views/user/list/view.go b/pkg/views/user/list/view.go index cd74bd25..5dc527e4 100644 --- a/pkg/views/user/list/view.go +++ b/pkg/views/user/list/view.go @@ -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: "Name", Width: 16}, {Title: "Administrator", Width: 16}, @@ -23,21 +18,6 @@ var columns = []table.Column{ {Title: "Registration Time", Width: 24}, } -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 ListUsers(users []*models.UserResp) { var rows []table.Row for _, user := range users { @@ -54,27 +34,8 @@ func ListUsers(users []*models.UserResp) { }) } - 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) diff --git a/pkg/views/user/select/view.go b/pkg/views/user/select/view.go index ea3b2b4c..722a63da 100644 --- a/pkg/views/user/select/view.go +++ b/pkg/views/user/select/view.go @@ -7,7 +7,7 @@ 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 UserList(users []*models.UserResp, choice chan<- int64) { @@ -17,10 +17,10 @@ func UserList(users []*models.UserResp, choice chan<- int64) { for i, r := range users { items[r.Username] = r.UserID - itemsList[i] = views.Item(r.Username) + itemsList[i] = selection.Item(r.Username) } - m := views.NewModel(itemsList, "User") + m := selection.NewModel(itemsList, "User") p, err := tea.NewProgram(m, tea.WithAltScreen()).Run() @@ -29,7 +29,7 @@ func UserList(users []*models.UserResp, choice chan<- int64) { os.Exit(1) } - if p, ok := p.(views.Model); ok { + if p, ok := p.(selection.Model); ok { choice <- items[p.Choice] }