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)
- --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=<API Token>
export IGNR_HEIGHT=10
export IGNR_FILTER=startswith
export IGNR_START_SEARCH=false
```
## Commands

View File

@ -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))

View File

@ -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.

23
new.go
View File

@ -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)
@ -78,8 +88,9 @@ func runPrompt(client *github.Client, height int) (*github.Gitignore, error) {
Label: "Select a .gitignore template",
Items: templates,
Templates: selectTemplates,
Size: height,
Searcher: filterFunc(templates),
Size: pc.Height,
Searcher: filterFunc(templates, pc.FilterType),
StartInSearchMode: pc.StartSearch,
}
i, _, err := prompt.Run()