-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/sh | ||
|
||
scriptdir=$(dirname $(readlink -f $0)) | ||
|
||
echo "# Conditions" | ||
echo | ||
|
||
$scriptdir/../cmd/carapace/carapace --condition \ | ||
| sed -r 's_(https://[^ ]+)_[\1](\1)_' \ | ||
| sed 's_^\([^ ]\+\) \+\(.*\)_- [\1]\(https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/conditions/#Condition\1) \2_' \ | ||
| sed -e ':loop' -e 's_\(carapace-bin/pkg/condition/[^#]*\)[.]_\1/_' -e 't loop' |
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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/rsteube/carapace-bin/pkg/conditions" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var conditionCmd = &cobra.Command{ | ||
Use: "--condition [condition]", | ||
Short: "list or execute condition", | ||
Args: cobra.ArbitraryArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
switch len(args) { | ||
case 0: | ||
printConditions() | ||
case 1: | ||
printCondition(strings.SplitN(args[0], "(", 2)[0]) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(conditionCmd).Standalone() | ||
macroCmd.Flags().SetInterspersed(false) | ||
|
||
carapace.Gen(conditionCmd).PositionalCompletion( | ||
carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
vals := make([]string, 0) | ||
for name, condition := range conditions.MacroMap { | ||
vals = append(vals, name, condition.Description) | ||
} | ||
return carapace.ActionValuesDescribed(vals...) | ||
}), | ||
carapace.ActionCallback(func(c carapace.Context) carapace.Action { | ||
m, err := conditions.MacroMap.Lookup("$" + c.Args[0]) | ||
if err != nil { | ||
return carapace.ActionMessage(err.Error()) | ||
} | ||
condition := m.Parse("$" + c.Args[0]) | ||
|
||
return carapace.ActionValues(strconv.FormatBool(condition(c))) | ||
}), | ||
) | ||
} | ||
|
||
func printConditions() { | ||
maxlen := 0 | ||
names := make([]string, 0) | ||
for name := range conditions.MacroMap { | ||
names = append(names, name) | ||
if len := len(name); len > maxlen { | ||
maxlen = len | ||
} | ||
} | ||
|
||
sort.Strings(names) | ||
for _, name := range names { | ||
fmt.Printf("%-"+strconv.Itoa(maxlen)+"v %v\n", name, conditions.MacroMap[name].Description) | ||
} | ||
} | ||
|
||
func printCondition(name string) { | ||
if m, ok := conditions.MacroMap[name]; ok { | ||
path := strings.Replace(name, ".", "/", -1) | ||
signature := "" | ||
if s := m.Signature(); s != "" { | ||
signature = fmt.Sprintf("(%v)", s) | ||
} | ||
|
||
fmt.Printf(`signature: $%v%v | ||
description: %v | ||
reference: https://pkg.go.dev/github.com/rsteube/carapace-bin/pkg/conditions/%v#Condition%v | ||
`, name, signature, m.Description, filepath.Dir(path), filepath.Base(path)) | ||
} | ||
} |
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