better keep of previous answer

This commit is contained in:
Celogeek 2022-01-02 12:13:25 +01:00
parent ca23f3f190
commit 028396b43c
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
2 changed files with 18 additions and 35 deletions

View File

@ -19,8 +19,9 @@ type ImagesTagCommand struct {
TagId int `short:"t" long:"tag-id" description:"look up for the first image of this tagId"`
TagName string `short:"T" long:"tag" description:"look up for the first image of this tagName"`
ExcludeTags string `short:"x" long:"exclude" description:"exclude tag from selection"`
KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"`
MaxImages int `short:"m" long:"max" description:"loop on a maximum number of images" default:"1"`
KeepSurveyFilter bool `short:"k" long:"keep" description:"keep survey filter"`
KeepPreviousAnswer bool `short:"K" long:"keep-previous-answer" description:"Preserve previous answer"`
}
func (c *ImagesTagCommand) Execute(args []string) error {
@ -56,7 +57,7 @@ func (c *ImagesTagCommand) Execute(args []string) error {
if c.ExcludeTags != "" {
exclude = regexp.MustCompile(c.ExcludeTags)
}
selectTags := tags.Tags.Selector(exclude, c.KeepSurveyFilter)
selectTags := tags.Tags.Selector(exclude, c.KeepSurveyFilter, c.KeepPreviousAnswer)
imagesToTags := make([]int, 0, c.MaxImages)
if c.Id > 0 {

View File

@ -3,7 +3,6 @@ package piwigotools
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/AlecAivazis/survey/v2"
@ -53,11 +52,9 @@ func (t Tags) JoinIds(sep string) string {
return strings.Join(ids, sep)
}
func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
options := make([]string, 1, len(t))
options[0] = "Same as before"
func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool, keepPreviousAnswer bool) func() Tags {
options := make([]string, 0, len(t))
tags := map[string]*Tag{}
tags[options[0]] = nil
for _, tag := range t {
if exclude != nil && exclude.MatchString(tag.Name) {
continue
@ -66,7 +63,7 @@ func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
tags[tag.Name] = tag
}
var previousTags Tags
previousAnswer := []string{}
return func() Tags {
answer := []string{}
@ -74,31 +71,16 @@ func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
Message: "Tags:",
Options: options,
PageSize: 20,
Default: previousAnswer,
}, &answer, survey.WithKeepFilter(keepFilter))
result := make([]*Tag, 0, len(answer))
alreadySelected := map[int]bool{}
for _, a := range answer {
if tags[a] == nil {
for _, p := range previousTags {
if _, ok := alreadySelected[p.Id]; !ok {
result = append(result, p)
result := make([]*Tag, len(answer))
for i, a := range answer {
result[i] = tags[a]
}
alreadySelected[p.Id] = true
if keepPreviousAnswer {
previousAnswer = answer
}
} else {
if _, ok := alreadySelected[tags[a].Id]; !ok {
result = append(result, tags[a])
}
alreadySelected[tags[a].Id] = true
}
}
sort.Slice(result, func(i, j int) bool {
return result[i].Name < result[j].Name
})
previousTags = result
return result
}