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

Commitcc41fe7

Browse files
committed
fix: duplicates, cursor positioning
1 parentce4b3c2 commitcc41fe7

File tree

2 files changed

+63
-17
lines changed

2 files changed

+63
-17
lines changed

‎cli/cliui/select.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ type multiSelectModel struct {
378378
messagestring
379379
canceledbool
380380
selectedbool
381-
isInputModebool// New field to track if we're adding a custom option
381+
isCustomInputModebool// New field to track if we're adding a custom option
382382
customInputstring// New field to store custom input
383383
enableCustomInputbool// New field to control whether custom input is allowed
384384
}
@@ -391,7 +391,7 @@ func (multiSelectModel) Init() tea.Cmd {
391391
func (mmultiSelectModel)Update(msg tea.Msg) (tea.Model, tea.Cmd) {
392392
varcmd tea.Cmd
393393

394-
ifm.isInputMode {
394+
ifm.isCustomInputMode {
395395
returnm.handleCustomInputMode(msg)
396396
}
397397

@@ -409,7 +409,7 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
409409
casetea.KeyEnter:
410410
// Switch to custom input mode if we're on the "+ Add custom value:" option
411411
ifm.enableCustomInput&&m.cursor==len(m.filteredOptions()) {
412-
m.isInputMode=true
412+
m.isCustomInputMode=true
413413
returnm,nil
414414
}
415415
iflen(m.options)!=0 {
@@ -427,17 +427,15 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
427427
returnm,nil
428428

429429
casetea.KeyUp:
430-
options:=m.filteredOptions()
431-
maxIndex:=len(options)
430+
maxIndex:=m.getMaxIndex()
432431
ifm.cursor>0 {
433432
m.cursor--
434433
}else {
435434
m.cursor=maxIndex
436435
}
437436

438437
casetea.KeyDown:
439-
options:=m.filteredOptions()
440-
maxIndex:=len(options)
438+
maxIndex:=m.getMaxIndex()
441439
ifm.cursor<maxIndex {
442440
m.cursor++
443441
}else {
@@ -473,6 +471,16 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
473471
returnm,cmd
474472
}
475473

474+
func (mmultiSelectModel)getMaxIndex()int {
475+
options:=m.filteredOptions()
476+
ifm.enableCustomInput {
477+
// Include the "+ Add custom value" entry
478+
returnlen(options)
479+
}
480+
// Includes only the actual options
481+
returnlen(options)-1
482+
}
483+
476484
// handleCustomInputMode manages keyboard interactions when in custom input mode
477485
func (m*multiSelectModel)handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cmd) {
478486
keyMsg,ok:=msg.(tea.KeyMsg)
@@ -499,15 +507,44 @@ func (m *multiSelectModel) handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cm
499507

500508
// handleCustomInputSubmission processes the submission of custom input
501509
func (m*multiSelectModel)handleCustomInputSubmission() (tea.Model, tea.Cmd) {
502-
ifm.customInput!="" {
503-
m.options=append(m.options,&multiSelectOption{
504-
option:m.customInput,
505-
chosen:true,
506-
})
510+
ifm.customInput=="" {
511+
m.isCustomInputMode=false
512+
returnm,nil
513+
}
514+
515+
// Clear search to ensure option is visible and cursor points to the new option
516+
m.search.SetValue("")
517+
518+
// Check for duplicates
519+
fori,opt:=rangem.options {
520+
ifstrings.EqualFold(opt.option,m.customInput) {
521+
// If the option exists but isn't chosen, select it
522+
if!opt.chosen {
523+
opt.chosen=true
524+
}
525+
526+
// Point cursor to the new option
527+
m.cursor=i
528+
529+
// Reset custom input mode to disabled
530+
m.isCustomInputMode=false
531+
m.customInput=""
532+
returnm,nil
533+
}
507534
}
508-
// Reset input state regardless of whether input was empty
535+
536+
// Add new unique option
537+
m.options=append(m.options,&multiSelectOption{
538+
option:m.customInput,
539+
chosen:true,
540+
})
541+
542+
// Point cursor to the newly added option
543+
m.cursor=len(m.options)-1
544+
545+
// Reset custom input mode to disabled
509546
m.customInput=""
510-
m.isInputMode=false
547+
m.isCustomInputMode=false
511548
returnm,nil
512549
}
513550

@@ -531,7 +568,7 @@ func (m multiSelectModel) View() string {
531568
returns.String()
532569
}
533570

534-
ifm.isInputMode {
571+
ifm.isCustomInputMode {
535572
_,_=s.WriteString(fmt.Sprintf("%s\nEnter custom value: %s\n",msg,m.customInput))
536573
returns.String()
537574
}

‎cli/prompts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func (RootCmd) promptExample() *serpent.Command {
4141
Default:"",
4242
Value:serpent.StringArrayOf(&multiSelectValues),
4343
}
44+
45+
enableCustomInputbool
46+
enableCustomInputOption= serpent.Option{
47+
Name:"enable-custom-input",
48+
Description:"Enable custom input option in multi-select.",
49+
Required:false,
50+
Flag:"enable-custom-input",
51+
Value:serpent.BoolOf(&enableCustomInput),
52+
}
4453
)
4554
cmd:=&serpent.Command{
4655
Use:"prompt-example",
@@ -159,12 +168,12 @@ func (RootCmd) promptExample() *serpent.Command {
159168
"Code","Chairs","Whale","Diamond","Carrot",
160169
},
161170
Defaults: []string{"Code"},
162-
EnableCustomInput:true,
171+
EnableCustomInput:enableCustomInput,
163172
})
164173
}
165174
_,_=fmt.Fprintf(inv.Stdout,"%q are nice choices.\n",strings.Join(multiSelectValues,", "))
166175
returnmultiSelectError
167-
},useThingsOption),
176+
},useThingsOption,enableCustomInputOption),
168177
promptCmd("rich-parameter",func(inv*serpent.Invocation)error {
169178
value,err:=cliui.RichSelect(inv, cliui.RichSelectOptions{
170179
Options: []codersdk.TemplateVersionParameterOption{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp