-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deletion of registry through cli tui
Signed-off-by: amands98 <amandeepsm.in@gmail.com>
- Loading branch information
Showing
5 changed files
with
172 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package project | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/list" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/goharbor/go-client/pkg/sdk/v2.0/models" | ||
) | ||
|
||
const listHeight = 14 | ||
|
||
var ( | ||
titleStyle = lipgloss.NewStyle().MarginLeft(2) | ||
itemStyle = lipgloss.NewStyle().PaddingLeft(4) | ||
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170")) | ||
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4) | ||
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1) | ||
) | ||
|
||
type item string | ||
|
||
func (i item) FilterValue() string { return "" } | ||
|
||
type itemDelegate struct{} | ||
|
||
func (d itemDelegate) Height() int { return 1 } | ||
func (d itemDelegate) Spacing() int { return 0 } | ||
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil } | ||
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) { | ||
i, ok := listItem.(item) | ||
if !ok { | ||
return | ||
} | ||
|
||
str := fmt.Sprintf("%d. %s", index+1, i) | ||
|
||
fn := itemStyle.Render | ||
if index == m.Index() { | ||
fn = func(s ...string) string { | ||
return selectedItemStyle.Render("> " + strings.Join(s, " ")) | ||
} | ||
} | ||
|
||
fmt.Fprint(w, fn(str)) | ||
} | ||
|
||
type model struct { | ||
list list.Model | ||
choice string | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.WindowSizeMsg: | ||
m.list.SetWidth(msg.Width) | ||
return m, nil | ||
|
||
case tea.KeyMsg: | ||
switch keypress := msg.String(); keypress { | ||
case "enter": | ||
i, ok := m.list.SelectedItem().(item) | ||
if ok { | ||
m.choice = string(i) | ||
} | ||
return m, tea.Quit | ||
} | ||
} | ||
|
||
var cmd tea.Cmd | ||
m.list, cmd = m.list.Update(msg) | ||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
if m.choice != "" { | ||
return "" | ||
} | ||
return "\n" + m.list.View() | ||
} | ||
|
||
func RegistryList(registry []*models.Registry, choice chan<- int64) { | ||
itemsList := make([]list.Item, len(registry)) | ||
|
||
items := map[string]int64{} | ||
|
||
for i, r := range registry { | ||
items[r.Name] = r.ID | ||
itemsList[i] = item(r.Name) | ||
} | ||
|
||
const defaultWidth = 20 | ||
|
||
l := list.New(itemsList, itemDelegate{}, defaultWidth, listHeight) | ||
l.Title = "Select a Registry" | ||
l.SetShowStatusBar(false) | ||
l.SetFilteringEnabled(false) | ||
l.Styles.Title = titleStyle | ||
l.Styles.PaginationStyle = paginationStyle | ||
l.Styles.HelpStyle = helpStyle | ||
|
||
m := model{list: l} | ||
|
||
p, err := tea.NewProgram(m, tea.WithAltScreen()).Run() | ||
|
||
if err != nil { | ||
fmt.Println("Error running program:", err) | ||
os.Exit(1) | ||
} | ||
|
||
if p, ok := p.(model); ok { | ||
choice <- items[p.choice] | ||
} | ||
|
||
} |