Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions completer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package console

import (
"fmt"
"strings"

"github.com/carapace-sh/carapace"
Expand All @@ -12,8 +13,14 @@ import (
"github.com/reeflective/console/internal/line"
)

func (c *Console) complete(input []rune, pos int) readline.Completions {
func (c *Console) complete(input []rune, pos int) (comps readline.Completions) {
menu := c.activeMenu()
defer func() {
if r := recover(); r != nil {
comps = readline.CompleteMessage(fmt.Sprintf("completion error: %v", r))
menu.resetPreRun()
}
}()

// Ensure the carapace library is called so that the function
// completer.Complete() variable is correctly initialized before use.
Expand Down Expand Up @@ -56,7 +63,7 @@ func (c *Console) complete(input []rune, pos int) readline.Completions {
}

// Assign both completions and command/flags/args usage strings.
comps := readline.CompleteRaw(raw)
comps = readline.CompleteRaw(raw)
comps = comps.Usage("%s", completions.Usage)
comps = c.justifyCommandComps(comps)

Expand Down
21 changes: 21 additions & 0 deletions completer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package console

import "testing"

func TestCompleteRecoversFromPanic(t *testing.T) {
c := New("test")
menu := c.activeMenu()
menu.Command = nil

defer func() {
if r := recover(); r != nil {
t.Fatalf("complete panicked: %v", r)
}
}()

_ = c.complete(nil, 0)

if menu.Command == nil {
t.Fatal("complete did not restore the command tree after panic")
}
}
Loading