diff --git a/completers/bat_completer/cmd/action/action.go b/completers/bat_completer/cmd/action/action.go index 32517b805c..88dca88a20 100644 --- a/completers/bat_completer/cmd/action/action.go +++ b/completers/bat_completer/cmd/action/action.go @@ -17,7 +17,7 @@ func ActionLanguages() carapace.Action { if len(splitted) == 1 { values = append(values, splitted[0], "", style.Default) } else if len(splitted) > 1 { - values = append(values, splitted[0], splitted[1], style.ForPathExt("."+strings.SplitN(splitted[1], ",", 2)[0])) + values = append(values, splitted[0], splitted[1], style.ForPathExt("."+strings.SplitN(splitted[1], ",", 2)[0], c)) } } return carapace.ActionStyledValuesDescribed(values...) diff --git a/completers/deno_completer/cmd/fmt.go b/completers/deno_completer/cmd/fmt.go index baf73c5e9b..4d31b7943c 100644 --- a/completers/deno_completer/cmd/fmt.go +++ b/completers/deno_completer/cmd/fmt.go @@ -29,8 +29,8 @@ func init() { carapace.Gen(fmtCmd).FlagCompletion(carapace.ActionMap{ "config": carapace.ActionFiles(), - "ext": carapace.ActionValues("ts", "tsx", "js", "jsx", "md", "json", "jsonc").StyleF(func(s string) string { - return style.ForPathExt("." + s) + "ext": carapace.ActionValues("ts", "tsx", "js", "jsx", "md", "json", "jsonc").StyleF(func(s string, sc style.Context) string { + return style.ForPathExt("."+s, sc) }), "options-prose-wrap": carapace.ActionValues("always", "never", "preserve").StyleF(style.ForKeyword), }) diff --git a/completers/http_completer/cmd/root.go b/completers/http_completer/cmd/root.go index ebef6ff283..e32bf7a64d 100644 --- a/completers/http_completer/cmd/root.go +++ b/completers/http_completer/cmd/root.go @@ -224,7 +224,7 @@ func ActionRequestItem() carapace.Action { headers := http.ActionRequestHeaderNames().Invoke(c).Suffix(":") // use full context return carapace.ActionMultiParts("", func(c carapace.Context) carapace.Action { - return ActionSeparators() + return ActionSeparators().Style(style.Blue) }).Invoke(context).Merge(headers).ToA() }) } @@ -238,5 +238,5 @@ func ActionSeparators() carapace.Action { "@", "Form file fields (only with --form or --multipart)", "=@", "A data field like '=', but takes a file path and embeds its content", ":=@", "A raw JSON field like ':=', but takes a file path and embeds its content", - ) + ).Tag("separators") } diff --git a/completers/inkscape_completer/cmd/root.go b/completers/inkscape_completer/cmd/root.go index 335092f017..9345cc39c5 100644 --- a/completers/inkscape_completer/cmd/root.go +++ b/completers/inkscape_completer/cmd/root.go @@ -87,8 +87,8 @@ func init() { "display": os.ActionDisplays(), "export-filename": carapace.ActionFiles(), "export-pdf-version": carapace.ActionValues("1.4", "1.5"), - "export-type": carapace.ActionValues("svg", "png", "ps", "eps", "pdf", "emf", "wmf", "xaml").StyleF(func(s string) string { - return style.ForPathExt("." + s) + "export-type": carapace.ActionValues("svg", "png", "ps", "eps", "pdf", "emf", "wmf", "xaml").StyleF(func(s string, sc style.Context) string { + return style.ForPathExt("."+s, sc) }), }) diff --git a/completers/lncrawl_completer/cmd/root.go b/completers/lncrawl_completer/cmd/root.go index 71ddc1cdf1..4b0cdfdec2 100644 --- a/completers/lncrawl_completer/cmd/root.go +++ b/completers/lncrawl_completer/cmd/root.go @@ -54,8 +54,8 @@ func init() { carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ "crawler": carapace.ActionFiles(), "filename": carapace.ActionFiles(), - "format": carapace.ActionValues("json", "epub", "text", "web", "docx", "mobi", "pdf", "rtf", "txt", "azw3", "fb2", "lit", "lrf", "oeb", "pdb", "rb", "snb", "tcr").StyleF(func(s string) string { - return style.ForPathExt("." + s) + "format": carapace.ActionValues("json", "epub", "text", "web", "docx", "mobi", "pdf", "rtf", "txt", "azw3", "fb2", "lit", "lrf", "oeb", "pdb", "rb", "snb", "tcr").StyleF(func(s string, sc style.Context) string { + return style.ForPathExt("."+s, sc) }), "output": carapace.ActionDirectories(), }) diff --git a/completers/mkdir_completer/cmd/root.go b/completers/mkdir_completer/cmd/root.go index 30c0da460a..3eef4f2f2d 100644 --- a/completers/mkdir_completer/cmd/root.go +++ b/completers/mkdir_completer/cmd/root.go @@ -17,6 +17,8 @@ func Execute() error { return rootCmd.Execute() } func init() { + carapace.Gen(rootCmd).Standalone() + rootCmd.Flags().BoolS("Z", "Z", false, "set SELinux security context of each created directory to the default type") rootCmd.Flags().String("context", "", "like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX") rootCmd.Flags().Bool("help", false, "display this help and exit") diff --git a/go.mod b/go.mod index 90413598f4..1c413879a2 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/pelletier/go-toml v1.9.5 - github.com/rsteube/carapace v0.29.4 + github.com/rsteube/carapace v0.30.0 github.com/rsteube/carapace-spec v0.3.1 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index e6cd9ec0a8..d1be55d9c9 100644 --- a/go.sum +++ b/go.sum @@ -13,8 +13,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rsteube/carapace v0.29.4 h1:rSqXBvfGYflAMJLFRbZpf6HicweNL5w6iA3n0Znd2yE= -github.com/rsteube/carapace v0.29.4/go.mod h1:/ALYHicIpak6TjQnKl7HupclqJydy2LQb6CkawYBxDo= +github.com/rsteube/carapace v0.30.0 h1:tJjVNqz1eBZPvMyFcSwCyncNgcTYy0bI/xZnOidX59U= +github.com/rsteube/carapace v0.30.0/go.mod h1:/ALYHicIpak6TjQnKl7HupclqJydy2LQb6CkawYBxDo= github.com/rsteube/carapace-pflag v0.0.4 h1:Onb0cLNLxg1xJr2EsMlBldAI5KkybrvZ89b5cRElZXI= github.com/rsteube/carapace-pflag v0.0.4/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/rsteube/carapace-spec v0.3.1 h1:kAl7Iu9ALJ32W/5mo64rmrfgjaIR1J58Z6xw887dFZo= diff --git a/pkg/actions/fs/blockdevice.go b/pkg/actions/fs/blockdevice.go index e8eff2727e..3c97e56eac 100644 --- a/pkg/actions/fs/blockdevice.go +++ b/pkg/actions/fs/blockdevice.go @@ -40,10 +40,10 @@ func ActionBlockDevices() carapace.Action { return actionBlockdevices(func(blockdevices []blockdevice) carapace.Action { vals := make([]string, 0) for _, b := range blockdevices { - vals = append(vals, b.Path, fmt.Sprintf("%v %v", b.Size, b.Parttypename), style.ForPath(b.Path)) + vals = append(vals, b.Path, fmt.Sprintf("%v %v", b.Size, b.Parttypename)) } - return carapace.ActionStyledValuesDescribed(vals...) - }) + return carapace.ActionValuesDescribed(vals...).StyleF(style.ForPath) + }).Tag("block devices") } // TODO add examples to actions diff --git a/pkg/actions/fs/extension.go b/pkg/actions/fs/extension.go index c6868b9fad..7575b2631e 100644 --- a/pkg/actions/fs/extension.go +++ b/pkg/actions/fs/extension.go @@ -870,7 +870,7 @@ func ActionFilenameExtensions() carapace.Action { "zip", "Zip (file format)", "zrx", "REXX scripting language for ZOC_(software)", "zs", "Script for Minecraft mod MineTweaker and CraftTweaker", - ).StyleF(func(s string) string { - return style.ForPathExt("." + s) + ).StyleF(func(s string, sc style.Context) string { + return style.ForPathExt("."+s, sc) }) } diff --git a/pkg/actions/fs/jar.go b/pkg/actions/fs/jar.go index c1fb672092..e85be9e9d4 100644 --- a/pkg/actions/fs/jar.go +++ b/pkg/actions/fs/jar.go @@ -27,7 +27,7 @@ func ActionJarFileClasses(file string) carapace.Action { vals = append(vals, name) } } - return carapace.ActionValues(vals...).Style(style.ForPathExt(".class")) + return carapace.ActionValues(vals...).Style(style.ForPathExt(".class", c)) } }) } diff --git a/pkg/actions/net/http/http.go b/pkg/actions/net/http/http.go index 55fdb552ce..8530094596 100644 --- a/pkg/actions/net/http/http.go +++ b/pkg/actions/net/http/http.go @@ -71,7 +71,7 @@ func ActionRequestHeaderNames() carapace.Action { "Upgrade", "Ask the server to upgrade to another protocol. Must not be used in HTTP/2.[13]", "Via", "Informs the server of proxies through which the request was sent.", "Warning", "A general warning about possible problems with the entity body.", - ) + ).Tag("request headers") } // ActionRequestHeaderValues completes values for given request header @@ -111,41 +111,41 @@ func ActionRequestHeaderValues(header string) carapace.Action { func ActionMediaTypes() carapace.Action { return carapace.ActionCallback(func(c carapace.Context) carapace.Action { return carapace.ActionStyledValues( - "application/x-executable", style.ForPathExt(".exe"), + "application/x-executable", style.ForPathExt(".exe", c), "application/graphql", style.Default, - "application/javascript", style.ForPathExt(".js"), - "application/json", style.ForPathExt(".json"), - "application/ld+json", style.ForPathExt(".json"), - "application/msword", style.ForPathExt(".docx"), - "application/pdf", style.ForPathExt(".pdf"), - "application/sql", style.ForPathExt(".sql"), - "application/vnd.api+json", style.ForPathExt(".json"), - "application/vnd.ms-excel", style.ForPathExt(".xslx"), - "application/vnd.ms-powerpoint", style.ForPathExt(".pptx"), - "application/vnd.oasis.opendocument.text", style.ForPathExt(".odt"), - "application/vnd.openxmlformats-officedocument.presentationml.presentation", style.ForPathExt(".odp"), - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", style.ForPathExt(".ods"), - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", style.ForPathExt(".odt"), + "application/javascript", style.ForPathExt(".js", c), + "application/json", style.ForPathExt(".json", c), + "application/ld+json", style.ForPathExt(".json", c), + "application/msword", style.ForPathExt(".docx", c), + "application/pdf", style.ForPathExt(".pdf", c), + "application/sql", style.ForPathExt(".sql", c), + "application/vnd.api+json", style.ForPathExt(".json", c), + "application/vnd.ms-excel", style.ForPathExt(".xslx", c), + "application/vnd.ms-powerpoint", style.ForPathExt(".pptx", c), + "application/vnd.oasis.opendocument.text", style.ForPathExt(".odt", c), + "application/vnd.openxmlformats-officedocument.presentationml.presentation", style.ForPathExt(".odp", c), + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", style.ForPathExt(".ods", c), + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", style.ForPathExt(".odt", c), "application/x-www-form-urlencoded", style.Default, - "application/xml", style.ForPathExt(".xml"), - "application/zip", style.ForPathExt(".zip"), - "application/zstd", style.ForPathExt(".zstd"), - "audio/mpeg", style.ForPathExt(".mpeg"), - "audio/ogg", style.ForPathExt(".ogg"), - "image/gif", style.ForPathExt(".gif"), - "image/apng", style.ForPathExt(".apng"), - "image/flif", style.ForPathExt(".flif"), - "image/webp", style.ForPathExt(".webp"), - "image/x-mng", style.ForPathExt(".x-mng"), - "image/jpeg", style.ForPathExt(".jpeg"), - "image/png", style.ForPathExt(".png"), + "application/xml", style.ForPathExt(".xml", c), + "application/zip", style.ForPathExt(".zip", c), + "application/zstd", style.ForPathExt(".zstd", c), + "audio/mpeg", style.ForPathExt(".mpeg", c), + "audio/ogg", style.ForPathExt(".ogg", c), + "image/gif", style.ForPathExt(".gif", c), + "image/apng", style.ForPathExt(".apng", c), + "image/flif", style.ForPathExt(".flif", c), + "image/webp", style.ForPathExt(".webp", c), + "image/x-mng", style.ForPathExt(".x-mng", c), + "image/jpeg", style.ForPathExt(".jpeg", c), + "image/png", style.ForPathExt(".png", c), "multipart/form-data", style.Default, - "text/css", style.ForPathExt(".css"), - "text/csv", style.ForPathExt(".csv"), - "text/html", style.ForPathExt(".html"), - "text/php", style.ForPathExt(".php"), - "text/plain", style.ForPathExt(".txt"), - "text/xml", style.ForPathExt(".xml"), + "text/css", style.ForPathExt(".css", c), + "text/csv", style.ForPathExt(".csv", c), + "text/html", style.ForPathExt(".html", c), + "text/php", style.ForPathExt(".php", c), + "text/plain", style.ForPathExt(".txt", c), + "text/xml", style.ForPathExt(".xml", c), ) }) } diff --git a/pkg/actions/net/http/method.go b/pkg/actions/net/http/method.go index c7102be0af..c68b0ffc43 100644 --- a/pkg/actions/net/http/method.go +++ b/pkg/actions/net/http/method.go @@ -22,5 +22,5 @@ func ActionRequestMethods() carapace.Action { "PUT", "Replace all current representations of the target resource", styles.CarapaceBin.HttpMethodPUT, "TRACE", "Perform a message loop-back test along the path to the target resource", styles.CarapaceBin.HttpMethodTRACE, ) - }) + }).Tag("request methods") } diff --git a/pkg/actions/net/http/status.go b/pkg/actions/net/http/status.go index f85b716626..ab90f8fcd9 100644 --- a/pkg/actions/net/http/status.go +++ b/pkg/actions/net/http/status.go @@ -3,6 +3,7 @@ package http import ( "github.com/rsteube/carapace" "github.com/rsteube/carapace-bin/pkg/styles" + "github.com/rsteube/carapace/pkg/style" ) // ActionStatusCodes completes http status codes @@ -68,7 +69,7 @@ func ActionStatusCodes() carapace.Action { "508", "Loop Detected", "510", "Not Extended", "511", "Network Authentication Required", - ).StyleF(func(s string) string { + ).StyleF(func(s string, _ style.Context) string { switch []rune(s)[0] { case '1': return styles.CarapaceBin.HttpStatusInformational diff --git a/pkg/actions/net/net.go b/pkg/actions/net/net.go index c0c3b225c6..243b46c7c9 100644 --- a/pkg/actions/net/net.go +++ b/pkg/actions/net/net.go @@ -17,7 +17,7 @@ import ( // pihole func ActionHosts() carapace.Action { return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - vals := []string{} + batch := carapace.Batch() if file, err := c.Abs("~/.ssh/known_hosts"); err == nil { if content, err := os.ReadFile(file); err == nil { r := regexp.MustCompile(`^(?P[^ ,#]+)`) @@ -26,11 +26,11 @@ func ActionHosts() carapace.Action { for _, entry := range strings.Split(string(content), "\n") { if r.MatchString(entry) { if host := r.FindStringSubmatch(entry)[0]; rIPv4.MatchString(host) { - vals = append(vals, host, style.Default) + batch = append(batch, carapace.ActionStyledValues(host, style.Default).Tag("ipv4 addresses")) } else if rIPv6.MatchString(host) { - vals = append(vals, host, style.Bold) + batch = append(batch, carapace.ActionStyledValues(host, style.Bold).Tag("ipv6 addresses")) } else { - vals = append(vals, host, style.Blue) + batch = append(batch, carapace.ActionStyledValues(host, style.Blue).Tag("hostnames")) } } } @@ -38,7 +38,7 @@ func ActionHosts() carapace.Action { return carapace.ActionMessage(err.Error()) } } - return carapace.ActionStyledValues(vals...) + return batch.ToA() }) } @@ -144,7 +144,7 @@ func ActionDevices(includedDevices IncludedDevices) carapace.Action { }) } return carapace.ActionMessage("neither nmcli nor ifconfig available") - }) + }).Tag("network devices") } // ActionConnections completes stored network connections @@ -163,7 +163,7 @@ func ActionConnections() carapace.Action { } return carapace.ActionValuesDescribed(vals...) }) - }) + }).Tag("network connections") } // ActionBssids completes BSSID's of local wifi networks @@ -185,7 +185,7 @@ func ActionBssids() carapace.Action { } return carapace.ActionStyledValuesDescribed(vals...) }) - }) + }).Tag("bssids") } // ActionSsids completes SSID's of local wifi networks @@ -207,7 +207,7 @@ func ActionSsids() carapace.Action { } return carapace.ActionStyledValuesDescribed(vals...) }) - }) + }).Tag("ssids") } func styleForBars(s string) string { diff --git a/pkg/actions/os/executable.go b/pkg/actions/os/executable.go index bb2797bd89..9b870ed2b2 100644 --- a/pkg/actions/os/executable.go +++ b/pkg/actions/os/executable.go @@ -38,7 +38,7 @@ func actionDirectoryExecutables(dir string, prefix string, manDescriptions map[s if description == "" { description = manDescriptions[f.Name()] } - vals = append(vals, f.Name(), description, style.ForPath(dir+"/"+f.Name())) + vals = append(vals, f.Name(), description, style.ForPath(dir+"/"+f.Name(), c)) } } } diff --git a/pkg/actions/tools/docker/compose/service.go b/pkg/actions/tools/docker/compose/service.go index 738fa28a58..76d3ed2013 100644 --- a/pkg/actions/tools/docker/compose/service.go +++ b/pkg/actions/tools/docker/compose/service.go @@ -115,18 +115,20 @@ func (o ContainerOpts) includeState(s string) bool { // carapace-bin-bash-1 (exited) // carapace-bin-elvish-1 (running) func ActionContainers(opts ContainerOpts) carapace.Action { - return actionExecCompose(opts.Files, "ps", "--format", "json", "--all")(func(output []byte) carapace.Action { - var containers []container - if err := json.Unmarshal(output, &containers); err != nil { - return carapace.ActionMessage(err.Error()) - } + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return actionExecCompose(opts.Files, "ps", "--format", "json", "--all")(func(output []byte) carapace.Action { + var containers []container + if err := json.Unmarshal(output, &containers); err != nil { + return carapace.ActionMessage(err.Error()) + } - vals := make([]string, 0) - for _, c := range containers { - if opts.includeState(c.State) { - vals = append(vals, c.Name, c.State, style.ForKeyword(c.State)) + vals := make([]string, 0) + for _, container := range containers { + if opts.includeState(container.State) { + vals = append(vals, container.Name, container.State, style.ForKeyword(container.State, c)) + } } - } - return carapace.ActionStyledValuesDescribed(vals...) + return carapace.ActionStyledValuesDescribed(vals...) + }) }) } diff --git a/pkg/actions/tools/docker/docker.go b/pkg/actions/tools/docker/docker.go index 3fa030c229..5f5ace570a 100644 --- a/pkg/actions/tools/docker/docker.go +++ b/pkg/actions/tools/docker/docker.go @@ -123,7 +123,7 @@ func ActionContainerPath() carapace.Action { vals := make([]string, 0) for _, path := range lines[:len(lines)-1] { - vals = append(vals, path, style.ForPathExt(path)) + vals = append(vals, path, style.ForPathExt(path, c)) } return carapace.ActionStyledValues(vals...) }) diff --git a/pkg/actions/tools/gh/content.go b/pkg/actions/tools/gh/content.go index 78559b136d..6f52838840 100644 --- a/pkg/actions/tools/gh/content.go +++ b/pkg/actions/tools/gh/content.go @@ -40,7 +40,7 @@ func ActionContents(opts ContentOpts) carapace.Action { if content.Type == "dir" { name = content.Name + "/" } - vals = append(vals, name, style.ForPathExt(name)) + vals = append(vals, name, style.ForPathExt(name, c)) } return carapace.ActionStyledValues(vals...) }) diff --git a/pkg/actions/tools/git/change.go b/pkg/actions/tools/git/change.go index 13b211a8cb..511bef95df 100644 --- a/pkg/actions/tools/git/change.go +++ b/pkg/actions/tools/git/change.go @@ -43,9 +43,9 @@ func ActionChanges(opts ChangeOpts) carapace.Action { return carapace.ActionMessage(err.Error()) } else { if status := line[:2]; strings.Contains(status, "D") { // deleted - untracked = append(untracked, relativePath, status, style.ForPathExt(relativePath)) + untracked = append(untracked, relativePath, status, style.ForPathExt(relativePath, c)) } else { - untracked = append(untracked, relativePath, status, style.ForPath(relativePath)) + untracked = append(untracked, relativePath, status, style.ForPath(relativePath, c)) } } } diff --git a/pkg/styles/gh.go b/pkg/styles/gh.go index 39059217e2..88cc7e9103 100644 --- a/pkg/styles/gh.go +++ b/pkg/styles/gh.go @@ -44,7 +44,7 @@ func init() { style.Register("gh", &Gh) } -func (_gh *gh) ForState(s string) string { +func (_gh *gh) ForState(s string, _ style.Context) string { switch s { case "closed": return _gh.StateClosed