/*
* Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-only, see NOTICE and LICENSE for
* more information.
*/
package types
import (
"bytes"
"fmt"
"io"
"strings"
"text/template"
"git.src.quest/~skye/tamma/styles"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
// ActionItem represents an action to perform.
type ActionItem struct {
Name string
ExecTemplate string
A map[string]interface{}
}
// ExecString returns ExecTemplate with template variables filled in.
func (i ActionItem) ExecString(target TargetItem) (string, error) {
i.A["target"] = target
tmpl, err := template.New(i.Name).Parse(i.ExecTemplate)
if err != nil {
return "", err
}
var execStringBuffer bytes.Buffer
err = tmpl.Execute(&execStringBuffer, i)
if err != nil {
return "", err
}
return execStringBuffer.String(), nil
}
// FilterValue returns the value to filter on.
func (i ActionItem) FilterValue() string { return i.Name }
// ActionItemDelegate represents the visual of an ActionItem.
type ActionItemDelegate struct{}
// Height returns the needed height.
func (d ActionItemDelegate) Height() int { return 1 }
// Spacing returns the needed spacing.
func (d ActionItemDelegate) Spacing() int { return 0 }
// Update is the tea update loop.
func (d ActionItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
// Render renders the component.
func (d ActionItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(ActionItem)
if !ok {
return
}
str := fmt.Sprintf("%s", i.Name)
fn := styles.ListItem.Render
if index == m.Index() {
fn = func(s ...string) string {
return styles.ListItemSelected.Render("> " + strings.Join(s, " "))
}
}
fmt.Fprint(w, fn(str))
}