-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzy.go
58 lines (50 loc) · 1.52 KB
/
fuzzy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// This file is part of termOTP, a TOTP program for your terminal.
// https://github.com/marcopaganini/termotp.
package main
import (
"fmt"
"github.com/ktr0731/go-fuzzyfinder"
)
const (
fuzzyPadding = 3
defaultIssuerName = "(no issuer name)"
)
// Return the maximum length of one of the otp entries (returned
// by function 'f'.
func maxlen(vault []otpEntry, f func(v otpEntry) string) int {
max := 0
for _, entry := range vault {
length := len(f(entry))
if length == 0 {
length = len(defaultIssuerName)
}
if length > max {
max = length
}
}
return max
}
// fuzzyFind opens a fuzzy finder window and allows the user to select the
// desired issuer/account and returns the token.
func fuzzyFind(vault []otpEntry) (string, error) {
maxIssuerLen := maxlen(vault, func(otp otpEntry) string { return otp.Issuer })
maxAccountLen := maxlen(vault, func(otp otpEntry) string { return otp.Account })
maxTokenLen := maxlen(vault, func(otp otpEntry) string { return otp.Token })
idx, err := fuzzyfinder.Find(
vault,
func(i int) string {
// Default issuer name
issuer := defaultIssuerName
if vault[i].Issuer != "" {
issuer = vault[i].Issuer
}
issuer = fmt.Sprintf("%-[1]*s", maxIssuerLen+fuzzyPadding, issuer)
account := fmt.Sprintf("%-[1]*s", maxAccountLen+fuzzyPadding, vault[i].Account)
token := fmt.Sprintf("%-[1]*s", maxTokenLen+fuzzyPadding, vault[i].Token)
return fmt.Sprintf("%s %s %s", issuer, account, token)
})
if err != nil {
return "", err
}
return vault[idx].Token, nil
}