Skip to content

Commit

Permalink
feat(select): added additional navigation keys (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpcsc committed Sep 16, 2023
1 parent 2fcdfcf commit fdca900
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions interactive_multiselect_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (p *InteractiveMultiselectPrinter) Show(text ...string) ([]string, error) {
p.selectedOptions = append(p.selectedOptions, i)
}
area.Update(p.renderSelectMenu())
case keys.Up:
case keys.Up, keys.CtrlP:
if len(p.fuzzySearchMatches) == 0 {
return false, nil
}
Expand All @@ -263,7 +263,7 @@ func (p *InteractiveMultiselectPrinter) Show(text ...string) ([]string, error) {
}

area.Update(p.renderSelectMenu())
case keys.Down:
case keys.Down, keys.CtrlN:
if len(p.fuzzySearchMatches) == 0 {
return false, nil
}
Expand Down
13 changes: 13 additions & 0 deletions interactive_multiselect_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func TestInteractiveMultiselectPrinter_Show_MaxHeightSlidingWindow(t *testing.T)
testza.AssertEqual(t, []string{"b", "e"}, result)
}

func TestInteractiveMultiselectPrinter_Show_AlternateNavigationKeys(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.CtrlN)
keyboard.SimulateKeyPress(keys.CtrlN)
keyboard.SimulateKeyPress(keys.CtrlN)
keyboard.SimulateKeyPress(keys.CtrlP)
keyboard.SimulateKeyPress(keys.Enter)
keyboard.SimulateKeyPress(keys.Tab)
}()
result, _ := pterm.DefaultInteractiveMultiselect.WithOptions([]string{"a", "b", "c", "d", "e"}).WithDefaultOptions([]string{"b"}).Show()
testza.AssertEqual(t, []string{"b", "c"}, result)
}

func TestInteractiveMultiselectPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveMultiselect.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
Expand Down
4 changes: 2 additions & 2 deletions interactive_select_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {
p.displayedOptions = append([]string{}, p.fuzzySearchMatches[p.displayedOptionsStart:p.displayedOptionsEnd]...)

area.Update(p.renderSelectMenu())
case keys.Up:
case keys.Up, keys.CtrlP:
if len(p.fuzzySearchMatches) == 0 {
return false, nil
}
Expand All @@ -219,7 +219,7 @@ func (p *InteractiveSelectPrinter) Show(text ...string) (string, error) {
}

area.Update(p.renderSelectMenu())
case keys.Down:
case keys.Down, keys.CtrlN:
if len(p.fuzzySearchMatches) == 0 {
return false, nil
}
Expand Down
11 changes: 11 additions & 0 deletions interactive_select_printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func TestInteractiveSelectPrinter_Show_MaxHeightSlidingWindow(t *testing.T) {
testza.AssertEqual(t, "c", result)
}

func TestInteractiveSelectPrinter_Show_AlternateNavigationKeys(t *testing.T) {
go func() {
keyboard.SimulateKeyPress(keys.CtrlN)
keyboard.SimulateKeyPress(keys.CtrlN)
keyboard.SimulateKeyPress(keys.CtrlP)
keyboard.SimulateKeyPress(keys.Enter)
}()
result, _ := pterm.DefaultInteractiveSelect.WithOptions([]string{"a", "b", "c", "d", "e"}).WithDefaultOption("b").Show()
testza.AssertEqual(t, "c", result)
}

func TestInteractiveSelectPrinter_WithDefaultText(t *testing.T) {
p := pterm.DefaultInteractiveSelect.WithDefaultText("default")
testza.AssertEqual(t, p.DefaultText, "default")
Expand Down

0 comments on commit fdca900

Please sign in to comment.