Add Z.Dynamic, closes #86

pull/97/head v0.13.0
rwxrob 2 years ago
parent a4a073847b
commit 26dded5d30
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -582,12 +582,26 @@ func (x *Cmd) Set(key, val string) error {
return Vars.Set(path+key, val)
}
// Del is a shorter version of Z.Vars.Del(x.Path()+"."+key.val) for
// convenience. Logs the error Z.Vars is not defined (see UseVars).
func (x *Cmd) Del(key string) error {
if Vars == nil {
return UsesVars{x}
}
path := x.Path()
if path != "." {
path += "."
}
Vars.Del(path + key)
return nil
}
// Fill fills out the passed text/template string using the Cmd instance
// as the data object source for the template. It is called by the Get*
// family of field accessors but can be called directly as well. Also
// see markfunc.go for list of predefined template functions.
func (x *Cmd) Fill(tmpl string) string {
funcs := to.MergedMaps(markFuncMap, x.Dynamic)
funcs := to.MergedMaps(Dynamic, x.Dynamic)
t, err := template.New("t").Funcs(funcs).Parse(tmpl)
if err != nil {
log.Println(err)

@ -257,7 +257,7 @@ func ExampleCmd_UsageCmdShortcuts() {
},
}
fmt.Println(x.UsageCmdShortcuts())
// Output:
// Unordered Output:
// foo - a long way to foo
// bar - a long long way to bar
}

@ -13,7 +13,14 @@ import (
// in their Description and other places where templated BonzaiMark is
// allowed.
var markFuncMap = template.FuncMap{
// Dynamic contains the package global default template language which
// can be supplemented or overridden by Bonzai developers. Note this is in
// addition to any specific syntax added specifically to a Cmd with
// Cmd.Dynamic (which takes higher priority). Use Z.Dynamic when
// a shared template language is to be used across all Cmds within
// a single Bonzai tree or branch. This allows powerful, template-driven
// applications to work well with one another.
var Dynamic = template.FuncMap{
// semantic
"exe": func(a string) string { return term.Under + a + term.Reset },

Loading…
Cancel
Save