Skip to content

Commit

Permalink
charm templates: change up method signatures for easier use
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaln7 committed Aug 17, 2022
1 parent f2c6f5c commit 041675b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
9 changes: 8 additions & 1 deletion cmd/charm-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,19 @@ func main() {
charm.TextWarning.S(dur.Truncate(time.Second).String()),
)

if err := charm.TemplateBuffered(charm.NewTextBox().Success(), heredoc.Doc(`
if err := charm.TemplateBufferedE(charm.NewTextBox().Success(), heredoc.Doc(`
{{ success checkmark }} Successfully built {{ success .img }} in {{ warning (duration .dur) }}`,
), map[string]any{
"img": img,
"dur": dur,
}); err != nil {
panic(err)
}

charm.TemplateBuffered(charm.NewTextBox().Success(), heredoc.Doc(`
{{ success checkmark }} Successfully built {{ success .img }} in {{ warning (duration .dur) }}`,
), map[string]any{
"img": img,
"dur": dur,
})
}
6 changes: 2 additions & 4 deletions commands/apps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,16 +298,14 @@ func RunAppsDevBuild(c *CmdConfig) error {
// charm.TextSuccess.S(res.BuildDuration.Truncate(time.Second).String()),
// )

if err := charm.TemplateBuffered(
charm.TemplateBuffered(
charm.NewTextBox().Success(),
`{{ success checkmark }} Successfully built {{ success .img }} in {{ warning (duration .dur) }}`,
map[string]any{
"img": res.Image,
"dur": res.BuildDuration,
},
); err != nil {
return err
}
)
return nil
}

Expand Down
9 changes: 7 additions & 2 deletions commands/charm/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package charm

import (
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/paginator"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var (
// ListDefaultStyle is the default list style.
ListDefaultStyle = Style{lipgloss.NewStyle().Margin(1, 2)}
ListDefaultStyle = Style{lipgloss.NewStyle().Margin(1, 1)}
)

// List is a component used to select an item from a list.
Expand All @@ -31,8 +32,12 @@ func NewList(items []list.Item) *List {
// TODO: accept an optional sample item for the height
delegate.SetHeight(3)

model := list.New(items, delegate, 0, 0)
model.Paginator.Type = paginator.Arabic
model.Paginator.ArabicFormat = "page %d of %d"

return &List{
model: list.New(items, delegate, 0, 0),
model: model,
Style: ListDefaultStyle,
}
}
Expand Down
26 changes: 22 additions & 4 deletions commands/charm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func Template(w io.Writer, content string, data any) error {
return tmpl.Execute(w, data)
}

// TemplateBuffered executes a template and writes the result to the given writer once.
func TemplateBuffered(w io.Writer, content string, data any) error {
// TemplateBufferedE executes a template and writes the result to the given writer once.
func TemplateBufferedE(w io.Writer, content string, data any) error {
var buf bytes.Buffer
err := Template(&buf, content, data)
if err != nil {
Expand All @@ -73,8 +73,17 @@ func TemplateBuffered(w io.Writer, content string, data any) error {
return err
}

// TemplateString executes a template and returns it as a string.
func TemplateString(content string, data any) (string, error) {
// TemplateBuffered executes a template and writes the result to the given writer once. If an error occurs, it is written
// to the writer instead.
func TemplateBuffered(w io.Writer, content string, data any) {
err := TemplateBufferedE(w, content, data)
if err != nil {
w.Write([]byte(err.Error()))
}
}

// TemplateStringE executes a template and returns it as a string.
func TemplateStringE(content string, data any) (string, error) {
var buf bytes.Buffer
err := Template(&buf, content, data)
if err != nil {
Expand All @@ -83,6 +92,15 @@ func TemplateString(content string, data any) (string, error) {
return buf.String(), nil
}

// TemplateString executes a template and returns it as a string. If an error occurs, the error text is returned instead.
func TemplateString(content string, data any) string {
res, err := TemplateStringE(content, data)
if err != nil {
return err.Error()
}
return res
}

// TemplatePrint executes a template and prints it directly to stdout.
func TemplatePrint(content string, data any) error {
return Template(os.Stdout, content, data)
Expand Down

0 comments on commit 041675b

Please sign in to comment.