diff --git a/completers/bat_completer/cmd/action/action.go b/completers/bat_completer/cmd/action/action.go new file mode 100644 index 0000000000..9fa0cd8cbf --- /dev/null +++ b/completers/bat_completer/cmd/action/action.go @@ -0,0 +1,33 @@ +package action + +import ( + "github.com/rsteube/carapace" + "strings" +) + +func ActionLanguages() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("bat", "--list-languages")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + values := make([]string, 0) + for _, line := range lines[:len(lines)-1] { + splitted := strings.Split(line, ":") + if len(splitted) == 1 { + values = append(values, splitted[0], "") + } else if len(splitted) > 1 { + values = append(values, splitted[0], splitted[1]) + } + } + return carapace.ActionValuesDescribed(values...) + }) + }) +} + +func ActionThemes() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("bat", "--list-themes")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + return carapace.ActionValues(lines[:len(lines)-1]...) + }) + }) +} diff --git a/completers/bat_completer/cmd/root.go b/completers/bat_completer/cmd/root.go index 7f87321795..3f897c59f1 100644 --- a/completers/bat_completer/cmd/root.go +++ b/completers/bat_completer/cmd/root.go @@ -1,9 +1,8 @@ package cmd import ( - "strings" - "github.com/rsteube/carapace" + "github.com/rsteube/carapace-bin/completers/bat_completer/cmd/action" "github.com/spf13/cobra" ) @@ -49,9 +48,9 @@ func init() { "color": carapace.ActionValues("auto", "never", "always"), "decorations": carapace.ActionValues("auto", "never", "always"), "italic-text": carapace.ActionValues("never", "always"), - "language": ActionLanguage(), + "language": action.ActionLanguages(), "style": carapace.ActionValues("auto", "full", "plain", "changes", "header", "grid", "numbers", "snip"), - "theme": ActionTheme(), + "theme": action.ActionThemes(), "wrap": carapace.ActionValues("auto", "never", "character"), }) @@ -59,29 +58,3 @@ func init() { carapace.ActionFiles(), ) } - -func ActionLanguage() carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionExecCommand("bat", "--list-languages")(func(output []byte) carapace.Action { - values := make([]string, 0) - for _, line := range strings.Split(string(output), "\n") { - // TODO fix ':' character in carapace - splitted := strings.Split(line, ":") - if len(splitted) == 1 { - values = append(values, splitted[0], "") - } else if len(splitted) > 1 { - values = append(values, splitted[0], splitted[1]) - } - } - return carapace.ActionValuesDescribed(values...) - }) - }) -} - -func ActionTheme() carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionExecCommand("bat", "--list-themes")(func(output []byte) carapace.Action { - return carapace.ActionValues(strings.Split(string(output), "\n")...) - }) - }) -}