Skip to content

Commit

Permalink
Match the prompt more fuzzily
Browse files Browse the repository at this point in the history
  • Loading branch information
slarwise committed Aug 9, 2024
1 parent 0775e2d commit fd75ee0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ func main() {
prompt = nextPrompt
filteredKeys = []string{}
for _, k := range keys {
// TODO: Match more fuzzily
if strings.Contains(k, prompt) {
if matchesPrompt(prompt, k) {
filteredKeys = append(filteredKeys, k)
}
}
Expand Down Expand Up @@ -430,3 +429,19 @@ func drawPrompt(s tcell.Screen, height int, prompt string) {
func drawLoadingScreen(s tcell.Screen, height int) {
drawLine(s, 2, height-2, tcell.StyleDefault.Foreground(tcell.ColorYellow), "Loading...")
}

func matchesPrompt(prompt, s string) bool {
if len(prompt) == 0 {
return true
}
index := 0
for _, c := range []byte(s) {
if c == prompt[index] {
if index == len(prompt)-1 {
return true
}
index++
}
}
return false
}
50 changes: 50 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,56 @@ func TestGetSecret(t *testing.T) {
}
}

func TestMatchesPrompt(t *testing.T) {
tests := map[string]struct {
prompt string
key string
expected bool
}{
"match": {
prompt: "set",
key: "secret",
expected: true,
},
"match2": {
prompt: "seet",
key: "secret",
expected: true,
},
"exact-match": {
prompt: "secret",
key: "secret",
expected: true,
},
"no-match": {
prompt: "asdf",
key: "secret",
expected: false,
},
"no-match1": {
prompt: "a",
key: "secret",
expected: false,
},
"no-match2": {
prompt: "seeet",
key: "secret",
expected: false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
if matchesPrompt(test.prompt, test.key) != test.expected {
if test.expected {
t.Fatalf("Expected %s to match %s", test.prompt, test.key)
} else {
t.Fatalf("Expected %s to not match %s", test.prompt, test.key)
}
}
})
}
}

func startVault(token, addr string) (*exec.Cmd, error) {
cmd := exec.Command("vault", "server", "-dev", "-dev-root-token-id", token, "-address", addr)
if err := cmd.Start(); err != nil {
Expand Down

0 comments on commit fd75ee0

Please sign in to comment.