github.com/wolfi-dev/wolfictl@v0.16.11/pkg/cli/components/advisory/field/list_field.go (about)

     1  package field
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	tea "github.com/charmbracelet/bubbletea"
     8  	"github.com/wolfi-dev/wolfictl/pkg/advisory"
     9  	"github.com/wolfi-dev/wolfictl/pkg/cli/components/list"
    10  	"github.com/wolfi-dev/wolfictl/pkg/cli/styles"
    11  )
    12  
    13  type ListField struct {
    14  	id             string
    15  	prompt         string
    16  	input          list.Model
    17  	done           bool
    18  	requestUpdater func(value string, req advisory.Request) advisory.Request
    19  }
    20  
    21  type ListFieldConfiguration struct {
    22  	// ID is a unique identifier for the field.
    23  	ID string
    24  
    25  	Prompt         string
    26  	Options        []string
    27  	RequestUpdater func(value string, req advisory.Request) advisory.Request
    28  }
    29  
    30  func NewListField(cfg ListFieldConfiguration) ListField {
    31  	l := list.New(cfg.Prompt, cfg.Options)
    32  	l.SelectedStyle = styles.Accented()
    33  	l.UnselectedStyle = styles.Secondary()
    34  
    35  	return ListField{
    36  		id:             cfg.ID,
    37  		prompt:         cfg.Prompt,
    38  		input:          l,
    39  		requestUpdater: cfg.RequestUpdater,
    40  	}
    41  }
    42  
    43  func (f ListField) ID() string {
    44  	return f.id
    45  }
    46  
    47  func (f ListField) UpdateRequest(req advisory.Request) advisory.Request {
    48  	value := f.Value()
    49  	return f.requestUpdater(value, req)
    50  }
    51  
    52  func (f ListField) SubmitValue() (Field, error) {
    53  	return f.setDone(), nil
    54  }
    55  
    56  func (f ListField) Update(msg tea.Msg) (Field, tea.Cmd) {
    57  	if f.done {
    58  		return f, nil
    59  	}
    60  
    61  	var cmd tea.Cmd
    62  	f.input, cmd = f.input.Update(msg)
    63  
    64  	return f, cmd
    65  }
    66  
    67  func (f ListField) setDone() ListField {
    68  	f.done = true
    69  	return f
    70  }
    71  
    72  func (f ListField) SetBlur() Field {
    73  	f.input = f.input.Blur()
    74  	return f
    75  }
    76  
    77  func (f ListField) SetFocus() (Field, tea.Cmd) {
    78  	f.input = f.input.Focus()
    79  	return f, nil
    80  }
    81  
    82  func (f ListField) IsDone() bool {
    83  	return f.done
    84  }
    85  
    86  func (f ListField) Value() string {
    87  	return f.input.SelectedItem()
    88  }
    89  
    90  func (f ListField) View() string {
    91  	var lines []string
    92  
    93  	if !f.done {
    94  		lines = append(lines, f.input.View())
    95  		helpText := fmt.Sprintf(
    96  			"%s %s %s %s",
    97  			helpKeyStyle.Render("Enter"),
    98  			helpExplanationStyle.Render("to confirm."),
    99  			helpKeyStyle.Render("Ctrl+C"),
   100  			helpExplanationStyle.Render("to quit."),
   101  		)
   102  
   103  		lines = append(lines, helpText)
   104  	} else {
   105  		lines = append(lines, f.prompt+"\n  "+f.input.SelectedItem())
   106  	}
   107  
   108  	return strings.Join(lines, "\n")
   109  }