DEVELOPMENT ENVIRONMENT

~liljamo/tamma

tamma/types/host.go -rw-r--r-- 1.3 KiB
fef67040Jonni Liljamo chore: bump version to 0.1.2 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * 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))
}