-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
108 lines (92 loc) · 2.67 KB
/
prompt.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"github.com/manifoldco/promptui"
)
type MusicMetadata struct {
Artist string
SongName string
Album string
Score float64
Sources int
}
func (m *MusicMetadata) Copy() MusicMetadata {
return MusicMetadata{
Artist: m.Artist,
SongName: m.SongName,
Album: m.Album,
Score: m.Score,
Sources: m.Sources,
}
}
// PromptSelectMatch is the first step of interactions with the user.
// Lists all the matches and a brieve list of details for each one.
func PromptSelectMatch(filename string, input []MusicMetadata) (int, error) {
templates := &promptui.SelectTemplates{
Label: "Select match for: {{ .FileName | red }}",
Active: "▸ {{ .Artist }} - {{ .SongName }}",
Inactive: " {{ .Artist | faint }} - {{ .SongName | faint }}",
Details: `
--------- Details ----------
{{ "Artist:" | faint }} {{ .Artist }}
{{ "Title:" | faint }} {{ .SongName }}
{{ "Album:" | faint }} {{ .Album }}
{{ "Score:" | faint }} {{ .Score }}
{{ "Sources:" | faint }} {{ .Sources }}`,
}
prompt := promptui.Select{
Label: struct{ FileName string }{FileName: filename},
Items: input,
Templates: templates,
Size: 6,
HideSelected: true,
}
index, _, err := prompt.Run()
if err != nil {
return -1, err
}
return index, nil
}
type MusicTags struct {
Tag string
NewValue string
OldValue string
}
// PromptSelectTag is the second step of interactions with the user.
// Shows the current and new id3 tags from the matched song.
func PromptSelectTag(filename string, input []MusicTags) (int, error) {
var index int
templates := &promptui.SelectTemplates{
Label: "Edit tags for: {{ .FileName | red }}",
Active: "{{ `▸` | green }} {{ .Tag }}{{with .NewValue}}: {{.}}{{end}} {{with .OldValue}} {{ `<` | faint }} {{ . | faint }} {{end}}",
Inactive: " {{ .Tag }}{{with .NewValue}}: {{.}}{{end}} {{with .OldValue}} {{ `<` | faint }} {{ . | faint }} {{end}}",
}
prompt := promptui.Select{
Label: struct{ FileName string }{FileName: filename},
Items: input,
Templates: templates,
Size: 4, // match number of tags + 1 (option to continue)
HideSelected: true,
}
index, _, err := prompt.Run()
if err != nil {
fmt.Printf("failed to run prompt %s\n", err)
return -1, err
}
return index, nil
}
// PromptNewValue is the third and last step of interactions with the user.
// Asks for an input from the user.
func PromptNewValue(oldValue string) (string, error) {
prompt := promptui.Prompt{
Label: "New value",
Default: oldValue,
HideEntered: true,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return "", err
}
return result, nil
}