/*
* Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-only, see NOTICE and LICENSE for
* more information.
*/
package types
import (
"fmt"
"io"
"strings"
"git.src.quest/~skye/tamma/styles"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
// HostItem represents a host item.
type HostItem struct {
Name string
IP string
Data map[string]interface{}
}
// FilterValue returns the value to filter on.
func (i HostItem) FilterValue() string { return i.Name }
// HostItemDelegate represents the visual of a HostItem.
type HostItemDelegate struct{}
// Height returns the needed height.
func (d HostItemDelegate) Height() int { return 1 }
// Spacing returns the needed spacing.
func (d HostItemDelegate) Spacing() int { return 0 }
// Update is the tea update loop.
func (d HostItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
// Render renders the component.
func (d HostItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(HostItem)
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))
}