summaryrefslogtreecommitdiff
path: root/internal/keepass/fzf.go
blob: a30e7692c2197268d563524f4a0a5f3567c217e5 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package keepass

import (
	"context"
	"sort"
	"strings"

	"codeberg.org/snonux/foostore/internal/picker"
	"codeberg.org/snonux/foostore/internal/store"
)

// Fzf launches fzf and returns only the selected description.
func (b *Backend) Fzf(ctx context.Context) (string, error) {
	result, err := b.FzfInteractive(ctx)
	if err != nil {
		return "", err
	}
	return result.Description, nil
}

// FzfInteractive launches fzf with action key bindings and returns the
// selected description plus the chosen action.
func (b *Backend) FzfInteractive(ctx context.Context) (store.PickerResult, error) {
	var indexes store.IndexSlice
	if err := b.WalkIndexes(ctx, "", func(idx *store.Index) error {
		indexes = append(indexes, idx)
		return nil
	}); err != nil {
		return store.PickerResult{}, err
	}
	if len(indexes) == 0 {
		return store.PickerResult{}, nil
	}

	sort.Sort(indexes)
	entries := buildPickerEntries(indexes)
	return runFzfInteractive(ctx, entries)
}

// buildPickerEntries converts a sorted IndexSlice into picker.Entry rows.
func buildPickerEntries(indexes store.IndexSlice) []picker.Entry {
	entries := make([]picker.Entry, 0, len(indexes))
	for i, idx := range indexes {
		kind := "TEXT"
		if idx.IsBinary() {
			kind = "BINARY"
		}
		hashSuffix := ""
		if len(idx.Hash) >= 63 {
			hashSuffix = idx.Hash[53:63]
		}
		entries = append(entries, picker.Entry{
			RowID:       i + 1,
			Description: idx.Description,
			Kind:        kind,
			HashSuffix:  hashSuffix,
		})
	}
	return entries
}

// runFzfInteractive calls picker.Run and maps the chosen key to a PickerAction.
func runFzfInteractive(ctx context.Context, entries []picker.Entry) (store.PickerResult, error) {
	selection, err := picker.Run(ctx, entries)
	if err != nil {
		return store.PickerResult{}, err
	}
	action, ok := parsePickerAction(selection.Key)
	if !ok || selection.Description == "" {
		return store.PickerResult{}, nil
	}
	return store.PickerResult{
		Description: selection.Description,
		Action:      action,
	}, nil
}

// parsePickerAction maps an fzf key string to a PickerAction.
func parsePickerAction(keyLine string) (store.PickerAction, bool) {
	switch strings.TrimSpace(keyLine) {
	case "", "enter":
		return store.PickerSelect, true
	case "ctrl-t", "alt-t":
		return store.PickerCat, true
	case "ctrl-y", "alt-y":
		return store.PickerPaste, true
	case "ctrl-o", "alt-o":
		return store.PickerOpen, true
	case "ctrl-e", "alt-e":
		return store.PickerEdit, true
	default:
		return "", false
	}
}