Skip to content
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

feat(select): added additional navigation keys #572

Merged
merged 1 commit into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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