mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 10:12:37 +02:00
better keep of previous answer
This commit is contained in:
parent
ca23f3f190
commit
028396b43c
@ -15,12 +15,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ImagesTagCommand struct {
|
type ImagesTagCommand struct {
|
||||||
Id int `short:"i" long:"id" description:"image id to tag"`
|
Id int `short:"i" long:"id" description:"image id to tag"`
|
||||||
TagId int `short:"t" long:"tag-id" description:"look up for the first image of this tagId"`
|
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"`
|
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"`
|
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"`
|
||||||
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 {
|
func (c *ImagesTagCommand) Execute(args []string) error {
|
||||||
@ -56,7 +57,7 @@ func (c *ImagesTagCommand) Execute(args []string) error {
|
|||||||
if c.ExcludeTags != "" {
|
if c.ExcludeTags != "" {
|
||||||
exclude = regexp.MustCompile(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)
|
imagesToTags := make([]int, 0, c.MaxImages)
|
||||||
if c.Id > 0 {
|
if c.Id > 0 {
|
||||||
|
@ -3,7 +3,6 @@ package piwigotools
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
@ -53,11 +52,9 @@ func (t Tags) JoinIds(sep string) string {
|
|||||||
return strings.Join(ids, sep)
|
return strings.Join(ids, sep)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
|
func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool, keepPreviousAnswer bool) func() Tags {
|
||||||
options := make([]string, 1, len(t))
|
options := make([]string, 0, len(t))
|
||||||
options[0] = "Same as before"
|
|
||||||
tags := map[string]*Tag{}
|
tags := map[string]*Tag{}
|
||||||
tags[options[0]] = nil
|
|
||||||
for _, tag := range t {
|
for _, tag := range t {
|
||||||
if exclude != nil && exclude.MatchString(tag.Name) {
|
if exclude != nil && exclude.MatchString(tag.Name) {
|
||||||
continue
|
continue
|
||||||
@ -66,7 +63,7 @@ func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
|
|||||||
tags[tag.Name] = tag
|
tags[tag.Name] = tag
|
||||||
}
|
}
|
||||||
|
|
||||||
var previousTags Tags
|
previousAnswer := []string{}
|
||||||
return func() Tags {
|
return func() Tags {
|
||||||
answer := []string{}
|
answer := []string{}
|
||||||
|
|
||||||
@ -74,31 +71,16 @@ func (t Tags) Selector(exclude *regexp.Regexp, keepFilter bool) func() Tags {
|
|||||||
Message: "Tags:",
|
Message: "Tags:",
|
||||||
Options: options,
|
Options: options,
|
||||||
PageSize: 20,
|
PageSize: 20,
|
||||||
|
Default: previousAnswer,
|
||||||
}, &answer, survey.WithKeepFilter(keepFilter))
|
}, &answer, survey.WithKeepFilter(keepFilter))
|
||||||
|
|
||||||
result := make([]*Tag, 0, len(answer))
|
result := make([]*Tag, len(answer))
|
||||||
alreadySelected := map[int]bool{}
|
for i, a := range answer {
|
||||||
for _, a := range answer {
|
result[i] = tags[a]
|
||||||
if tags[a] == nil {
|
}
|
||||||
for _, p := range previousTags {
|
if keepPreviousAnswer {
|
||||||
if _, ok := alreadySelected[p.Id]; !ok {
|
previousAnswer = answer
|
||||||
result = append(result, p)
|
|
||||||
}
|
|
||||||
alreadySelected[p.Id] = true
|
|
||||||
}
|
|
||||||
} 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
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user