-
Notifications
You must be signed in to change notification settings - Fork 142
refactor(robot): introduce listselect model for project prompts #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
05ef593
a722865
577d468
306ddd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,5 +23,6 @@ go.work | |
|
|
||
| /harbor | ||
| dist/ | ||
| demo/ | ||
| /dagger.gen.go | ||
| /internal/* | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package listselect | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/charmbracelet/bubbles/list" | ||
| tea "github.com/charmbracelet/bubbletea" | ||
| "github.com/goharbor/harbor-cli/pkg/views" | ||
| ) | ||
|
|
||
| const listHeight = 14 | ||
|
|
||
| type Item string | ||
|
|
||
| func (i Item) FilterValue() string { return string(i) } | ||
|
|
||
| type ItemDelegate struct { | ||
| Selected *map[int]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 | ||
| } | ||
|
|
||
| checked := " " | ||
| if d.Selected != nil { | ||
| if _, ok := (*d.Selected)[index]; ok { | ||
| checked = "✓" | ||
| } | ||
| } | ||
|
|
||
| str := fmt.Sprintf("[%s] %d. %s", checked, index+1, i) | ||
|
|
||
| fn := views.ItemStyle.Render | ||
| if index == m.Index() { | ||
| fn = func(s ...string) string { | ||
| return views.SelectedItemStyle.Render("> " + strings.Join(s, " ")) | ||
| } | ||
| } | ||
|
|
||
| fmt.Fprint(w, fn(str)) | ||
| } | ||
|
|
||
| type Model struct { | ||
| List list.Model | ||
| Choices []string | ||
| Selected map[int]struct{} | ||
| Aborted bool | ||
| } | ||
|
|
||
| func NewModel(items []list.Item, construct string) Model { | ||
| const defaultWidth = 20 | ||
| selected := make(map[int]struct{}) | ||
| l := list.New(items, ItemDelegate{Selected: &selected}, defaultWidth, listHeight) | ||
| l.Title = "Select one or more " + construct + " (space to toggle, enter to confirm)" | ||
| l.SetShowStatusBar(false) | ||
| l.SetFilteringEnabled(true) | ||
| l.Styles.Title = views.TitleStyle | ||
| l.Styles.PaginationStyle = views.PaginationStyle | ||
| l.Styles.HelpStyle = views.HelpStyle | ||
|
|
||
| return Model{List: l, Selected: selected} | ||
| } | ||
|
|
||
| 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 " ": | ||
| idx := m.List.Index() | ||
| if _, ok := m.Selected[idx]; ok { | ||
| delete(m.Selected, idx) | ||
| } else { | ||
| m.Selected[idx] = struct{}{} | ||
| } | ||
| return m, nil | ||
| case "enter": | ||
| for idx := range m.Selected { | ||
| if i, ok := m.List.Items()[idx].(Item); ok { | ||
| m.Choices = append(m.Choices, string(i)) | ||
| } | ||
| } | ||
|
Comment on lines
+127
to
+131
|
||
| return m, tea.Quit | ||
qcserestipy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case "ctrl+c", "esc": | ||
| m.Aborted = true | ||
| 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.Aborted { | ||
| return "" | ||
| } | ||
| if len(m.Choices) > 0 { | ||
| return fmt.Sprintf("Selected: %s\n", strings.Join(m.Choices, ", ")) | ||
| } | ||
| return "\n" + m.List.View() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Help needs to have a "space" in the help menu