-
Notifications
You must be signed in to change notification settings - Fork 27
/
listitem.go
51 lines (40 loc) · 1.25 KB
/
listitem.go
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
package wishlist
import (
"strings"
"github.com/charmbracelet/bubbles/list"
)
var _ list.Item = ItemWrapper{}
// ItemWrapper wrappes an Endpoint and a set of descriptors and acts as a list.Item.
type ItemWrapper struct {
endpoint *Endpoint
descriptors []descriptor
styles styles
}
// FilterValue to abide the list.Item interface.
func (i ItemWrapper) FilterValue() string { return i.endpoint.Name }
// Title to abide the list.Item interface.
func (i ItemWrapper) Title() string { return i.endpoint.Name }
// Description to abide the list.Item interface.
func (i ItemWrapper) Description() string {
lines := make([]string, 0, len(i.descriptors))
for _, desc := range i.descriptors {
lines = append(lines, desc(i.endpoint, i.styles))
}
return strings.Join(lines, "\n")
}
type descriptor func(e *Endpoint, styles styles) string
func withSSHURL(i *Endpoint, _ styles) string {
return Link{URL: "ssh://" + i.Address}.String()
}
func withLink(i *Endpoint, styles styles) string {
if l := i.Link.String(); l != "" {
return l
}
return styles.NoContent.Render("no link")
}
func withDescription(i *Endpoint, styles styles) string {
if desc := strings.Split(i.Desc, "\n")[0]; desc != "" {
return desc
}
return styles.NoContent.Render("no description")
}