diff --git a/README.md b/README.md index b5d940e..30429fd 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ go install github.com/onyx-and-iris/ignr@latest - --height/-H: Height of the selection prompt (default 10) - --filter/-f: Type of filter to apply to the list of templates (default startswith) - may be one of (startswith, contains) +- --start-search/-s: Start the prompt in search mode (default false) *environment variables* @@ -32,6 +33,7 @@ go install github.com/onyx-and-iris/ignr@latest export IGNR_TOKEN= export IGNR_HEIGHT=10 export IGNR_FILTER=startswith +export IGNR_START_SEARCH=false ``` ## Commands diff --git a/filter.go b/filter.go index f9aba31..cc9ca13 100644 --- a/filter.go +++ b/filter.go @@ -2,13 +2,11 @@ package main import ( "strings" - - "github.com/spf13/viper" ) // filterFunc returns a function that filters templates based on the specified filter type. -func filterFunc(templates []string) func(input string, index int) bool { - switch viper.GetString("filter") { +func filterFunc(templates []string, filterType string) func(input string, index int) bool { + switch filterType { case "contains": return func(input string, index int) bool { return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input)) diff --git a/main.go b/main.go index d16291c..a829c59 100644 --- a/main.go +++ b/main.go @@ -57,14 +57,17 @@ func init() { rootCmd.PersistentFlags().IntP("height", "H", 10, "Height of the selection prompt") rootCmd.PersistentFlags(). StringP("filter", "f", "startswith", "Type of filter to apply to the list of templates (e.g., 'startswith', 'contains')") + rootCmd.PersistentFlags().BoolP("start-search", "s", false, "Start the prompt in search mode") rootCmd.Flags().BoolP("version", "v", false, "Print the version of the CLI") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) viper.SetEnvPrefix("IGNR") viper.AutomaticEnv() viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")) viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height")) viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter")) + viper.BindPFlag("start-search", rootCmd.PersistentFlags().Lookup("start-search")) } // main is the entry point of the application. diff --git a/new.go b/new.go index fe4f1b8..f3bebfc 100644 --- a/new.go +++ b/new.go @@ -29,11 +29,21 @@ func init() { rootCmd.AddCommand(newCmd) } +type promptConfig struct { + Height int + StartSearch bool + FilterType string +} + // runNewCommand is the handler for the 'new' command. // It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file. func runNewCommand(cmd *cobra.Command, _ []string) error { - height := viper.GetInt("height") - if height <= 0 { + pc := promptConfig{ + Height: viper.GetInt("height"), + StartSearch: viper.GetBool("start-search"), + FilterType: viper.GetString("filter"), + } + if pc.Height <= 0 { return errors.New("height must be a positive integer") } @@ -42,7 +52,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error { return errors.New("failed to get GitHub client from context") } - content, err := runPrompt(client, height) + content, err := runPrompt(client, &pc) if err != nil { return fmt.Errorf("error running selection prompt: %w", err) } @@ -61,7 +71,7 @@ func runNewCommand(cmd *cobra.Command, _ []string) error { } // runPrompt is a helper function to run the selection prompt for .gitignore templates. -func runPrompt(client *github.Client, height int) (*github.Gitignore, error) { +func runPrompt(client *github.Client, pc *promptConfig) (*github.Gitignore, error) { templates, _, err := client.Gitignores.List(context.Background()) if err != nil { return nil, fmt.Errorf("error retrieving gitignore template list: %w", err) @@ -75,11 +85,12 @@ func runPrompt(client *github.Client, height int) (*github.Gitignore, error) { } prompt := promptui.Select{ - Label: "Select a .gitignore template", - Items: templates, - Templates: selectTemplates, - Size: height, - Searcher: filterFunc(templates), + Label: "Select a .gitignore template", + Items: templates, + Templates: selectTemplates, + Size: pc.Height, + Searcher: filterFunc(templates, pc.FilterType), + StartInSearchMode: pc.StartSearch, } i, _, err := prompt.Run()