diff --git a/completions_powershell.go b/completions_powershell.go index a0f271a..3c4cf4b 100644 --- a/completions_powershell.go +++ b/completions_powershell.go @@ -9,11 +9,23 @@ import ( func WritePowerShellCompletions(out io.Writer, rootCmd *Command) error { return powerShellTpl.Execute(out, map[string]any{ - "rootCmd": rootCmd, - "globalOpts": opt.Globals(), + "RootCmd": rootCmd, + "GlobalOpts": opt.Globals(), }) } -var powerShellTpl = template.Must(template.New("PowerShell").Parse(` - +var powerShellTpl = template.Must(template.New("PowerShell").Funcs(tplFuncs).Parse(` +{{- $rootCmd := .RootCmd -}} +{{- $progName := $rootCmd.Name -}} +{{- $varName := $rootCmd.Name | camel -}} + +[scriptblock]$__{{ $varName }}CompleterBlock = { + param( + $WordToComplete, + $CommandAst, + $CursorPosition + ) +} + +Register-ArgumentCompleter -CommandName {{ $progName }} -ScriptBlock $__{{ $varName }}CompleterBlock `)) diff --git a/completions_zsh.go b/completions_zsh.go index 50f0cf7..6c12903 100644 --- a/completions_zsh.go +++ b/completions_zsh.go @@ -15,6 +15,7 @@ func WriteZshCompletions(out io.Writer, rootCmd *Command) error { } // TODO: Fix indentation and other spacing +// TODO: Implement global options var zshTpl = template.Must(template.New("zsh").Funcs(tplFuncs).Parse(` {{- $rootCmd := .RootCmd -}} {{- $progName := $rootCmd.Name -}} diff --git a/go.mod b/go.mod index f6fed3c..3aac729 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module go.fifitido.net/cmd go 1.21.3 + +require github.com/iancoleman/strcase v0.3.0 diff --git a/go.sum b/go.sum index e69de29..6261b6a 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= +github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= diff --git a/tpl_funcs.go b/tpl_funcs.go index 834f3a4..db95e70 100644 --- a/tpl_funcs.go +++ b/tpl_funcs.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "text/template" + + "github.com/iancoleman/strcase" ) var tplFuncs = template.FuncMap{ @@ -19,6 +21,8 @@ var tplFuncs = template.FuncMap{ "sub": tplSub, "dec": tplDec, "mult": tplMult, + "pascal": tplPascal, + "camel": tplCamel, } func tplMap(vals ...any) (map[string]any, error) { @@ -42,7 +46,7 @@ func tplJoin(strs []string, sep string) string { } func tplUnder(s string) string { - return strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(s, " ", "_"), "-", "_")) + return strcase.ToSnake(s) } func tplVarPrefix(s string) string { @@ -80,3 +84,11 @@ func tplDec(i int) int { func tplMult(a, b int) int { return a * b } + +func tplPascal(s string) string { + return strcase.ToCamel(s) +} + +func tplCamel(s string) string { + return strcase.ToLowerCamel(s) +}