Skip to content

Commit

Permalink
bat: skip empty line in themes/languages
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Aug 9, 2021
1 parent 6564c9f commit f33d3a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
33 changes: 33 additions & 0 deletions completers/bat_completer/cmd/action/action.go
Original file line number Diff line number Diff line change
@@ -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]...)
})
})
}
33 changes: 3 additions & 30 deletions completers/bat_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -49,39 +48,13 @@ 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"),
})

carapace.Gen(rootCmd).PositionalAnyCompletion(
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")...)
})
})
}

0 comments on commit f33d3a8

Please sign in to comment.