From ad27fc695e9a2f1678fbd0b59b691e368334ecc9 Mon Sep 17 00:00:00 2001 From: AlexNg Date: Mon, 8 Apr 2024 23:40:02 +0800 Subject: [PATCH] feat: Add query flag with RegExp and string.Contains() support --- src/list.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/list.go b/src/list.go index ee0d76c..9748a2b 100644 --- a/src/list.go +++ b/src/list.go @@ -3,6 +3,7 @@ package src import ( "fmt" "os" + "regexp" "strings" "github.com/caffeine-addictt/video-manager/src/utils" @@ -10,6 +11,7 @@ import ( ) var listFlags struct { + query string allowExt []string rejectExt []string count int @@ -42,11 +44,24 @@ var listCommand = &cobra.Command{ Debug(fmt.Sprintf("Merged extensions: %v", extMap)) Info("Filtering extensions...") - if considerAllow || considerReject { + if considerAllow || considerReject || listFlags.query != "" { for i := 0; i < len(filenames); i++ { split := strings.Split(filenames[i], ".") extension := split[len(split)-1] + // if !strings.Contains(filenames[i], listFlags.query) { + match, err := regexp.MatchString(listFlags.query, filenames[i]) + if err != nil { + Info("RegExp failed to match " + filenames[i] + ": " + err.Error()) + } + + if (err != nil && !match) || (err == nil && !strings.Contains(filenames[i], listFlags.query)) { + Debug(fmt.Sprintf("%s does not contain or match %s", filenames[i], listFlags.query)) + filenames[len(filenames)-1], filenames[i], filenames = "", filenames[len(filenames)-1], filenames[:len(filenames)-1] + i-- + continue + } + _, ok := extMap[extension] if (considerAllow && !ok) || (considerReject && ok) { filenames[len(filenames)-1], filenames[i], filenames = "", filenames[len(filenames)-1], filenames[:len(filenames)-1] @@ -66,6 +81,7 @@ var listCommand = &cobra.Command{ func init() { rootCommand.AddCommand(listCommand) listCommand.Flags().IntVarP(&listFlags.count, "count", "n", 0, "Number of videos to list [0 = all] (defaults to 0)") + listCommand.Flags().StringVarP(&listFlags.query, "query", "q", "", "Query string to filter results (defaults to '')") listCommand.Flags().StringSliceVarP(&listFlags.allowExt, "allow", "a", []string{}, "The extensions of the videos to list [mp4 etc.] (defaults to [])") listCommand.Flags().StringSliceVarP(&listFlags.rejectExt, "exclude", "e", []string{}, "The extensions of the videos to list exclude [mp4 etc] (defaults to [])")