Merge pull request #549 from BayLee4/main

Enhance tab completion
This commit is contained in:
Zack 2023-03-15 08:11:00 -07:00 committed by GitHub
commit e1b49e6d1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import (
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
@ -365,10 +366,21 @@ func (t TabComplete) Do(line []rune, pos int) ([][]rune, int) {
// No completion
return [][]rune{[]rune("")}, 0
}
if len(words) == 1 && nbCharacter == utils.NbPinNumbers {
// Check if word is indeed a number
_, err := strconv.Atoi(lastPartialWord)
if err == nil {
return [][]rune{[]rune("-")}, nbCharacter
}
}
var strArray [][]rune
for _, s := range mnemonicode.WordList {
if strings.HasPrefix(s, lastPartialWord) {
strArray = append(strArray, []rune(s[nbCharacter:]))
var completionCandidate = s[nbCharacter:]
if len(words) <= mnemonicode.WordsRequired(utils.NbBytesWords) {
completionCandidate += "-"
}
strArray = append(strArray, []rune(completionCandidate))
}
}
return strArray, nbCharacter

View File

@ -27,6 +27,9 @@ import (
"github.com/schollz/mnemonicode"
)
const NbPinNumbers = 4
const NbBytesWords = 4
// Get or create home directory
func GetConfigDir() (homedir string, err error) {
homedir, err = os.UserHomeDir()
@ -186,7 +189,7 @@ func GenerateRandomPin() string {
s := ""
max := new(big.Int)
max.SetInt64(9)
for i := 0; i < 4; i++ {
for i := 0; i < NbPinNumbers; i++ {
v, err := rand.Int(rand.Reader, max)
if err != nil {
panic(err)
@ -199,7 +202,7 @@ func GenerateRandomPin() string {
// GetRandomName returns mnemonicoded random name
func GetRandomName() string {
var result []string
bs := make([]byte, 4)
bs := make([]byte, NbBytesWords)
rand.Read(bs)
result = mnemonicode.EncodeWordList(result, bs)
return GenerateRandomPin() + "-" + strings.Join(result, "-")