add --start-search/-s flag

upd README
This commit is contained in:
onyx-and-iris 2025-06-18 08:56:05 +01:00
parent e1875fb894
commit cf93198462
4 changed files with 27 additions and 13 deletions

View File

@ -23,6 +23,7 @@ go install github.com/onyx-and-iris/ignr@latest
- --height/-H: Height of the selection prompt (default 10) - --height/-H: Height of the selection prompt (default 10)
- --filter/-f: Type of filter to apply to the list of templates (default startswith) - --filter/-f: Type of filter to apply to the list of templates (default startswith)
- may be one of (startswith, contains) - may be one of (startswith, contains)
- --start-search/-s: Start the prompt in search mode (default false)
*environment variables* *environment variables*
@ -32,6 +33,7 @@ go install github.com/onyx-and-iris/ignr@latest
export IGNR_TOKEN=<API Token> export IGNR_TOKEN=<API Token>
export IGNR_HEIGHT=10 export IGNR_HEIGHT=10
export IGNR_FILTER=startswith export IGNR_FILTER=startswith
export IGNR_START_SEARCH=false
``` ```
## Commands ## Commands

View File

@ -2,13 +2,11 @@ package main
import ( import (
"strings" "strings"
"github.com/spf13/viper"
) )
// filterFunc returns a function that filters templates based on the specified filter type. // filterFunc returns a function that filters templates based on the specified filter type.
func filterFunc(templates []string) func(input string, index int) bool { func filterFunc(templates []string, filterType string) func(input string, index int) bool {
switch viper.GetString("filter") { switch filterType {
case "contains": case "contains":
return func(input string, index int) bool { return func(input string, index int) bool {
return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input)) return strings.Contains(strings.ToLower(templates[index]), strings.ToLower(input))

View File

@ -57,14 +57,17 @@ func init() {
rootCmd.PersistentFlags().IntP("height", "H", 10, "Height of the selection prompt") rootCmd.PersistentFlags().IntP("height", "H", 10, "Height of the selection prompt")
rootCmd.PersistentFlags(). rootCmd.PersistentFlags().
StringP("filter", "f", "startswith", "Type of filter to apply to the list of templates (e.g., 'startswith', 'contains')") 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") rootCmd.Flags().BoolP("version", "v", false, "Print the version of the CLI")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.SetEnvPrefix("IGNR") viper.SetEnvPrefix("IGNR")
viper.AutomaticEnv() viper.AutomaticEnv()
viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token")) viper.BindPFlag("token", rootCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height")) viper.BindPFlag("height", rootCmd.PersistentFlags().Lookup("height"))
viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter")) viper.BindPFlag("filter", rootCmd.PersistentFlags().Lookup("filter"))
viper.BindPFlag("start-search", rootCmd.PersistentFlags().Lookup("start-search"))
} }
// main is the entry point of the application. // main is the entry point of the application.

29
new.go
View File

@ -29,11 +29,21 @@ func init() {
rootCmd.AddCommand(newCmd) rootCmd.AddCommand(newCmd)
} }
type promptConfig struct {
Height int
StartSearch bool
FilterType string
}
// runNewCommand is the handler for the 'new' command. // runNewCommand is the handler for the 'new' command.
// It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file. // It retrieves the selected .gitignore template from GitHub and writes it to the .gitignore file.
func runNewCommand(cmd *cobra.Command, _ []string) error { func runNewCommand(cmd *cobra.Command, _ []string) error {
height := viper.GetInt("height") pc := promptConfig{
if height <= 0 { 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") 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") return errors.New("failed to get GitHub client from context")
} }
content, err := runPrompt(client, height) content, err := runPrompt(client, &pc)
if err != nil { if err != nil {
return fmt.Errorf("error running selection prompt: %w", err) 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. // 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()) templates, _, err := client.Gitignores.List(context.Background())
if err != nil { if err != nil {
return nil, fmt.Errorf("error retrieving gitignore template list: %w", err) 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{ prompt := promptui.Select{
Label: "Select a .gitignore template", Label: "Select a .gitignore template",
Items: templates, Items: templates,
Templates: selectTemplates, Templates: selectTemplates,
Size: height, Size: pc.Height,
Searcher: filterFunc(templates), Searcher: filterFunc(templates, pc.FilterType),
StartInSearchMode: pc.StartSearch,
} }
i, _, err := prompt.Run() i, _, err := prompt.Run()