-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat)!: global update, interface change and project structure change
- Loading branch information
Showing
15 changed files
with
190 additions
and
355 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,98 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/log" | ||
cc "github.com/ivanpirog/coloredcobra" | ||
"github.com/manifoldco/promptui" | ||
"github.com/neptship/ignore/internal" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "ignore", | ||
Short: "Create files .ignore quickly and simply", | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ignoreFile, err := chooseIgnoreFile() | ||
if err != nil { | ||
internal.CallClear() | ||
os.Exit(1) | ||
} | ||
ignoreTemplate, err := chooseIgnoreTemplate() | ||
if err != nil { | ||
internal.CallClear() | ||
os.Exit(1) | ||
} | ||
internal.AddIgnoreTemplate(ignoreFile, ignoreTemplate) | ||
}, | ||
} | ||
|
||
func chooseIgnoreFile() (string, error) { | ||
tr := internal.NewTemplateRegistry() | ||
templatesIgnore := tr.List() | ||
|
||
templates := &promptui.SelectTemplates{ | ||
Active: `{{">" | blue | bold }} {{ . | blue | bold }}`, | ||
Inactive: `{{.}}`, | ||
Selected: `{{ "√ " | green | bold }} {{ "Choose .ignore file" | bold }} {{"»" | black}} {{ . | blue }}`, | ||
Label: `{{ . | bold }}`, | ||
} | ||
|
||
prompt := promptui.Select{ | ||
Label: promptui.IconInitial + " Choose .ignore file", | ||
Templates: templates, | ||
Items: internal.IgnoreFiles, | ||
Size: 8, | ||
Searcher: func(input string, index int) bool { | ||
pepper := templatesIgnore[index] | ||
name := strings.Replace(strings.ToLower(pepper), " ", "", -1) | ||
input = strings.Replace(strings.ToLower(input), " ", "", -1) | ||
|
||
return strings.Contains(name, input) | ||
}, | ||
} | ||
|
||
_, ignoreFile, err := prompt.Run() | ||
return ignoreFile, err | ||
} | ||
|
||
func chooseIgnoreTemplate() (string, error) { | ||
tr := internal.NewTemplateRegistry() | ||
templatesIgnore := tr.List() | ||
|
||
templates := &promptui.SelectTemplates{ | ||
Active: `{{">" | blue | bold }} {{ . | blue | bold }}`, | ||
Inactive: `{{ . | underline}}`, | ||
Selected: `{{ "√ " | green | bold }} {{ "Choose .ignore template" | bold }} {{"»" | black}} {{ . | blue }}`, | ||
Label: `{{ . | bold }}`, | ||
} | ||
|
||
prompt := promptui.Select{ | ||
Label: promptui.IconInitial + " Choose .ignore template", | ||
Templates: templates, | ||
Items: templatesIgnore, | ||
Size: 8, | ||
Searcher: func(input string, index int) bool { | ||
pepper := templatesIgnore[index] | ||
name := strings.Replace(strings.ToLower(pepper), " ", "", -1) | ||
input = strings.Replace(strings.ToLower(input), " ", "", -1) | ||
|
||
return strings.Contains(name, input) | ||
}, | ||
} | ||
|
||
_, ignoreTemplate, err := prompt.Run() | ||
return ignoreTemplate, err | ||
} | ||
|
||
func Execute() { | ||
cc.Init(&cc.Config{ | ||
RootCmd: rootCmd, | ||
Headings: cc.HiBlue + cc.Bold + cc.Underline, | ||
Commands: cc.HiYellow + cc.Bold, | ||
Example: cc.Italic, | ||
ExecName: cc.Bold, | ||
Flags: cc.Bold, | ||
FlagsDataType: cc.Italic + cc.HiBlue, | ||
NoExtraNewlines: true, | ||
NoBottomNewline: true, | ||
}) | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
internal.CallClear() | ||
} | ||
} | ||
|
||
func init() { | ||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
func handleErr(err error) { | ||
if err == nil { | ||
return | ||
} | ||
|
||
log.Error(err) | ||
_, _ = fmt.Fprintf( | ||
os.Stderr, | ||
"%s\n", | ||
strings.Trim(err.Error(), " \n"), | ||
) | ||
os.Exit(1) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.