diff --git a/cmd/piwigo-cli/images_tag.go b/cmd/piwigo-cli/images_tag.go index 8849304..f424a11 100644 --- a/cmd/piwigo-cli/images_tag.go +++ b/cmd/piwigo-cli/images_tag.go @@ -15,12 +15,13 @@ import ( ) type ImagesTagCommand struct { - 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"` - 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"` + 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"` + 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"` + 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 { diff --git a/internal/piwigo/piwigotools/tags.go b/internal/piwigo/piwigotools/tags.go index 4dbd403..13b502a 100644 --- a/internal/piwigo/piwigotools/tags.go +++ b/internal/piwigo/piwigotools/tags.go @@ -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) - } - alreadySelected[p.Id] = true - } - } else { - if _, ok := alreadySelected[tags[a].Id]; !ok { - result = append(result, tags[a]) - } - alreadySelected[tags[a].Id] = true - } + result := make([]*Tag, len(answer)) + for i, a := range answer { + result[i] = tags[a] + } + if keepPreviousAnswer { + previousAnswer = answer } - - sort.Slice(result, func(i, j int) bool { - return result[i].Name < result[j].Name - }) - - previousTags = result return result }