Add text/template rendering to Get* methods

pull/85/head v0.4.0
rwxrob 2 years ago
parent a3e22c48f8
commit d01674c3e0
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -7,10 +7,10 @@ Card](https://goreportcard.com/badge/github.com/rwxrob/bonzai)](https://goreport
Meticulously manicured monolith and multicall binaries, built from
imported composite commands, on *any* device, with recursive,
light-weight tab completion, and colorful embedded documentation from
terminal or local web browser. Replace messy collections of shell
scripts ported to clean Go code and compiled into a single, portable `z`
command.
light-weight tab completion, and colorful, rendered-templated
documentation from terminal or local web browser. Replace messy
collections of shell scripts ported to clean Go code and compiled into a
single, portable `z` command.
## 🤚 TL;DR; 🛑

@ -4,11 +4,13 @@
package Z
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"strings"
"text/template"
"github.com/rwxrob/bonzai"
"github.com/rwxrob/bonzai/comp"
@ -433,43 +435,58 @@ func (x *Cmd) Q(q string) string {
return Conf.Query(x.PathString() + "." + q)
}
// 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.
func (x *Cmd) Fill(tmpl string) string {
t, err := template.New("t").Parse(tmpl)
if err != nil {
log.Println(err)
}
var buf bytes.Buffer
if err := t.Execute(&buf, x); err != nil {
log.Println(err)
}
return buf.String()
}
// --------------------- bonzai.Command interface ---------------------
// GetName fulfills the bonzai.Command interface.
func (x *Cmd) GetName() string { return x.Name }
func (x *Cmd) GetName() string { return x.Fill(x.Name) }
// GetTitle fulfills the bonzai.Command interface.
func (x *Cmd) GetTitle() string { return x.Title() }
func (x *Cmd) GetTitle() string { return x.Fill(x.Title()) }
// GetAliases fulfills the bonzai.Command interface.
func (x *Cmd) GetAliases() []string { return x.Aliases }
// Summary fulfills the bonzai.Command interface.
func (x *Cmd) GetSummary() string { return x.Summary }
func (x *Cmd) GetSummary() string { return x.Fill(x.Summary) }
// Usage fulfills the bonzai.Command interface.
func (x *Cmd) GetUsage() string { return x.Usage }
func (x *Cmd) GetUsage() string { return x.Fill(x.Usage) }
// Version fulfills the bonzai.Command interface.
func (x *Cmd) GetVersion() string { return x.Version }
func (x *Cmd) GetVersion() string { return x.Fill(x.Version) }
// Copyright fulfills the bonzai.Command interface.
func (x *Cmd) GetCopyright() string { return x.Copyright }
func (x *Cmd) GetCopyright() string { return x.Fill(x.Copyright) }
// License fulfills the bonzai.Command interface.
func (x *Cmd) GetLicense() string { return x.License }
func (x *Cmd) GetLicense() string { return x.Fill(x.License) }
// Description fulfills the bonzai.Command interface.
func (x *Cmd) GetDescription() string { return x.Description }
func (x *Cmd) GetDescription() string { return x.Fill(x.Description) }
// Site fulfills the bonzai.Command interface.
func (x *Cmd) GetSite() string { return x.Site }
func (x *Cmd) GetSite() string { return x.Fill(x.Site) }
// Source fulfills the bonzai.Command interface.
func (x *Cmd) GetSource() string { return x.Source }
func (x *Cmd) GetSource() string { return x.Fill(x.Source) }
// Issues fulfills the bonzai.Command interface.
func (x *Cmd) GetIssues() string { return x.Issues }
func (x *Cmd) GetIssues() string { return x.Fill(x.Issues) }
// MinArgs fulfills the bonzai.Command interface.
func (x *Cmd) GetMinArgs() int { return x.MinArgs }
@ -508,13 +525,20 @@ func (x *Cmd) GetParams() []string { return x.Params }
func (x *Cmd) GetOther() []bonzai.Section {
var sections []bonzai.Section
for _, s := range x.Other {
s.Body = x.Fill(s.Body)
sections = append(sections, bonzai.Section(s))
}
return sections
}
// GetOtherTitles fulfills the bonzai.Command interface.
func (x *Cmd) GetOtherTitles() []string { return x.OtherTitles() }
func (x *Cmd) GetOtherTitles() []string {
var titles []string
for _, title := range x.OtherTitles() {
titles = append(titles, x.Fill(title))
}
return titles
}
// GetCompleter fulfills the Command interface.
func (x *Cmd) GetCompleter() bonzai.Completer { return x.Completer }

Loading…
Cancel
Save