Skip to content

Commit

Permalink
feat: support keyword search
Browse files Browse the repository at this point in the history
support search mode if list size > 4
  • Loading branch information
zsystm committed May 19, 2024
1 parent 6316a90 commit 6d3ab0c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -23,6 +24,11 @@ func MustSelectContractABI(abis map[string]abi.ABI) abi.ABI {
Label: fmt.Sprintf("Select the contract to interact (total: %d)", len(abis)),
Items: contractNames,
Size: DefaultPromptListSize,
Searcher: func(input string, index int) bool {
// if there is a method name that contains the input, return true
return strings.Contains(contractNames[index], input)
},
StartInSearchMode: shouldSupportSearchMode(len(abis)),
}

_, selected, err := prompt.Run()
Expand Down Expand Up @@ -98,6 +104,11 @@ func MustSelectMethod(contractABI abi.ABI, rw MethodType) (string, abi.Method) {
Label: fmt.Sprintf("Select Method (total: %d)", len(methodNames)),
Items: methodNames,
Size: DefaultPromptListSize,
Searcher: func(input string, index int) bool {
// if there is a method name that contains the input, return true
return strings.Contains(methodNames[index], input)
},
StartInSearchMode: shouldSupportSearchMode(len(methodNames)),
}

_, selectedMethod, err := prompt.Run()
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

const SelectableListSize = 4

func shouldSupportSearchMode(listLen int) bool {
return listLen > SelectableListSize
}

0 comments on commit 6d3ab0c

Please sign in to comment.