-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeligates.go
65 lines (51 loc) · 1.58 KB
/
deligates.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"io"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type chatItemDelegate struct{}
func (d chatItemDelegate) Height() int { return 1 }
func (d chatItemDelegate) Spacing() int { return 0 }
func (d chatItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d chatItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(Chat)
if !ok {
return
}
str := fmt.Sprintf("%s", i.name)
fn := itemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render(strings.Join(s, " "))
}
}
fmt.Fprint(w, fn(str))
}
type messageItemDelegate struct{}
func (d messageItemDelegate) Height() int { return 1 }
func (d messageItemDelegate) Spacing() int { return 0 }
func (d messageItemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d messageItemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(Message)
if !ok {
return
}
head := lipgloss.NewStyle().Bold(true).Render(i.From)
var str string
if i.Type == "message" {
str = fmt.Sprintf("%s: %s", head, i.Content)
} else {
str = fmt.Sprintf("%s: %s", head, "🖹 "+i.Content)
}
fn := itemStyle.Render
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render(strings.Join(s, " "))
}
}
fmt.Fprint(w, fn(str))
}