Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: add cherry-picks for patch 2.18.2#16061

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
stirby merged 3 commits intorelease/2.18frompatch/2.18.2
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 132 additions & 17 deletionscli/cliui/select.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -300,9 +300,10 @@ func (m selectModel) filteredOptions() []string {
}

typeMultiSelectOptionsstruct {
Messagestring
Options []string
Defaults []string
Messagestring
Options []string
Defaults []string
EnableCustomInputbool
}

funcMultiSelect(inv*serpent.Invocation,optsMultiSelectOptions) ([]string,error) {
Expand All@@ -328,9 +329,10 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er
}

initialModel:=multiSelectModel{
search:textinput.New(),
options:options,
message:opts.Message,
search:textinput.New(),
options:options,
message:opts.Message,
enableCustomInput:opts.EnableCustomInput,
}

initialModel.search.Prompt=""
Expand DownExpand Up@@ -370,12 +372,15 @@ type multiSelectOption struct {
}

typemultiSelectModelstruct {
search textinput.Model
options []*multiSelectOption
cursorint
messagestring
canceledbool
selectedbool
search textinput.Model
options []*multiSelectOption
cursorint
messagestring
canceledbool
selectedbool
isCustomInputModebool// track if we're adding a custom option
customInputstring// store custom input
enableCustomInputbool// control whether custom input is allowed
}

func (multiSelectModel)Init() tea.Cmd {
Expand All@@ -386,6 +391,10 @@ func (multiSelectModel) Init() tea.Cmd {
func (mmultiSelectModel)Update(msg tea.Msg) (tea.Model, tea.Cmd) {
varcmd tea.Cmd

ifm.isCustomInputMode {
returnm.handleCustomInputMode(msg)
}

switchmsg:=msg.(type) {
caseterminateMsg:
m.canceled=true
Expand All@@ -398,6 +407,11 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
returnm,tea.Quit

casetea.KeyEnter:
// Switch to custom input mode if we're on the "+ Add custom value:" option
ifm.enableCustomInput&&m.cursor==len(m.filteredOptions()) {
m.isCustomInputMode=true
returnm,nil
}
iflen(m.options)!=0 {
m.selected=true
returnm,tea.Quit
Expand All@@ -413,16 +427,16 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
returnm,nil

casetea.KeyUp:
options:=m.filteredOptions()
maxIndex:=m.getMaxIndex()
ifm.cursor>0 {
m.cursor--
}else {
m.cursor=len(options)-1
m.cursor=maxIndex
}

casetea.KeyDown:
options:=m.filteredOptions()
ifm.cursor<len(options)-1 {
maxIndex:=m.getMaxIndex()
ifm.cursor<maxIndex {
m.cursor++
}else {
m.cursor=0
Expand DownExpand Up@@ -457,6 +471,91 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
returnm,cmd
}

func (mmultiSelectModel)getMaxIndex()int {
options:=m.filteredOptions()
ifm.enableCustomInput {
// Include the "+ Add custom value" entry
returnlen(options)
}
// Includes only the actual options
returnlen(options)-1
}

// handleCustomInputMode manages keyboard interactions when in custom input mode
func (m*multiSelectModel)handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cmd) {
keyMsg,ok:=msg.(tea.KeyMsg)
if!ok {
returnm,nil
}

switchkeyMsg.Type {
casetea.KeyEnter:
returnm.handleCustomInputSubmission()

casetea.KeyCtrlC:
m.canceled=true
returnm,tea.Quit

casetea.KeyBackspace:
returnm.handleCustomInputBackspace()

default:
m.customInput+=keyMsg.String()
returnm,nil
}
}

// handleCustomInputSubmission processes the submission of custom input
func (m*multiSelectModel)handleCustomInputSubmission() (tea.Model, tea.Cmd) {
ifm.customInput=="" {
m.isCustomInputMode=false
returnm,nil
}

// Clear search to ensure option is visible and cursor points to the new option
m.search.SetValue("")

// Check for duplicates
fori,opt:=rangem.options {
ifopt.option==m.customInput {
// If the option exists but isn't chosen, select it
if!opt.chosen {
opt.chosen=true
}

// Point cursor to the new option
m.cursor=i

// Reset custom input mode to disabled
m.isCustomInputMode=false
m.customInput=""
returnm,nil
}
}

// Add new unique option
m.options=append(m.options,&multiSelectOption{
option:m.customInput,
chosen:true,
})

// Point cursor to the newly added option
m.cursor=len(m.options)-1

// Reset custom input mode to disabled
m.customInput=""
m.isCustomInputMode=false
returnm,nil
}

// handleCustomInputBackspace handles backspace in custom input mode
func (m*multiSelectModel)handleCustomInputBackspace() (tea.Model, tea.Cmd) {
iflen(m.customInput)>0 {
m.customInput=m.customInput[:len(m.customInput)-1]
}
returnm,nil
}

func (mmultiSelectModel)View()string {
vars strings.Builder

Expand All@@ -469,13 +568,19 @@ func (m multiSelectModel) View() string {
returns.String()
}

ifm.isCustomInputMode {
_,_=s.WriteString(fmt.Sprintf("%s\nEnter custom value: %s\n",msg,m.customInput))
returns.String()
}

_,_=s.WriteString(fmt.Sprintf(
"%s %s[Use arrows to move, space to select, <right> to all, <left> to none, type to filter]\n",
msg,
m.search.View(),
))

fori,option:=rangem.filteredOptions() {
options:=m.filteredOptions()
fori,option:=rangeoptions {
cursor:=" "
chosen:="[ ]"
o:=option.option
Expand All@@ -498,6 +603,16 @@ func (m multiSelectModel) View() string {
))
}

ifm.enableCustomInput {
// Add the "+ Add custom value" option at the bottom
cursor:=" "
text:=" + Add custom value"
ifm.cursor==len(options) {
cursor=pretty.Sprint(DefaultStyles.Keyword,"> ")
text=pretty.Sprint(DefaultStyles.Keyword,text)
}
_,_=s.WriteString(fmt.Sprintf("%s%s\n",cursor,text))
}
returns.String()
}

Expand Down
33 changes: 33 additions & 0 deletionscli/cliui/select_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -101,6 +101,39 @@ func TestMultiSelect(t *testing.T) {
}()
require.Equal(t,items,<-msgChan)
})

t.Run("MultiSelectWithCustomInput",func(t*testing.T) {
t.Parallel()
items:= []string{"Code","Chairs","Whale","Diamond","Carrot"}
ptty:=ptytest.New(t)
msgChan:=make(chan []string)
gofunc() {
resp,err:=newMultiSelectWithCustomInput(ptty,items)
assert.NoError(t,err)
msgChan<-resp
}()
require.Equal(t,items,<-msgChan)
})
}

funcnewMultiSelectWithCustomInput(ptty*ptytest.PTY,items []string) ([]string,error) {
varvalues []string
cmd:=&serpent.Command{
Handler:func(inv*serpent.Invocation)error {
selectedItems,err:=cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Options:items,
Defaults:items,
EnableCustomInput:true,
})
iferr==nil {
values=selectedItems
}
returnerr
},
}
inv:=cmd.Invoke()
ptty.Attach(inv)
returnvalues,inv.Run()
}

funcnewMultiSelect(ptty*ptytest.PTY,items []string) ([]string,error) {
Expand Down
16 changes: 13 additions & 3 deletionscli/prompts.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,6 +41,15 @@ func (RootCmd) promptExample() *serpent.Command {
Default:"",
Value:serpent.StringArrayOf(&multiSelectValues),
}

enableCustomInputbool
enableCustomInputOption= serpent.Option{
Name:"enable-custom-input",
Description:"Enable custom input option in multi-select.",
Required:false,
Flag:"enable-custom-input",
Value:serpent.BoolOf(&enableCustomInput),
}
)
cmd:=&serpent.Command{
Use:"prompt-example",
Expand DownExpand Up@@ -156,14 +165,15 @@ func (RootCmd) promptExample() *serpent.Command {
multiSelectValues,multiSelectError=cliui.MultiSelect(inv, cliui.MultiSelectOptions{
Message:"Select some things:",
Options: []string{
"Code","Chair","Whale","Diamond","Carrot",
"Code","Chairs","Whale","Diamond","Carrot",
},
Defaults: []string{"Code"},
Defaults: []string{"Code"},
EnableCustomInput:enableCustomInput,
})
}
_,_=fmt.Fprintf(inv.Stdout,"%q are nice choices.\n",strings.Join(multiSelectValues,", "))
returnmultiSelectError
},useThingsOption),
},useThingsOption,enableCustomInputOption),
promptCmd("rich-parameter",func(inv*serpent.Invocation)error {
value,err:=cliui.RichSelect(inv, cliui.RichSelectOptions{
Options: []codersdk.TemplateVersionParameterOption{
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp